math.inc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. var
  53. _w : dword;
  54. begin
  55. _w:=w;
  56. asm
  57. ldmxcsr _w
  58. end;
  59. end;
  60. function GetSSECSR : dword;
  61. var
  62. _w : dword;
  63. begin
  64. asm
  65. stmxcsr _w
  66. end;
  67. result:=_w;
  68. end;
  69. {****************************************************************************
  70. EXTENDED data type routines
  71. ****************************************************************************}
  72. {$define FPC_SYSTEM_HAS_PI}
  73. function fpc_pi_real : ValReal;compilerproc;
  74. begin
  75. { Function is handled internal in the compiler }
  76. runerror(207);
  77. result:=0;
  78. end;
  79. {$define FPC_SYSTEM_HAS_ABS}
  80. function fpc_abs_real(d : ValReal) : ValReal;compilerproc;
  81. begin
  82. { Function is handled internal in the compiler }
  83. runerror(207);
  84. result:=0;
  85. end;
  86. {$define FPC_SYSTEM_HAS_SQR}
  87. function fpc_sqr_real(d : ValReal) : ValReal;compilerproc;
  88. begin
  89. { Function is handled internal in the compiler }
  90. runerror(207);
  91. result:=0;
  92. end;
  93. {$define FPC_SYSTEM_HAS_SQRT}
  94. function fpc_sqrt_real(d : ValReal) : ValReal;compilerproc;
  95. begin
  96. { Function is handled internal in the compiler }
  97. runerror(207);
  98. result:=0;
  99. end;
  100. {$define FPC_SYSTEM_HAS_ARCTAN}
  101. function fpc_arctan_real(d : ValReal) : ValReal;compilerproc;
  102. begin
  103. { Function is handled internal in the compiler }
  104. runerror(207);
  105. result:=0;
  106. end;
  107. {$define FPC_SYSTEM_HAS_LN}
  108. function fpc_ln_real(d : ValReal) : ValReal;compilerproc;
  109. begin
  110. { Function is handled internal in the compiler }
  111. runerror(207);
  112. result:=0;
  113. end;
  114. {$define FPC_SYSTEM_HAS_SIN}
  115. function fpc_sin_real(d : ValReal) : ValReal;compilerproc;
  116. begin
  117. { Function is handled internal in the compiler }
  118. runerror(207);
  119. result:=0;
  120. end;
  121. {$define FPC_SYSTEM_HAS_COS}
  122. function fpc_cos_real(d : ValReal) : ValReal;compilerproc;
  123. begin
  124. { Function is handled internal in the compiler }
  125. runerror(207);
  126. result:=0;
  127. end;
  128. {$ifndef WIN64}
  129. {$define FPC_SYSTEM_HAS_EXP}
  130. function fpc_exp_real(d : ValReal) : ValReal;assembler;compilerproc;
  131. asm
  132. subq $16,%rsp
  133. // comes from DJ GPP
  134. fldt d
  135. fldl2e
  136. fmulp %st,%st(1)
  137. fstcw -2(%rbp)
  138. fstcw -4(%rbp)
  139. andw $0xf3ff,-4(%rbp)
  140. orw $0x0400,-4(%rbp)
  141. fldcw -4(%rbp)
  142. fld %st(0)
  143. frndint
  144. fldcw -2(%rbp)
  145. fxch %st(1)
  146. fsub %st(1),%st
  147. f2xm1
  148. fld1
  149. faddp %st,%st(1)
  150. fscale
  151. fstp %st(1)
  152. end;
  153. {$define FPC_SYSTEM_HAS_FRAC}
  154. function fpc_frac_real(d : ValReal) : ValReal;assembler;compilerproc;
  155. asm
  156. subq $16,%rsp
  157. fnstcw -4(%rbp)
  158. fwait
  159. movw -4(%rbp),%cx
  160. orw $0x0c3f,%cx
  161. movw %cx,-8(%rbp)
  162. fldcw -8(%rbp)
  163. fwait
  164. fldt d
  165. frndint
  166. fldt d
  167. fsub %st(1),%st
  168. fstp %st(1)
  169. fnclex
  170. fldcw -4(%rbp)
  171. end;
  172. {$define FPC_SYSTEM_HAS_INT}
  173. function fpc_int_real(d : ValReal) : ValReal;assembler;compilerproc;
  174. asm
  175. subq $16,%rsp
  176. fnstcw -4(%rbp)
  177. fwait
  178. movw -4(%rbp),%cx
  179. orw $0x0c3f,%cx
  180. movw %cx,-8(%rbp)
  181. fldcw -8(%rbp)
  182. fwait
  183. fldt d
  184. frndint
  185. fwait
  186. fldcw -4(%rbp)
  187. end;
  188. {$define FPC_SYSTEM_HAS_TRUNC}
  189. function fpc_trunc_real(d : ValReal) : int64;assembler;compilerproc;
  190. var
  191. oldcw,
  192. newcw : word;
  193. res : int64;
  194. asm
  195. fnstcw oldcw
  196. fwait
  197. movw oldcw,%cx
  198. orw $0x0c3f,%cx
  199. movw %cx,newcw
  200. fldcw newcw
  201. fldt d
  202. fistpq res
  203. fwait
  204. movq res,%rax
  205. fldcw oldcw
  206. end;
  207. {$define FPC_SYSTEM_HAS_ROUND}
  208. function fpc_round_real(d : ValReal) : int64;assembler;compilerproc;
  209. var
  210. res : int64;
  211. asm
  212. fldt d
  213. fistpq res
  214. fwait
  215. movq res,%rax
  216. end;
  217. {$define FPC_SYSTEM_HAS_POWER}
  218. function power(bas,expo : extended) : extended;
  219. begin
  220. if bas=0 then
  221. begin
  222. if expo<>0 then
  223. power:=0.0
  224. else
  225. HandleError(207);
  226. end
  227. else if expo=0 then
  228. power:=1
  229. else
  230. { bas < 0 is not allowed }
  231. if bas<0 then
  232. handleerror(207)
  233. else
  234. power:=exp(ln(bas)*expo);
  235. end;
  236. {$endif WIN64}