math.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. {$ifndef WIN64}
  32. procedure Set8087CW(cw:word);assembler;
  33. asm
  34. movw cw,%ax
  35. {$ifdef FPC_PIC}
  36. movq default8087cw@GOTPCREL(%rip),%rax
  37. movw %ax,(%rax)
  38. fnclex
  39. fldcw (%rax)
  40. {$else FPC_PIC}
  41. movw %ax,default8087cw
  42. fnclex
  43. fldcw default8087cw
  44. {$endif FPC_PIC}
  45. end;
  46. function Get8087CW:word;assembler;
  47. asm
  48. pushq $0
  49. fnstcw (%rsp)
  50. popq %rax
  51. end;
  52. {$endif WIN64}
  53. {****************************************************************************
  54. EXTENDED data type routines
  55. ****************************************************************************}
  56. {$define FPC_SYSTEM_HAS_PI}
  57. function fpc_pi_real : ValReal;compilerproc;
  58. begin
  59. { Function is handled internal in the compiler }
  60. runerror(207);
  61. result:=0;
  62. end;
  63. {$define FPC_SYSTEM_HAS_ABS}
  64. function fpc_abs_real(d : ValReal) : ValReal;compilerproc;
  65. begin
  66. { Function is handled internal in the compiler }
  67. runerror(207);
  68. result:=0;
  69. end;
  70. {$define FPC_SYSTEM_HAS_SQR}
  71. function fpc_sqr_real(d : ValReal) : 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_SQRT}
  78. function fpc_sqrt_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_ARCTAN}
  85. function fpc_arctan_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_LN}
  92. function fpc_ln_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_SIN}
  99. function fpc_sin_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_COS}
  106. function fpc_cos_real(d : ValReal) : ValReal;compilerproc;
  107. begin
  108. { Function is handled internal in the compiler }
  109. runerror(207);
  110. result:=0;
  111. end;
  112. {$ifndef WIN64}
  113. {$define FPC_SYSTEM_HAS_EXP}
  114. function fpc_exp_real(d : ValReal) : ValReal;assembler;compilerproc;
  115. asm
  116. subq $16,%rsp
  117. // comes from DJ GPP
  118. fldt d
  119. fldl2e
  120. fmulp %st,%st(1)
  121. fstcw -2(%rbp)
  122. fstcw -4(%rbp)
  123. andw $0xf3ff,-4(%rbp)
  124. orw $0x0400,-4(%rbp)
  125. fldcw -4(%rbp)
  126. fld %st(0)
  127. frndint
  128. fldcw -2(%rbp)
  129. fxch %st(1)
  130. fsub %st(1),%st
  131. f2xm1
  132. fld1
  133. faddp %st,%st(1)
  134. fscale
  135. fstp %st(1)
  136. end;
  137. {$define FPC_SYSTEM_HAS_FRAC}
  138. function fpc_frac_real(d : ValReal) : ValReal;assembler;compilerproc;
  139. asm
  140. subq $16,%rsp
  141. fnstcw -4(%rbp)
  142. fwait
  143. movw -4(%rbp),%cx
  144. orw $0x0c3f,%cx
  145. movw %cx,-8(%rbp)
  146. fldcw -8(%rbp)
  147. fwait
  148. fldt d
  149. frndint
  150. fldt d
  151. fsub %st(1),%st
  152. fstp %st(1)
  153. fnclex
  154. fldcw -4(%rbp)
  155. end;
  156. {$define FPC_SYSTEM_HAS_INT}
  157. function fpc_int_real(d : ValReal) : ValReal;assembler;compilerproc;
  158. asm
  159. subq $16,%rsp
  160. fnstcw -4(%rbp)
  161. fwait
  162. movw -4(%rbp),%cx
  163. orw $0x0c3f,%cx
  164. movw %cx,-8(%rbp)
  165. fldcw -8(%rbp)
  166. fwait
  167. fldt d
  168. frndint
  169. fwait
  170. fldcw -4(%rbp)
  171. end;
  172. {$define FPC_SYSTEM_HAS_TRUNC}
  173. function fpc_trunc_real(d : ValReal) : int64;assembler;compilerproc;
  174. var
  175. oldcw,
  176. newcw : word;
  177. res : int64;
  178. asm
  179. fnstcw oldcw
  180. fwait
  181. movw oldcw,%cx
  182. orw $0x0c3f,%cx
  183. movw %cx,newcw
  184. fldcw newcw
  185. fldt d
  186. fistpq res
  187. fwait
  188. movq res,%rax
  189. fldcw oldcw
  190. end;
  191. {$define FPC_SYSTEM_HAS_ROUND}
  192. function fpc_round_real(d : ValReal) : int64;assembler;compilerproc;
  193. var
  194. res : int64;
  195. asm
  196. fldt d
  197. fistpq res
  198. fwait
  199. movq res,%rax
  200. end;
  201. {$define FPC_SYSTEM_HAS_POWER}
  202. function power(bas,expo : extended) : extended;
  203. begin
  204. if bas=0 then
  205. begin
  206. if expo<>0 then
  207. power:=0.0
  208. else
  209. HandleError(207);
  210. end
  211. else if expo=0 then
  212. power:=1
  213. else
  214. { bas < 0 is not allowed }
  215. if bas<0 then
  216. handleerror(207)
  217. else
  218. power:=exp(ln(bas)*expo);
  219. end;
  220. {$endif WIN64}