mathu.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2004 by Florian Klaempfl
  4. member of the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$ASMMODE GAS}
  12. {$ifdef FPC_HAS_TYPE_EXTENDED}
  13. {$define FPC_MATH_HAS_ARCTAN2}
  14. function arctan2(y,x : float) : float;assembler;
  15. asm
  16. fldt y
  17. fldt x
  18. fpatan
  19. fwait
  20. end;
  21. {$define FPC_MATH_HAS_TAN}
  22. function tan(x : float) : float;assembler;
  23. asm
  24. fldt X
  25. fptan
  26. fstp %st
  27. fwait
  28. end;
  29. {$define FPC_MATH_HAS_COTAN}
  30. function cotan(x : float) : float;assembler;
  31. asm
  32. fldt X
  33. fptan
  34. fdivp %st,%st(1)
  35. fwait
  36. end;
  37. {$define FPC_MATH_HAS_LOG2}
  38. function log2(x : float) : float;assembler;
  39. asm
  40. fld1
  41. fldt x
  42. fyl2x
  43. fwait
  44. end;
  45. {$endif FPC_HAS_TYPE_EXTENDED}
  46. {$define FPC_MATH_HAS_SINCOS}
  47. {$ifdef FPC_HAS_TYPE_EXTENDED}
  48. procedure sincos(theta : extended;out sinus,cosinus : extended);assembler;
  49. asm
  50. fldt theta
  51. fsincos
  52. {$ifdef WIN64}
  53. fstpl (%r8)
  54. fstpl (%rdx)
  55. {$else WIN64}
  56. fstpt (%rsi)
  57. fstpt (%rdi)
  58. {$endif WIN64}
  59. fwait
  60. end;
  61. {$endif FPC_HAS_TYPE_EXTENDED}
  62. {$asmmode intel}
  63. procedure sincos(theta : double;out sinus,cosinus : double);assembler;
  64. var
  65. t : double;
  66. asm
  67. movsd qword ptr t,xmm0
  68. fld qword ptr t
  69. fsincos
  70. fstp qword ptr [cosinus]
  71. fstp qword ptr [sinus]
  72. fwait
  73. end;
  74. procedure sincos(theta : single;out sinus,cosinus : single);assembler;
  75. var
  76. t : single;
  77. asm
  78. movss dword ptr t,xmm0
  79. fld dword ptr t
  80. fsincos
  81. fstp dword ptr [cosinus]
  82. fstp dword ptr [sinus]
  83. fwait
  84. end;
  85. {$define FPC_MATH_HAS_DIVMOD}
  86. {$asmmode intel}
  87. procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: Word);assembler;
  88. asm
  89. {$ifdef WIN64}
  90. mov eax, ecx
  91. movzx ecx, dx
  92. cdq
  93. idiv ecx
  94. mov [r8], ax
  95. mov [r9], dx
  96. {$else WIN64}
  97. mov eax, edi
  98. movzx esi, si
  99. mov rdi, rdx
  100. cdq
  101. idiv esi
  102. mov [rdi], ax
  103. mov [rcx], dx
  104. {$endif WIN64}
  105. end;
  106. procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: SmallInt);assembler;
  107. asm
  108. {$ifdef WIN64}
  109. mov eax, ecx
  110. movzx ecx, dx
  111. cdq
  112. idiv ecx
  113. mov [r8], ax
  114. mov [r9], dx
  115. {$else WIN64}
  116. mov eax, edi
  117. movzx esi, si
  118. mov rdi, rdx
  119. cdq
  120. idiv esi
  121. mov [rdi], ax
  122. mov [rcx], dx
  123. {$endif WIN64}
  124. end;
  125. procedure DivMod(Dividend: DWord; Divisor: DWord; var Result, Remainder: DWord);assembler;
  126. asm
  127. {$ifdef WIN64}
  128. mov eax, ecx
  129. mov ecx, edx
  130. xor edx, edx
  131. div ecx
  132. mov [r8], eax
  133. mov [r9], edx
  134. {$else WIN64}
  135. mov eax, edi
  136. mov rdi, rdx
  137. xor edx, edx
  138. div esi
  139. mov [rdi], eax
  140. mov [rcx], edx
  141. {$endif WIN64}
  142. end;
  143. procedure DivMod(Dividend: Integer; Divisor: Integer; var Result, Remainder: Integer);assembler;
  144. asm
  145. {$ifdef WIN64}
  146. mov eax, ecx
  147. mov ecx, edx
  148. cdq
  149. idiv ecx
  150. mov [r8], eax
  151. mov [r9], edx
  152. {$else WIN64}
  153. mov eax, edi
  154. mov rdi, rdx
  155. cdq
  156. idiv esi
  157. mov [rdi], eax
  158. mov [rcx], edx
  159. {$endif WIN64}
  160. end;
  161. {$asmmode gas}
  162. function GetRoundMode: TFPURoundingMode;
  163. begin
  164. {$ifndef FPC_HAS_TYPE_EXTENDED}
  165. Result:=TFPURoundingMode((GetMXCSR shr 13) and $3);
  166. {$else win64}
  167. Result:=TFPURoundingMode((Get8087CW shr 10) and $3);
  168. {$endif win64}
  169. end;
  170. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  171. var
  172. CtlWord: Word;
  173. SSECSR: dword;
  174. begin
  175. CtlWord:=Get8087CW;
  176. SSECSR:=GetMXCSR;
  177. Set8087CW((CtlWord and $F3FF) or (Ord(RoundMode) shl 10));
  178. SetMXCSR((SSECSR and $ffff9fff) or (dword(RoundMode) shl 13));
  179. {$ifdef FPC_HAS_TYPE_EXTENDED}
  180. Result:=TFPURoundingMode((CtlWord shr 10) and 3);
  181. {$else}
  182. Result:=TFPURoundingMode((SSECSR shr 13) and 3);
  183. {$endif FPC_HAS_TYPE_EXTENDED}
  184. end;
  185. function GetPrecisionMode: TFPUPrecisionMode;
  186. begin
  187. Result:=TFPUPrecisionMode((Get8087CW shr 8) and 3);
  188. end;
  189. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  190. var
  191. CtlWord: Word;
  192. begin
  193. CtlWord:=Get8087CW;
  194. Set8087CW((CtlWord and $FCFF) or (Ord(Precision) shl 8));
  195. Result:=TFPUPrecisionMode((CtlWord shr 8) and 3);
  196. end;
  197. function GetExceptionMask: TFPUExceptionMask;
  198. begin
  199. {$ifndef FPC_HAS_TYPE_EXTENDED}
  200. Result:=TFPUExceptionMask(dword((GetMXCSR shr 7) and $3f));
  201. {$else win64}
  202. Result:=TFPUExceptionMask(dword(Get8087CW and $3F));
  203. {$endif win64}
  204. end;
  205. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  206. var
  207. CtlWord: Word;
  208. SSECSR: dword;
  209. begin
  210. CtlWord:=Get8087CW;
  211. SSECSR:=GetMXCSR;
  212. Set8087CW((CtlWord and $FFC0) or Byte(Longint(Mask)));
  213. SetMXCSR((SSECSR and $ffffe07f) or (dword(Mask) shl 7));
  214. {$ifdef FPC_HAS_TYPE_EXTENDED}
  215. Result:=TFPUExceptionMask(dword(CtlWord and $3F));
  216. {$else}
  217. Result:=TFPUExceptionMask((SSECSR shr 7) and $3F);
  218. {$endif FPC_HAS_TYPE_EXTENDED}
  219. end;
  220. procedure ClearExceptions(RaisePending: Boolean);assembler;
  221. asm
  222. cmpb $0,RaisePending
  223. je .Lclear
  224. fwait
  225. .Lclear:
  226. fnclex
  227. end;