math.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. {$ifdef FPUX86_HAS_AVXUNIT}
  59. vldmxcsr w
  60. {$else FPUX86_HAS_AVXUNIT}
  61. ldmxcsr w
  62. {$endif FPUX86_HAS_AVXUNIT}
  63. end;
  64. end;
  65. function GetMXCSR : dword;assembler;
  66. var
  67. _w : dword;
  68. asm
  69. {$ifdef FPUX86_HAS_AVXUNIT}
  70. vstmxcsr _w
  71. {$else FPUX86_HAS_AVXUNIT}
  72. stmxcsr _w
  73. {$endif FPUX86_HAS_AVXUNIT}
  74. movl _w,%eax
  75. end;
  76. procedure SetSSECSR(w : dword);
  77. begin
  78. SetMXCSR(w);
  79. end;
  80. function GetSSECSR: dword;
  81. begin
  82. result:=GetMXCSR;
  83. end;
  84. {****************************************************************************
  85. EXTENDED data type routines
  86. ****************************************************************************}
  87. {$ifndef FPC_SYSTEM_HAS_ABS}
  88. {$define FPC_SYSTEM_HAS_ABS}
  89. function fpc_abs_real(d : ValReal) : ValReal;compilerproc;
  90. {$ifndef cpullvm}
  91. begin
  92. { Function is handled internal in the compiler }
  93. runerror(207);
  94. result:=0;
  95. {$else not cpullvm}
  96. assembler;
  97. asm
  98. fldt d
  99. fabs
  100. {$endif not cpullvm}
  101. end;
  102. {$endif FPC_SYSTEM_HAS_ABS}
  103. {$ifndef FPC_SYSTEM_HAS_SQR}
  104. {$define FPC_SYSTEM_HAS_SQR}
  105. function fpc_sqr_real(d : ValReal) : ValReal;compilerproc;
  106. {$ifndef cpullvm}
  107. begin
  108. { Function is handled internal in the compiler }
  109. runerror(207);
  110. result:=0;
  111. {$else not cpullvm}
  112. begin
  113. fpc_sqr_real:=d*d;
  114. {$endif not cpullvm}
  115. end;
  116. {$endif FPC_SYSTEM_HAS_SQR}
  117. {$ifndef FPC_SYSTEM_HAS_SQRT}
  118. {$define FPC_SYSTEM_HAS_SQRT}
  119. function fpc_sqrt_real(d : ValReal) : ValReal;compilerproc;
  120. {$ifndef cpullvm}
  121. begin
  122. { Function is handled internal in the compiler }
  123. runerror(207);
  124. result:=0;
  125. {$else not cpullvm}
  126. assembler;
  127. asm
  128. fldt d
  129. fsqrt
  130. {$endif not cpullvm}
  131. end;
  132. {$endif FPC_SYSTEM_HAS_SQRT}
  133. {$ifdef FPC_HAS_TYPE_EXTENDED}
  134. {$ifndef FPC_SYSTEM_HAS_ARCTAN}
  135. {$define FPC_SYSTEM_HAS_ARCTAN}
  136. function fpc_arctan_real(d : ValReal) : ValReal;compilerproc;
  137. {$ifndef cpullvm}
  138. begin
  139. { Function is handled internal in the compiler }
  140. runerror(207);
  141. result:=0;
  142. {$else not cpullvm}
  143. assembler;
  144. asm
  145. fldt d
  146. fld1
  147. fpatan
  148. {$endif not cpullvm}
  149. end;
  150. {$endif FPC_SYSTEM_HAS_ARCTAN}
  151. {$ifndef FPC_SYSTEM_HAS_LN}
  152. {$define FPC_SYSTEM_HAS_LN}
  153. function fpc_ln_real(d : ValReal) : ValReal;compilerproc;
  154. {$ifndef cpullvm}
  155. begin
  156. { Function is handled internal in the compiler }
  157. runerror(207);
  158. result:=0;
  159. {$else not cpullvm}
  160. assembler;
  161. asm
  162. fldln2
  163. fldt d
  164. fyl2x
  165. {$endif not cpullvm}
  166. end;
  167. {$endif FPC_SYSTEM_HAS_LN}
  168. {$ifndef FPC_SYSTEM_HAS_SIN}
  169. {$define FPC_SYSTEM_HAS_SIN}
  170. function fpc_sin_real(d : ValReal) : ValReal;compilerproc;
  171. {$ifndef cpullvm}
  172. begin
  173. { Function is handled internal in the compiler }
  174. runerror(207);
  175. result:=0;
  176. {$else not cpullvm}
  177. assembler;
  178. asm
  179. fldt d
  180. fsin
  181. {$endif not cpullvm}
  182. end;
  183. {$endif FPC_SYSTEM_HAS_SIN}
  184. {$ifndef FPC_SYSTEM_HAS_COS}
  185. {$define FPC_SYSTEM_HAS_COS}
  186. function fpc_cos_real(d : ValReal) : ValReal;compilerproc;
  187. {$ifndef cpullvm}
  188. begin
  189. { Function is handled internal in the compiler }
  190. runerror(207);
  191. result:=0;
  192. {$else not cpullvm}
  193. assembler;
  194. asm
  195. fldt d
  196. fcos
  197. {$endif not cpullvm}
  198. end;
  199. {$endif FPC_SYSTEM_HAS_COS}
  200. {$ifndef FPC_SYSTEM_HAS_EXP}
  201. {$define FPC_SYSTEM_HAS_EXP}
  202. { exp function adapted from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt
  203. * translated into AT&T syntax
  204. + PIC support
  205. * return +Inf/0 for +Inf/-Inf input, instead of NaN }
  206. function fpc_exp_real(d : ValReal) : ValReal;assembler;compilerproc;
  207. const
  208. ln2hi: double=6.9314718036912382E-001;
  209. ln2lo: double=1.9082149292705877E-010;
  210. large: single=24576.0;
  211. two: single=2.0;
  212. half: single=0.5;
  213. asm
  214. fldt d
  215. fldl2e
  216. fmul %st(1),%st { z = d * log2(e) }
  217. frndint
  218. { Calculate frac(z) using modular arithmetic to avoid precision loss }
  219. fldl ln2hi(%rip)
  220. fmul %st(1),%st
  221. fsubrp %st,%st(2)
  222. fldl ln2lo(%rip)
  223. fmul %st(1),%st
  224. fsubrp %st,%st(2)
  225. fxch %st(1) { (d-int(z)*ln2_hi)-int(z)*ln2_lo }
  226. fldl2e
  227. fmulp %st,%st(1) { frac(z) }
  228. { Above calculation can yield |frac(z)|>1, particularly when rounding mode
  229. is not "round to nearest". f2xm1 is undefined in that case, so it's
  230. necessary to check }
  231. fld %st
  232. fabs
  233. fld1
  234. fcomip %st(1),%st(0)
  235. fstp %st
  236. jp .L3 { NaN }
  237. jae .L1 { |frac(z)| <= 1, good }
  238. fld %st(1)
  239. fabs
  240. flds large(%rip)
  241. fcomip %st(1),%st(0)
  242. fstp %st
  243. jb .L3 { int(z) >= 24576 }
  244. .L0:
  245. { Calculate 2**frac(z)-1 as N*(N+2), where N=2**(frac(z)/2)-1 }
  246. fmuls half(%rip)
  247. f2xm1
  248. fld %st
  249. fadds two(%rip)
  250. fmulp %st,%st(1)
  251. jmp .L2
  252. .L3:
  253. fstp %st { pop frac(z) and load 0 }
  254. fldz
  255. .L1:
  256. f2xm1
  257. .L2:
  258. fld1
  259. faddp %st,%st(1)
  260. fscale
  261. fstp %st(1)
  262. end;
  263. {$endif FPC_SYSTEM_HAS_EXP}
  264. {$ifndef FPC_SYSTEM_HAS_FRAC}
  265. {$define FPC_SYSTEM_HAS_FRAC}
  266. function fpc_frac_real(d : ValReal) : ValReal;assembler;compilerproc;
  267. var
  268. oldcw,newcw: word;
  269. asm
  270. fnstcw oldcw
  271. fldt d
  272. movw oldcw,%cx
  273. orw $0x0c00,%cx
  274. movw %cx,newcw
  275. fldcw newcw
  276. fld %st
  277. frndint
  278. fsubrp %st,%st(1)
  279. fwait
  280. fldcw oldcw
  281. end;
  282. {$endif FPC_SYSTEM_HAS_FRAC}
  283. {$ifndef FPC_SYSTEM_HAS_INT}
  284. {$define FPC_SYSTEM_HAS_INT}
  285. function fpc_int_real(d : ValReal) : ValReal;assembler;compilerproc;
  286. var
  287. oldcw,newcw: word;
  288. asm
  289. fnstcw oldcw
  290. movw oldcw,%cx
  291. orw $0x0c00,%cx
  292. movw %cx,newcw
  293. fldcw newcw
  294. fldt d
  295. frndint
  296. fwait
  297. fldcw oldcw
  298. end;
  299. {$endif FPC_SYSTEM_HAS_INT}
  300. {$ifndef FPC_SYSTEM_HAS_TRUNC}
  301. {$define FPC_SYSTEM_HAS_TRUNC}
  302. function fpc_trunc_real(d : ValReal) : int64;assembler;compilerproc;
  303. var
  304. oldcw,
  305. newcw : word;
  306. res : int64;
  307. asm
  308. fnstcw oldcw
  309. movw oldcw,%cx
  310. orw $0x0c00,%cx
  311. movw %cx,newcw
  312. fldcw newcw
  313. fldt d
  314. fistpq res
  315. fwait
  316. movq res,%rax
  317. fldcw oldcw
  318. end;
  319. {$endif FPC_SYSTEM_HAS_TRUNC}
  320. {$ifndef FPC_SYSTEM_HAS_ROUND}
  321. {$define FPC_SYSTEM_HAS_ROUND}
  322. function fpc_round_real(d : ValReal) : int64;assembler;compilerproc;
  323. var
  324. res : int64;
  325. asm
  326. fldt d
  327. fistpq res
  328. fwait
  329. movq res,%rax
  330. end;
  331. {$endif FPC_SYSTEM_HAS_ROUND}
  332. {$else FPC_HAS_TYPE_EXTENDED}
  333. {$ifndef FPC_SYSTEM_HAS_INT}
  334. {$define FPC_SYSTEM_HAS_INT}
  335. function fpc_int_real(d : ValReal) : ValReal;compilerproc; assembler; nostackframe;
  336. asm
  337. movq %xmm0, %rax
  338. shr $48, %rax
  339. and $0x7ff0,%ax
  340. cmp $0x4330,%ax
  341. jge .L0
  342. cvttsd2si %xmm0, %rax
  343. cvtsi2sd %rax, %xmm0
  344. .L0:
  345. end;
  346. {$endif FPC_SYSTEM_HAS_INT}
  347. {$ifndef FPC_SYSTEM_HAS_TRUNC}
  348. {$define FPC_SYSTEM_HAS_TRUNC}
  349. function fpc_trunc_real(d : ValReal) : int64;compilerproc; assembler; nostackframe;
  350. asm
  351. cvttsd2si %xmm0,%rax;
  352. end;
  353. {$endif FPC_SYSTEM_HAS_TRUNC}
  354. {$ifndef FPC_SYSTEM_HAS_ROUND}
  355. {$define FPC_SYSTEM_HAS_ROUND}
  356. function fpc_round_real(d : ValReal) : int64;compilerproc; assembler; nostackframe;
  357. asm
  358. cvtsd2si %xmm0,%rax;
  359. end;
  360. {$endif FPC_SYSTEM_HAS_ROUND}
  361. {$ifndef FPC_SYSTEM_HAS_FRAC}
  362. {$define FPC_SYSTEM_HAS_FRAC}
  363. function fpc_frac_real(d: ValReal) : ValReal;compilerproc; assembler; nostackframe;
  364. asm
  365. { Windows defines %xmm4 and %xmm5 as first non-parameter volatile registers;
  366. on SYSV systems all are considered as such, so use %xmm4 }
  367. movq %xmm0, %rax
  368. movapd %xmm0, %xmm4
  369. shr $48, %rax
  370. and $0x7ff0,%ax
  371. cmp $0x4330,%ax
  372. jge .L0
  373. cvttsd2si %xmm0, %rax
  374. cvtsi2sd %rax, %xmm4
  375. .L0:
  376. subsd %xmm4, %xmm0
  377. end;
  378. {$endif FPC_SYSTEM_HAS_FRAC}
  379. {$endif FPC_HAS_TYPE_EXTENDED}