math.inc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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;nostackframe;
  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;nostackframe;
  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;nostackframe;
  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;nostackframe;
  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;nostackframe;
  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. fcompp
  215. fstsw %ax
  216. sahf
  217. jp .L3 { NaN }
  218. jae .L1 { |frac(z)| <= 1, good }
  219. fld %st(1)
  220. fabs
  221. fcomps large(%rip)
  222. fstsw %ax
  223. sahf
  224. jb .L0 { int(z) < 24576 }
  225. .L3:
  226. fstp %st { pop frac(z) and load 0 }
  227. fldz
  228. jmp .L1
  229. .L0:
  230. { Calculate 2**frac(z)-1 as N*(N+2), where N=2**(frac(z)/2)-1 }
  231. fmuls half(%rip)
  232. f2xm1
  233. fld %st
  234. fadds two(%rip)
  235. fmulp %st,%st(1)
  236. jmp .L2
  237. .L1:
  238. f2xm1
  239. .L2:
  240. fld1
  241. faddp %st,%st(1)
  242. fscale
  243. fstp %st(1)
  244. end;
  245. {$endif FPC_SYSTEM_HAS_EXP}
  246. {$ifndef FPC_SYSTEM_HAS_FRAC}
  247. {$define FPC_SYSTEM_HAS_FRAC}
  248. function fpc_frac_real(d : ValReal) : ValReal;assembler;compilerproc;
  249. var
  250. oldcw,newcw: word;
  251. asm
  252. fnstcw oldcw
  253. fwait
  254. movw oldcw,%cx
  255. orw $0x0c3f,%cx
  256. movw %cx,newcw
  257. fldcw newcw
  258. fwait
  259. fldt d
  260. frndint
  261. fldt d
  262. fsub %st(1),%st
  263. fstp %st(1)
  264. fnclex
  265. fldcw oldcw
  266. end;
  267. {$endif FPC_SYSTEM_HAS_FRAC}
  268. {$ifndef FPC_SYSTEM_HAS_INT}
  269. {$define FPC_SYSTEM_HAS_INT}
  270. function fpc_int_real(d : ValReal) : ValReal;assembler;compilerproc;
  271. var
  272. oldcw,newcw: word;
  273. asm
  274. fnstcw oldcw
  275. fwait
  276. movw oldcw,%cx
  277. orw $0x0c3f,%cx
  278. movw %cx,newcw
  279. fldcw newcw
  280. fwait
  281. fldt d
  282. frndint
  283. fwait
  284. fldcw oldcw
  285. end;
  286. {$endif FPC_SYSTEM_HAS_INT}
  287. {$ifndef FPC_SYSTEM_HAS_TRUNC}
  288. {$define FPC_SYSTEM_HAS_TRUNC}
  289. function fpc_trunc_real(d : ValReal) : int64;assembler;compilerproc;
  290. var
  291. oldcw,
  292. newcw : word;
  293. res : int64;
  294. asm
  295. fnstcw oldcw
  296. fwait
  297. movw oldcw,%cx
  298. orw $0x0c3f,%cx
  299. movw %cx,newcw
  300. fldcw newcw
  301. fldt d
  302. fistpq res
  303. fwait
  304. movq res,%rax
  305. fldcw oldcw
  306. end;
  307. {$endif FPC_SYSTEM_HAS_TRUNC}
  308. {$ifndef FPC_SYSTEM_HAS_ROUND}
  309. {$define FPC_SYSTEM_HAS_ROUND}
  310. function fpc_round_real(d : ValReal) : int64;assembler;compilerproc;
  311. var
  312. res : int64;
  313. asm
  314. fldt d
  315. fistpq res
  316. fwait
  317. movq res,%rax
  318. end;
  319. {$endif FPC_SYSTEM_HAS_ROUND}
  320. {$else FPC_HAS_TYPE_EXTENDED}
  321. {$define FPC_SYSTEM_HAS_TRUNC}
  322. function fpc_trunc_real(d : ValReal) : int64;compilerproc; assembler; nostackframe;
  323. asm
  324. cvttsd2si %xmm0,%rax;
  325. end;
  326. {$define FPC_SYSTEM_HAS_ROUND}
  327. function fpc_round_real(d : ValReal) : int64;compilerproc; assembler; nostackframe;
  328. asm
  329. cvtsd2si %xmm0,%rax;
  330. end;
  331. {$endif FPC_HAS_TYPE_EXTENDED}