math.inc 6.0 KB

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