math.inc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. Using functions from AMath/DAMath libraries, which are covered by the
  13. following license:
  14. (C) Copyright 2009-2013 Wolfgang Ehrhardt
  15. This software is provided 'as-is', without any express or implied warranty.
  16. In no event will the authors be held liable for any damages arising from
  17. the use of this software.
  18. Permission is granted to anyone to use this software for any purpose,
  19. including commercial applications, and to alter it and redistribute it
  20. freely, subject to the following restrictions:
  21. 1. The origin of this software must not be misrepresented; you must not
  22. claim that you wrote the original software. If you use this software in
  23. a product, an acknowledgment in the product documentation would be
  24. appreciated but is not required.
  25. 2. Altered source versions must be plainly marked as such, and must not be
  26. misrepresented as being the original software.
  27. 3. This notice may not be removed or altered from any source distribution.
  28. ----------------------------------------------------------------------------}
  29. {****************************************************************************
  30. FPU Control word
  31. ****************************************************************************}
  32. {$push}
  33. {$codealign constmin=16}
  34. const
  35. FPC_ABSMASK_SINGLE: array[0..1] of qword=($7fffffff7fffffff,$7fffffff7fffffff); cvar; public;
  36. FPC_ABSMASK_DOUBLE: array[0..1] of qword=($7fffffffffffffff,$7fffffffffffffff); cvar; public;
  37. {$pop}
  38. procedure Set8087CW(cw:word);
  39. begin
  40. { pic-safe ; cw will not be a regvar because it's accessed from }
  41. { assembler }
  42. default8087cw:=cw;
  43. asm
  44. fnclex
  45. fldcw cw
  46. end;
  47. end;
  48. function Get8087CW:word;assembler;
  49. asm
  50. pushl $0
  51. fnstcw (%esp)
  52. popl %eax
  53. end;
  54. procedure SetMXCSR(w : dword);
  55. begin
  56. defaultmxcsr:=w;
  57. asm
  58. ldmxcsr w
  59. end;
  60. end;
  61. function GetMXCSR : dword;
  62. var
  63. _w : dword;
  64. begin
  65. asm
  66. stmxcsr _w
  67. end;
  68. result:=_w;
  69. end;
  70. procedure SetSSECSR(w : dword);
  71. begin
  72. SetMXCSR(w);
  73. end;
  74. function GetSSECSR: dword;
  75. begin
  76. result:=GetMXCSR;
  77. end;
  78. {****************************************************************************
  79. EXTENDED data type routines
  80. ****************************************************************************}
  81. {$define FPC_SYSTEM_HAS_ABS}
  82. function fpc_abs_real(d : ValReal) : ValReal;compilerproc;
  83. begin
  84. { Function is handled internal in the compiler }
  85. runerror(207);
  86. result:=0;
  87. end;
  88. {$define FPC_SYSTEM_HAS_SQR}
  89. function fpc_sqr_real(d : ValReal) : ValReal;compilerproc;
  90. begin
  91. { Function is handled internal in the compiler }
  92. runerror(207);
  93. result:=0;
  94. end;
  95. {$define FPC_SYSTEM_HAS_SQRT}
  96. function fpc_sqrt_real(d : ValReal) : ValReal;compilerproc;
  97. begin
  98. { Function is handled internal in the compiler }
  99. runerror(207);
  100. result:=0;
  101. end;
  102. {$define FPC_SYSTEM_HAS_ARCTAN}
  103. function fpc_arctan_real(d : ValReal) : ValReal;compilerproc;
  104. begin
  105. { Function is handled internal in the compiler }
  106. runerror(207);
  107. result:=0;
  108. end;
  109. {$define FPC_SYSTEM_HAS_LN}
  110. function fpc_ln_real(d : ValReal) : ValReal;compilerproc;
  111. begin
  112. { Function is handled internal in the compiler }
  113. runerror(207);
  114. result:=0;
  115. end;
  116. {$define FPC_SYSTEM_HAS_SIN}
  117. function fpc_sin_real(d : ValReal) : ValReal;compilerproc;
  118. begin
  119. { Function is handled internal in the compiler }
  120. runerror(207);
  121. result:=0;
  122. end;
  123. {$define FPC_SYSTEM_HAS_COS}
  124. function fpc_cos_real(d : ValReal) : ValReal;compilerproc;
  125. begin
  126. { Function is handled internal in the compiler }
  127. runerror(207);
  128. result:=0;
  129. end;
  130. {$define FPC_SYSTEM_HAS_EXP}
  131. { exp function adapted from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt
  132. * translated into AT&T syntax
  133. + PIC support
  134. * return +Inf/0 for +Inf/-Inf input, instead of NaN }
  135. function fpc_exp_real(d : ValReal) : ValReal;assembler;compilerproc;
  136. const
  137. ln2hi: double=6.9314718036912382E-001;
  138. ln2lo: double=1.9082149292705877E-010;
  139. large: single=24576.0;
  140. two: single=2.0;
  141. half: single=0.5;
  142. asm
  143. call .LPIC
  144. .LPIC:
  145. pop %ecx
  146. fldt d
  147. fldl2e
  148. fmul %st(1),%st { z = d * log2(e) }
  149. frndint
  150. { Calculate frac(z) using modular arithmetic to avoid precision loss. }
  151. fldl ln2hi-.LPIC(%ecx)
  152. fmul %st(1),%st
  153. fsubrp %st,%st(2)
  154. fldl ln2lo-.LPIC(%ecx)
  155. fmul %st(1),%st
  156. fsubrp %st,%st(2)
  157. fxch %st(1) { (d-int(z)*ln2_hi)-int(z)*ln2_lo }
  158. fldl2e
  159. fmulp %st,%st(1) { frac(z) }
  160. { The above code can result in |frac(z)|>1, particularly when rounding mode
  161. is not "round to nearest". f2xm1 is undefined in this case, so a check
  162. is necessary. Furthermore, frac(z) evaluates to NaN for d=+-Inf. }
  163. fld %st
  164. fabs
  165. fld1
  166. fcompp
  167. fstsw %ax
  168. sahf
  169. jp .L3 { NaN }
  170. jae .L1 { frac(z) <= 1 }
  171. fld %st(1)
  172. fabs
  173. fcomps large-.LPIC(%ecx)
  174. fstsw %ax
  175. sahf
  176. jb .L0 { int(z) < 24576 }
  177. .L3:
  178. fstp %st { zero out frac(z), hard way because }
  179. fldz { "fsub %st,%st" does not work for NaN }
  180. jmp .L1
  181. .L0:
  182. { Calculate 2**frac(z)-1 as N*(N+2), where N=2**(frac(z)/2)-1 }
  183. fmuls half-.LPIC(%ecx)
  184. f2xm1
  185. fld %st
  186. fadds two-.LPIC(%ecx)
  187. fmulp %st,%st(1)
  188. jmp .L2
  189. .L1:
  190. f2xm1
  191. .L2:
  192. fld1
  193. faddp %st,%st(1)
  194. fscale
  195. fstp %st(1)
  196. end;
  197. {$define FPC_SYSTEM_HAS_FRAC}
  198. function fpc_frac_real(d : ValReal) : ValReal;assembler;compilerproc;
  199. asm
  200. subl $4,%esp
  201. fnstcw (%esp)
  202. fwait
  203. movw (%esp),%cx
  204. orw $0x0f00,(%esp)
  205. fldcw (%esp)
  206. fldt d
  207. frndint
  208. fldt d
  209. fsub %st(1),%st
  210. fstp %st(1)
  211. movw %cx,(%esp)
  212. fldcw (%esp)
  213. end;
  214. {$define FPC_SYSTEM_HAS_INT}
  215. function fpc_int_real(d : ValReal) : ValReal;assembler;compilerproc;
  216. asm
  217. subl $4,%esp
  218. fnstcw (%esp)
  219. fwait
  220. movw (%esp),%cx
  221. orw $0x0f00,(%esp)
  222. fldcw (%esp)
  223. fwait
  224. fldt d
  225. frndint
  226. fwait
  227. movw %cx,(%esp)
  228. fldcw (%esp)
  229. end;
  230. {$define FPC_SYSTEM_HAS_TRUNC}
  231. function fpc_trunc_real(d : ValReal) : int64;assembler;compilerproc;
  232. asm
  233. subl $12,%esp
  234. fldt d
  235. fnstcw (%esp)
  236. movw (%esp),%cx
  237. orw $0x0f00,(%esp)
  238. fldcw (%esp)
  239. movw %cx,(%esp)
  240. fistpq 4(%esp)
  241. fldcw (%esp)
  242. fwait
  243. movl 4(%esp),%eax
  244. movl 8(%esp),%edx
  245. end;
  246. {$define FPC_SYSTEM_HAS_ROUND}
  247. { keep for bootstrapping with 2.0.x }
  248. function fpc_round_real(d : ValReal) : int64;compilerproc;assembler;
  249. var
  250. res : int64;
  251. asm
  252. fldt d
  253. fistpq res
  254. fwait
  255. movl res,%eax
  256. movl res+4,%edx
  257. end;