math.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. {
  2. Implementation of mathematical routines for x86_64
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2005 by 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. label
  12. FPC_ABSMASK_DOUBLE,
  13. FPC_ABSMASK_SINGLE;
  14. procedure dummyproc;assembler;
  15. asm
  16. .data
  17. .balign 16
  18. .globl FPC_ABSMASK_SINGLE
  19. FPC_ABSMASK_SINGLE:
  20. .quad 0x7FFFFFFF7FFFFFFF
  21. .quad 0x7FFFFFFF7FFFFFFF
  22. .globl FPC_ABSMASK_DOUBLE
  23. FPC_ABSMASK_DOUBLE:
  24. .quad 0x7FFFFFFFFFFFFFFF
  25. .quad 0x7FFFFFFFFFFFFFFF
  26. .text
  27. end;
  28. {****************************************************************************
  29. FPU Control word
  30. ****************************************************************************}
  31. procedure Set8087CW(cw:word);assembler;
  32. asm
  33. movw cw,%ax
  34. {$ifdef FPC_PIC}
  35. movq default8087cw@GOTPCREL(%rip),%rdx
  36. movw %ax,(%rdx)
  37. fnclex
  38. fldcw (%rdx)
  39. {$else FPC_PIC}
  40. movw %ax,default8087cw
  41. fnclex
  42. fldcw default8087cw
  43. {$endif FPC_PIC}
  44. end;
  45. function Get8087CW:word;assembler;
  46. asm
  47. pushq $0
  48. fnstcw (%rsp)
  49. popq %rax
  50. end;
  51. procedure SetSSECSR(w : dword);
  52. begin
  53. mxcsr:=w;
  54. asm
  55. ldmxcsr w
  56. end;
  57. end;
  58. function GetSSECSR : dword;
  59. var
  60. _w : dword;
  61. begin
  62. asm
  63. stmxcsr _w
  64. end;
  65. result:=_w;
  66. end;
  67. {****************************************************************************
  68. EXTENDED data type routines
  69. ****************************************************************************}
  70. {$define FPC_SYSTEM_HAS_PI}
  71. function fpc_pi_real : ValReal;compilerproc;
  72. begin
  73. { Function is handled internal in the compiler }
  74. runerror(207);
  75. result:=0;
  76. end;
  77. {$define FPC_SYSTEM_HAS_ABS}
  78. function fpc_abs_real(d : ValReal) : ValReal;compilerproc;
  79. begin
  80. { Function is handled internal in the compiler }
  81. runerror(207);
  82. result:=0;
  83. end;
  84. {$define FPC_SYSTEM_HAS_SQR}
  85. function fpc_sqr_real(d : ValReal) : ValReal;compilerproc;
  86. begin
  87. { Function is handled internal in the compiler }
  88. runerror(207);
  89. result:=0;
  90. end;
  91. {$define FPC_SYSTEM_HAS_SQRT}
  92. function fpc_sqrt_real(d : ValReal) : ValReal;compilerproc;
  93. begin
  94. { Function is handled internal in the compiler }
  95. runerror(207);
  96. result:=0;
  97. end;
  98. {$define FPC_SYSTEM_HAS_ARCTAN}
  99. function fpc_arctan_real(d : ValReal) : ValReal;compilerproc;
  100. begin
  101. { Function is handled internal in the compiler }
  102. runerror(207);
  103. result:=0;
  104. end;
  105. {$define FPC_SYSTEM_HAS_LN}
  106. function fpc_ln_real(d : ValReal) : ValReal;compilerproc;
  107. begin
  108. { Function is handled internal in the compiler }
  109. runerror(207);
  110. result:=0;
  111. end;
  112. {$define FPC_SYSTEM_HAS_SIN}
  113. function fpc_sin_real(d : ValReal) : ValReal;compilerproc;
  114. begin
  115. { Function is handled internal in the compiler }
  116. runerror(207);
  117. result:=0;
  118. end;
  119. {$define FPC_SYSTEM_HAS_COS}
  120. function fpc_cos_real(d : ValReal) : ValReal;compilerproc;
  121. begin
  122. { Function is handled internal in the compiler }
  123. runerror(207);
  124. result:=0;
  125. end;
  126. {$ifndef WIN64}
  127. {$define FPC_SYSTEM_HAS_EXP}
  128. function fpc_exp_real(d : ValReal) : ValReal;assembler;compilerproc;
  129. asm
  130. subq $16,%rsp
  131. // comes from DJ GPP
  132. fldt d
  133. fldl2e
  134. fmulp %st,%st(1)
  135. fstcw -2(%rbp)
  136. fstcw -4(%rbp)
  137. andw $0xf3ff,-4(%rbp)
  138. orw $0x0400,-4(%rbp)
  139. fldcw -4(%rbp)
  140. fld %st(0)
  141. frndint
  142. fldcw -2(%rbp)
  143. fxch %st(1)
  144. fsub %st(1),%st
  145. f2xm1
  146. fld1
  147. faddp %st,%st(1)
  148. fscale
  149. fstp %st(1)
  150. end;
  151. {$define FPC_SYSTEM_HAS_FRAC}
  152. function fpc_frac_real(d : ValReal) : ValReal;assembler;compilerproc;
  153. asm
  154. subq $16,%rsp
  155. fnstcw -4(%rbp)
  156. fwait
  157. movw -4(%rbp),%cx
  158. orw $0x0c3f,%cx
  159. movw %cx,-8(%rbp)
  160. fldcw -8(%rbp)
  161. fwait
  162. fldt d
  163. frndint
  164. fldt d
  165. fsub %st(1),%st
  166. fstp %st(1)
  167. fnclex
  168. fldcw -4(%rbp)
  169. end;
  170. {$define FPC_SYSTEM_HAS_INT}
  171. function fpc_int_real(d : ValReal) : ValReal;assembler;compilerproc;
  172. asm
  173. subq $16,%rsp
  174. fnstcw -4(%rbp)
  175. fwait
  176. movw -4(%rbp),%cx
  177. orw $0x0c3f,%cx
  178. movw %cx,-8(%rbp)
  179. fldcw -8(%rbp)
  180. fwait
  181. fldt d
  182. frndint
  183. fwait
  184. fldcw -4(%rbp)
  185. end;
  186. {$define FPC_SYSTEM_HAS_TRUNC}
  187. function fpc_trunc_real(d : ValReal) : int64;assembler;compilerproc;
  188. var
  189. oldcw,
  190. newcw : word;
  191. res : int64;
  192. asm
  193. fnstcw oldcw
  194. fwait
  195. movw oldcw,%cx
  196. orw $0x0c3f,%cx
  197. movw %cx,newcw
  198. fldcw newcw
  199. fldt d
  200. fistpq res
  201. fwait
  202. movq res,%rax
  203. fldcw oldcw
  204. end;
  205. {$define FPC_SYSTEM_HAS_ROUND}
  206. function fpc_round_real(d : ValReal) : int64;assembler;compilerproc;
  207. var
  208. res : int64;
  209. asm
  210. fldt d
  211. fistpq res
  212. fwait
  213. movq res,%rax
  214. end;
  215. {$define FPC_SYSTEM_HAS_POWER}
  216. function power(bas,expo : extended) : extended;
  217. begin
  218. if bas=0 then
  219. begin
  220. if expo<>0 then
  221. power:=0.0
  222. else
  223. HandleError(207);
  224. end
  225. else if expo=0 then
  226. power:=1
  227. else
  228. { bas < 0 is not allowed }
  229. if bas<0 then
  230. handleerror(207)
  231. else
  232. power:=exp(ln(bas)*expo);
  233. end;
  234. {$endif WIN64}