math.inc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. {-------------------------------------------------------------------------
  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. {$push}
  30. {$codealign constmin=16}
  31. const
  32. FPC_ABSMASK_SINGLE: array[0..1] of qword=($7fffffff7fffffff,$7fffffff7fffffff); cvar; public;
  33. FPC_ABSMASK_DOUBLE: array[0..1] of qword=($7fffffffffffffff,$7fffffffffffffff); cvar; public;
  34. {$pop}
  35. {****************************************************************************
  36. FPU Control word
  37. ****************************************************************************}
  38. procedure Set8087CW(cw:word);
  39. begin
  40. default8087cw:=cw;
  41. asm
  42. fnclex
  43. fldcw cw
  44. end;
  45. end;
  46. function Get8087CW:word;assembler;
  47. var
  48. tmp: word;
  49. asm
  50. fnstcw tmp
  51. movw tmp,%ax
  52. andl $0xffff,%eax { clears bits 32-63 }
  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;assembler;
  62. var
  63. _w : dword;
  64. asm
  65. stmxcsr _w
  66. movl _w,%eax
  67. end;
  68. procedure SetSSECSR(w : dword);
  69. begin
  70. SetMXCSR(w);
  71. end;
  72. function GetSSECSR: dword;
  73. begin
  74. result:=GetMXCSR;
  75. end;
  76. {****************************************************************************
  77. EXTENDED data type routines
  78. ****************************************************************************}
  79. {$ifndef FPC_SYSTEM_HAS_ABS}
  80. {$define FPC_SYSTEM_HAS_ABS}
  81. function fpc_abs_real(d : ValReal) : ValReal;compilerproc;
  82. begin
  83. { Function is handled internal in the compiler }
  84. runerror(207);
  85. result:=0;
  86. end;
  87. {$endif FPC_SYSTEM_HAS_ABS}
  88. {$ifndef FPC_SYSTEM_HAS_SQR}
  89. {$define FPC_SYSTEM_HAS_SQR}
  90. function fpc_sqr_real(d : ValReal) : ValReal;compilerproc;
  91. begin
  92. { Function is handled internal in the compiler }
  93. runerror(207);
  94. result:=0;
  95. end;
  96. {$endif FPC_SYSTEM_HAS_SQR}
  97. {$ifndef FPC_SYSTEM_HAS_SQRT}
  98. {$define FPC_SYSTEM_HAS_SQRT}
  99. function fpc_sqrt_real(d : ValReal) : ValReal;compilerproc;
  100. begin
  101. { Function is handled internal in the compiler }
  102. runerror(207);
  103. result:=0;
  104. end;
  105. {$endif FPC_SYSTEM_HAS_SQRT}
  106. {$ifdef FPC_HAS_TYPE_EXTENDED}
  107. {$ifndef FPC_SYSTEM_HAS_ARCTAN}
  108. {$define FPC_SYSTEM_HAS_ARCTAN}
  109. function fpc_arctan_real(d : ValReal) : ValReal;compilerproc;
  110. begin
  111. { Function is handled internal in the compiler }
  112. runerror(207);
  113. result:=0;
  114. end;
  115. {$endif FPC_SYSTEM_HAS_ARCTAN}
  116. {$ifndef FPC_SYSTEM_HAS_LN}
  117. {$define FPC_SYSTEM_HAS_LN}
  118. function fpc_ln_real(d : ValReal) : ValReal;compilerproc;
  119. begin
  120. { Function is handled internal in the compiler }
  121. runerror(207);
  122. result:=0;
  123. end;
  124. {$endif FPC_SYSTEM_HAS_LN}
  125. {$ifndef FPC_SYSTEM_HAS_SIN}
  126. {$define FPC_SYSTEM_HAS_SIN}
  127. function fpc_sin_real(d : ValReal) : ValReal;compilerproc;
  128. begin
  129. { Function is handled internal in the compiler }
  130. runerror(207);
  131. result:=0;
  132. end;
  133. {$endif FPC_SYSTEM_HAS_SIN}
  134. {$ifndef FPC_SYSTEM_HAS_COS}
  135. {$define FPC_SYSTEM_HAS_COS}
  136. function fpc_cos_real(d : ValReal) : ValReal;compilerproc;
  137. begin
  138. { Function is handled internal in the compiler }
  139. runerror(207);
  140. result:=0;
  141. end;
  142. {$endif FPC_SYSTEM_HAS_COS}
  143. {$ifndef FPC_SYSTEM_HAS_EXP}
  144. {$define FPC_SYSTEM_HAS_EXP}
  145. { exp function adapted from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt
  146. * translated into AT&T syntax
  147. + PIC support
  148. * return +Inf/0 for +Inf/-Inf input, instead of NaN }
  149. function fpc_exp_real(d : ValReal) : ValReal;assembler;compilerproc;
  150. const
  151. ln2hi: double=6.9314718036912382E-001;
  152. ln2lo: double=1.9082149292705877E-010;
  153. large: single=24576.0;
  154. two: single=2.0;
  155. half: single=0.5;
  156. asm
  157. fldt d
  158. fldl2e
  159. fmul %st(1),%st { z = d * log2(e) }
  160. frndint
  161. { Calculate frac(z) using modular arithmetic to avoid precision loss }
  162. fldl ln2hi(%rip)
  163. fmul %st(1),%st
  164. fsubrp %st,%st(2)
  165. fldl ln2lo(%rip)
  166. fmul %st(1),%st
  167. fsubrp %st,%st(2)
  168. fxch %st(1) { (d-int(z)*ln2_hi)-int(z)*ln2_lo }
  169. fldl2e
  170. fmulp %st,%st(1) { frac(z) }
  171. { Above calculation can yield |frac(z)|>1, particularly when rounding mode
  172. is not "round to nearest". f2xm1 is undefined in that case, so it's
  173. necessary to check }
  174. fld %st
  175. fabs
  176. fld1
  177. fcomip
  178. fstp %st
  179. jp .L3 { NaN }
  180. jae .L1 { |frac(z)| <= 1, good }
  181. fld %st(1)
  182. fabs
  183. flds large(%rip)
  184. fcomip
  185. fstp %st
  186. jb .L3 { int(z) >= 24576 }
  187. .L0:
  188. { Calculate 2**frac(z)-1 as N*(N+2), where N=2**(frac(z)/2)-1 }
  189. fmuls half(%rip)
  190. f2xm1
  191. fld %st
  192. fadds two(%rip)
  193. fmulp %st,%st(1)
  194. jmp .L2
  195. .L3:
  196. fstp %st { pop frac(z) and load 0 }
  197. fldz
  198. .L1:
  199. f2xm1
  200. .L2:
  201. fld1
  202. faddp %st,%st(1)
  203. fscale
  204. fstp %st(1)
  205. end;
  206. {$endif FPC_SYSTEM_HAS_EXP}
  207. {$ifndef FPC_SYSTEM_HAS_FRAC}
  208. {$define FPC_SYSTEM_HAS_FRAC}
  209. function fpc_frac_real(d : ValReal) : ValReal;assembler;compilerproc;
  210. var
  211. oldcw,newcw: word;
  212. asm
  213. fnstcw oldcw
  214. fwait
  215. movw oldcw,%cx
  216. orw $0x0c3f,%cx
  217. movw %cx,newcw
  218. fldcw newcw
  219. fwait
  220. fldt d
  221. frndint
  222. fldt d
  223. fsub %st(1),%st
  224. fstp %st(1)
  225. fnclex
  226. fldcw oldcw
  227. end;
  228. {$endif FPC_SYSTEM_HAS_FRAC}
  229. {$ifndef FPC_SYSTEM_HAS_INT}
  230. {$define FPC_SYSTEM_HAS_INT}
  231. function fpc_int_real(d : ValReal) : ValReal;assembler;compilerproc;
  232. var
  233. oldcw,newcw: word;
  234. asm
  235. fnstcw oldcw
  236. fwait
  237. movw oldcw,%cx
  238. orw $0x0c3f,%cx
  239. movw %cx,newcw
  240. fldcw newcw
  241. fwait
  242. fldt d
  243. frndint
  244. fwait
  245. fldcw oldcw
  246. end;
  247. {$endif FPC_SYSTEM_HAS_INT}
  248. {$ifndef FPC_SYSTEM_HAS_TRUNC}
  249. {$define FPC_SYSTEM_HAS_TRUNC}
  250. function fpc_trunc_real(d : ValReal) : int64;assembler;compilerproc;
  251. var
  252. oldcw,
  253. newcw : word;
  254. res : int64;
  255. asm
  256. fnstcw oldcw
  257. fwait
  258. movw oldcw,%cx
  259. orw $0x0c3f,%cx
  260. movw %cx,newcw
  261. fldcw newcw
  262. fldt d
  263. fistpq res
  264. fwait
  265. movq res,%rax
  266. fldcw oldcw
  267. end;
  268. {$endif FPC_SYSTEM_HAS_TRUNC}
  269. {$ifndef FPC_SYSTEM_HAS_ROUND}
  270. {$define FPC_SYSTEM_HAS_ROUND}
  271. function fpc_round_real(d : ValReal) : int64;assembler;compilerproc;
  272. var
  273. res : int64;
  274. asm
  275. fldt d
  276. fistpq res
  277. fwait
  278. movq res,%rax
  279. end;
  280. {$endif FPC_SYSTEM_HAS_ROUND}
  281. {$else FPC_HAS_TYPE_EXTENDED}
  282. {$define FPC_SYSTEM_HAS_TRUNC}
  283. function fpc_trunc_real(d : ValReal) : int64;compilerproc; assembler; nostackframe;
  284. asm
  285. cvttsd2si %xmm0,%rax;
  286. end;
  287. {$define FPC_SYSTEM_HAS_ROUND}
  288. function fpc_round_real(d : ValReal) : int64;compilerproc; assembler; nostackframe;
  289. asm
  290. cvtsd2si %xmm0,%rax;
  291. end;
  292. {$endif FPC_HAS_TYPE_EXTENDED}