math.inc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2001 by the Free Pascal development team
  4. Implementation of mathematical routines (for extended type)
  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. {****************************************************************************
  30. FPU Control word
  31. ****************************************************************************}
  32. {$push}
  33. {$codealign constmin=16}
  34. const
  35. FPC_ABSMASK_SINGLE: array[0..1] of qword=($7fffffff7fffffff,$7fffffff7fffffff); cvar; public;
  36. FPC_ABSMASK_DOUBLE: array[0..1] of qword=($7fffffffffffffff,$7fffffffffffffff); cvar; public;
  37. {$pop}
  38. procedure Set8087CW(cw:word);
  39. begin
  40. { pic-safe ; cw will not be a regvar because it's accessed from }
  41. { assembler }
  42. default8087cw:=cw;
  43. asm
  44. fnclex
  45. fldcw cw
  46. end;
  47. end;
  48. function Get8087CW:word;assembler;
  49. asm
  50. pushl $0
  51. fnstcw (%esp)
  52. popl %eax
  53. end;
  54. procedure SetMXCSR(w : dword);
  55. begin
  56. defaultmxcsr:=w;
  57. {$ifndef OLD_ASSEMBLER}
  58. asm
  59. ldmxcsr w
  60. end;
  61. {$else}
  62. { Use convoluted code to avoid relocation on
  63. ldmxcsr opcode, and use .byte version }
  64. asm
  65. mov w,%eax
  66. subl $4,%esp
  67. mov %eax,(%esp)
  68. //ldmxcsr (%esp)
  69. .byte 0x0f,0xae,0x14,0x24
  70. addl $4,%esp
  71. end;
  72. {$endif OLD_ASSEMBLER}
  73. end;
  74. function GetMXCSR : dword;
  75. var
  76. _w : dword;
  77. begin
  78. {$ifndef OLD_ASSEMBLER}
  79. asm
  80. stmxcsr _w
  81. end;
  82. {$else}
  83. asm
  84. { Use convoluted code to avoid relocation on
  85. ldmxcsr opcode, and use .byte version }
  86. subl $4,%esp
  87. //stmxcsr (%esp)
  88. .byte 0x0f,0xae,0x14,0x24
  89. mov (%esp),%eax
  90. addl $4,%esp
  91. mov %eax,_w
  92. end;
  93. {$endif OLD_ASSEMBLER}
  94. result:=_w;
  95. end;
  96. procedure SetSSECSR(w : dword);
  97. begin
  98. SetMXCSR(w);
  99. end;
  100. function GetSSECSR: dword;
  101. begin
  102. result:=GetMXCSR;
  103. end;
  104. {****************************************************************************
  105. EXTENDED data type routines
  106. ****************************************************************************}
  107. {$define FPC_SYSTEM_HAS_ABS}
  108. function fpc_abs_real(d : ValReal) : ValReal;compilerproc;
  109. begin
  110. { Function is handled internal in the compiler }
  111. runerror(207);
  112. result:=0;
  113. end;
  114. {$define FPC_SYSTEM_HAS_SQR}
  115. function fpc_sqr_real(d : ValReal) : ValReal;compilerproc;
  116. begin
  117. { Function is handled internal in the compiler }
  118. runerror(207);
  119. result:=0;
  120. end;
  121. {$define FPC_SYSTEM_HAS_SQRT}
  122. function fpc_sqrt_real(d : ValReal) : ValReal;compilerproc;
  123. begin
  124. { Function is handled internal in the compiler }
  125. runerror(207);
  126. result:=0;
  127. end;
  128. {$define FPC_SYSTEM_HAS_ARCTAN}
  129. function fpc_arctan_real(d : ValReal) : ValReal;compilerproc;
  130. begin
  131. { Function is handled internal in the compiler }
  132. runerror(207);
  133. result:=0;
  134. end;
  135. {$define FPC_SYSTEM_HAS_LN}
  136. function fpc_ln_real(d : ValReal) : ValReal;compilerproc;
  137. begin
  138. { Function is handled internal in the compiler }
  139. runerror(207);
  140. result:=0;
  141. end;
  142. {$define FPC_SYSTEM_HAS_SIN}
  143. function fpc_sin_real(d : ValReal) : ValReal;compilerproc;
  144. begin
  145. { Function is handled internal in the compiler }
  146. runerror(207);
  147. result:=0;
  148. end;
  149. {$define FPC_SYSTEM_HAS_COS}
  150. function fpc_cos_real(d : ValReal) : ValReal;compilerproc;
  151. begin
  152. { Function is handled internal in the compiler }
  153. runerror(207);
  154. result:=0;
  155. end;
  156. {$if not defined(FPC_PIC) or defined(OLD_ASSEMBLER)}
  157. {$ifndef OPENBSD}
  158. {$define DISABLE_PIC_IN_EXP_REAL}
  159. {$endif ndef OPENBSD}
  160. {$endif}
  161. {$define FPC_SYSTEM_HAS_EXP}
  162. { exp function adapted from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt
  163. * translated into AT&T syntax
  164. + PIC support
  165. * return +Inf/0 for +Inf/-Inf input, instead of NaN }
  166. function fpc_exp_real(d : ValReal) : ValReal;assembler;compilerproc;
  167. const
  168. ln2hi: double=6.9314718036912382E-001;
  169. ln2lo: double=1.9082149292705877E-010;
  170. large: single=24576.0;
  171. two: single=2.0;
  172. half: single=0.5;
  173. asm
  174. {$ifndef DISABLE_PIC_IN_EXP_REAL}
  175. call .LPIC
  176. .LPIC:
  177. pop %ecx
  178. {$endif not DISABLE_PIC_IN_EXP_REAL}
  179. fldt d
  180. fldl2e
  181. fmul %st(1),%st { z = d * log2(e) }
  182. frndint
  183. { Calculate frac(z) using modular arithmetic to avoid precision loss. }
  184. {$ifndef DISABLE_PIC_IN_EXP_REAL}
  185. fldl ln2hi-.LPIC(%ecx)
  186. {$else}
  187. fldl ln2hi
  188. {$endif}
  189. fmul %st(1),%st
  190. fsubrp %st,%st(2)
  191. {$ifndef DISABLE_PIC_IN_EXP_REAL}
  192. fldl ln2lo-.LPIC(%ecx)
  193. {$else}
  194. fldl ln2lo
  195. {$endif}
  196. fmul %st(1),%st
  197. fsubrp %st,%st(2)
  198. fxch %st(1) { (d-int(z)*ln2_hi)-int(z)*ln2_lo }
  199. fldl2e
  200. fmulp %st,%st(1) { frac(z) }
  201. { The above code can result in |frac(z)|>1, particularly when rounding mode
  202. is not "round to nearest". f2xm1 is undefined in this case, so a check
  203. is necessary. Furthermore, frac(z) evaluates to NaN for d=+-Inf. }
  204. fld %st
  205. fabs
  206. fld1
  207. fcompp
  208. fstsw %ax
  209. sahf
  210. jp .L3 { NaN }
  211. jae .L1 { frac(z) <= 1 }
  212. fld %st(1)
  213. fabs
  214. {$ifndef DISABLE_PIC_IN_EXP_REAL}
  215. fcomps large-.LPIC(%ecx)
  216. {$else}
  217. fcomps large
  218. {$endif}
  219. fstsw %ax
  220. sahf
  221. jb .L0 { int(z) < 24576 }
  222. .L3:
  223. fstp %st { zero out frac(z), hard way because }
  224. fldz { "fsub %st,%st" does not work for NaN }
  225. jmp .L1
  226. .L0:
  227. { Calculate 2**frac(z)-1 as N*(N+2), where N=2**(frac(z)/2)-1 }
  228. {$ifndef DISABLE_PIC_IN_EXP_REAL}
  229. fmuls half-.LPIC(%ecx)
  230. {$else}
  231. fmuls half
  232. {$endif}
  233. f2xm1
  234. fld %st
  235. {$ifndef DISABLE_PIC_IN_EXP_REAL}
  236. fadds two-.LPIC(%ecx)
  237. {$else}
  238. fadds two
  239. {$endif}
  240. fmulp %st,%st(1)
  241. jmp .L2
  242. .L1:
  243. f2xm1
  244. .L2:
  245. fld1
  246. faddp %st,%st(1)
  247. fscale
  248. fstp %st(1)
  249. end;
  250. {$define FPC_SYSTEM_HAS_FRAC}
  251. function fpc_frac_real(d : ValReal) : ValReal;assembler;compilerproc;
  252. asm
  253. subl $4,%esp
  254. fnstcw (%esp)
  255. fwait
  256. movw (%esp),%cx
  257. orw $0x0f00,(%esp)
  258. fldcw (%esp)
  259. fldt d
  260. frndint
  261. fldt d
  262. fsub %st(1),%st
  263. fstp %st(1)
  264. movw %cx,(%esp)
  265. fldcw (%esp)
  266. end;
  267. {$define FPC_SYSTEM_HAS_INT}
  268. function fpc_int_real(d : ValReal) : ValReal;assembler;compilerproc;
  269. asm
  270. subl $4,%esp
  271. fnstcw (%esp)
  272. fwait
  273. movw (%esp),%cx
  274. orw $0x0f00,(%esp)
  275. fldcw (%esp)
  276. fwait
  277. fldt d
  278. frndint
  279. fwait
  280. movw %cx,(%esp)
  281. fldcw (%esp)
  282. end;
  283. {$define FPC_SYSTEM_HAS_TRUNC}
  284. function fpc_trunc_real(d : ValReal) : int64;assembler;compilerproc;
  285. asm
  286. subl $12,%esp
  287. fldt d
  288. fnstcw (%esp)
  289. movw (%esp),%cx
  290. orw $0x0f00,(%esp)
  291. fldcw (%esp)
  292. movw %cx,(%esp)
  293. fistpq 4(%esp)
  294. fldcw (%esp)
  295. fwait
  296. movl 4(%esp),%eax
  297. movl 8(%esp),%edx
  298. end;
  299. {$define FPC_SYSTEM_HAS_ROUND}
  300. { keep for bootstrapping with 2.0.x }
  301. function fpc_round_real(d : ValReal) : int64;compilerproc;assembler;
  302. var
  303. res : int64;
  304. asm
  305. fldt d
  306. fistpq res
  307. fwait
  308. movl res,%eax
  309. movl res+4,%edx
  310. end;