math.inc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2001 by the Free Pascal development team
  4. Implementation of mathematical routines (for extended type)
  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. {****************************************************************************
  12. FPU Control word
  13. ****************************************************************************}
  14. procedure Set8087CW(cw:word);
  15. begin
  16. { pic-safe ; cw will not be a regvar because it's accessed from }
  17. { assembler }
  18. default8087cw:=cw;
  19. asm
  20. fnclex
  21. fldcw cw
  22. end;
  23. end;
  24. function Get8087CW:word;assembler;
  25. asm
  26. pushl $0
  27. fnstcw (%esp)
  28. popl %eax
  29. end;
  30. procedure SetSSECSR(w : dword);
  31. begin
  32. mxcsr:=w;
  33. asm
  34. ldmxcsr w
  35. end;
  36. end;
  37. function GetSSECSR : dword;
  38. var
  39. _w : dword;
  40. begin
  41. asm
  42. stmxcsr _w
  43. end;
  44. result:=_w;
  45. end;
  46. {****************************************************************************
  47. EXTENDED data type routines
  48. ****************************************************************************}
  49. {$define FPC_SYSTEM_HAS_PI}
  50. function fpc_pi_real : ValReal;compilerproc;
  51. begin
  52. { Function is handled internal in the compiler }
  53. runerror(207);
  54. result:=0;
  55. end;
  56. {$define FPC_SYSTEM_HAS_ABS}
  57. function fpc_abs_real(d : ValReal) : 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_SQR}
  64. function fpc_sqr_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_SQRT}
  71. function fpc_sqrt_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_ARCTAN}
  78. function fpc_arctan_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_LN}
  85. function fpc_ln_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_SIN}
  92. function fpc_sin_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_COS}
  99. function fpc_cos_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_EXP}
  106. function fpc_exp_real(d : ValReal) : ValReal;assembler;compilerproc;
  107. asm
  108. fldt d
  109. fldl2e
  110. fmul %st(1),%st { z = d * log2(e) }
  111. frndint
  112. { Calculate frac(z) using modular arithmetic to avoid precision loss.
  113. Avoid PIC hacks by using immediate operands (it's not the fastest,
  114. but likely the cleanest solution). }
  115. pushl $0x3fe62e42 { ln(2).hi=6.9314718036912382E-001 }
  116. pushl $0xfee00000
  117. fldl (%esp)
  118. fmul %st(1),%st
  119. fsubrp %st,%st(2)
  120. pushl $0x3dea39ef { ln(2).lo=1.9082149292705877E-010 }
  121. pushl $0x35793c76
  122. fldl (%esp)
  123. fmul %st(1),%st
  124. fsubrp %st,%st(2)
  125. fxch %st(1) { (d-int(z)*ln2_hi)-int(z)*ln2_lo }
  126. fldl2e
  127. fmulp %st,%st(1) { frac(z) }
  128. fld %st
  129. fabs
  130. fld1
  131. fcompp
  132. fstsw %ax
  133. sahf
  134. jae .L1 { frac(z) <= 1 }
  135. fld %st(1)
  136. fabs
  137. pushl $0x46c00000 { single(24576.0) }
  138. fcomps (%esp)
  139. fstsw %ax
  140. sahf
  141. jb .L0 { int(z) < 24576 }
  142. fsub %st,%st
  143. jmp .L1
  144. .L0:
  145. { Calculate 2**frac(z)-1 as N*(N+2), where N=2**(frac(z)/2)-1 }
  146. pushl $0x3f000000 { single(0.5) }
  147. fmuls (%esp)
  148. f2xm1
  149. fld %st
  150. pushl $0x40000000 { single(2.0) }
  151. fadds (%esp)
  152. fmulp %st,%st(1)
  153. jmp .L2
  154. .L1:
  155. f2xm1
  156. .L2:
  157. fld1
  158. faddp %st,%st(1)
  159. fscale
  160. fstp %st(1)
  161. end;
  162. {$define FPC_SYSTEM_HAS_FRAC}
  163. function fpc_frac_real(d : ValReal) : ValReal;assembler;compilerproc;
  164. asm
  165. subl $4,%esp
  166. fnstcw (%esp)
  167. fwait
  168. movw (%esp),%cx
  169. orw $0x0f00,(%esp)
  170. fldcw (%esp)
  171. fldt d
  172. frndint
  173. fldt d
  174. fsub %st(1),%st
  175. fstp %st(1)
  176. movw %cx,(%esp)
  177. fldcw (%esp)
  178. end;
  179. {$define FPC_SYSTEM_HAS_INT}
  180. function fpc_int_real(d : ValReal) : ValReal;assembler;compilerproc;
  181. asm
  182. subl $4,%esp
  183. fnstcw (%esp)
  184. fwait
  185. movw (%esp),%cx
  186. orw $0x0f00,(%esp)
  187. fldcw (%esp)
  188. fwait
  189. fldt d
  190. frndint
  191. fwait
  192. movw %cx,(%esp)
  193. fldcw (%esp)
  194. end;
  195. {$define FPC_SYSTEM_HAS_TRUNC}
  196. function fpc_trunc_real(d : ValReal) : int64;assembler;compilerproc;
  197. asm
  198. subl $12,%esp
  199. fldt d
  200. fnstcw (%esp)
  201. movw (%esp),%cx
  202. orw $0x0f00,(%esp)
  203. fldcw (%esp)
  204. movw %cx,(%esp)
  205. fistpq 4(%esp)
  206. fldcw (%esp)
  207. fwait
  208. movl 4(%esp),%eax
  209. movl 8(%esp),%edx
  210. end;
  211. {$define FPC_SYSTEM_HAS_ROUND}
  212. { keep for bootstrapping with 2.0.x }
  213. function fpc_round_real(d : ValReal) : int64;compilerproc;assembler;
  214. var
  215. res : int64;
  216. asm
  217. fldt d
  218. fistpq res
  219. fwait
  220. movl res,%eax
  221. movl res+4,%edx
  222. end;