math.inc 11 KB

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