math.inc 11 KB

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