genmath.inc 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2007 by Several contributors
  4. Generic mathematical routines (on type real)
  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. { Credits }
  13. {*************************************************************************}
  14. { Copyright Abandoned, 1987, Fred Fish }
  15. { }
  16. { This previously copyrighted work has been placed into the }
  17. { public domain by the author (Fred Fish) and may be freely used }
  18. { for any purpose, private or commercial. I would appreciate }
  19. { it, as a courtesy, if this notice is left in all copies and }
  20. { derivative works. Thank you, and enjoy... }
  21. { }
  22. { The author makes no warranty of any kind with respect to this }
  23. { product and explicitly disclaims any implied warranties of }
  24. { merchantability or fitness for any particular purpose. }
  25. {-------------------------------------------------------------------------}
  26. { Copyright (c) 1992 Odent Jean Philippe }
  27. { }
  28. { The source can be modified as long as my name appears and some }
  29. { notes explaining the modifications done are included in the file. }
  30. {-------------------------------------------------------------------------}
  31. { Copyright (c) 1997 Carl Eric Codere }
  32. {-------------------------------------------------------------------------}
  33. {-------------------------------------------------------------------------
  34. Using functions from AMath/DAMath libraries, which are covered by the
  35. following license:
  36. (C) Copyright 2009-2013 Wolfgang Ehrhardt
  37. This software is provided 'as-is', without any express or implied warranty.
  38. In no event will the authors be held liable for any damages arising from
  39. the use of this software.
  40. Permission is granted to anyone to use this software for any purpose,
  41. including commercial applications, and to alter it and redistribute it
  42. freely, subject to the following restrictions:
  43. 1. The origin of this software must not be misrepresented; you must not
  44. claim that you wrote the original software. If you use this software in
  45. a product, an acknowledgment in the product documentation would be
  46. appreciated but is not required.
  47. 2. Altered source versions must be plainly marked as such, and must not be
  48. misrepresented as being the original software.
  49. 3. This notice may not be removed or altered from any source distribution.
  50. ----------------------------------------------------------------------------}
  51. type
  52. PReal = ^Real;
  53. { float64 definition is now in genmathh.inc,
  54. to ensure that float64 will always be in
  55. the system interface symbol table. }
  56. const
  57. PIO4 = 7.85398163397448309616E-1; { pi/4 }
  58. SQRT2 = 1.41421356237309504880; { sqrt(2) }
  59. LOG2E = 1.4426950408889634073599; { 1/log(2) }
  60. lossth = 1.073741824e9;
  61. MAXLOG = 8.8029691931113054295988E1; { log(2**127) }
  62. MINLOG = -8.872283911167299960540E1; { log(2**-128) }
  63. H2_54: double = 18014398509481984.0; {2^54}
  64. huge: double = 1e300;
  65. one: double = 1.0;
  66. zero: double = 0;
  67. {$if not defined(FPC_SYSTEM_HAS_SIN) or not defined(FPC_SYSTEM_HAS_COS)}
  68. const sincof : array[0..5] of Real = (
  69. 1.58962301576546568060E-10,
  70. -2.50507477628578072866E-8,
  71. 2.75573136213857245213E-6,
  72. -1.98412698295895385996E-4,
  73. 8.33333333332211858878E-3,
  74. -1.66666666666666307295E-1);
  75. coscof : array[0..5] of Real = (
  76. -1.13585365213876817300E-11,
  77. 2.08757008419747316778E-9,
  78. -2.75573141792967388112E-7,
  79. 2.48015872888517045348E-5,
  80. -1.38888888888730564116E-3,
  81. 4.16666666666665929218E-2);
  82. {$endif}
  83. {*
  84. -------------------------------------------------------------------------------
  85. Raises the exceptions specified by `flags'. Floating-point traps can be
  86. defined here if desired. It is currently not possible for such a trap
  87. to substitute a result value. If traps are not implemented, this routine
  88. should be simply `softfloat_exception_flags |= flags;'.
  89. -------------------------------------------------------------------------------
  90. *}
  91. procedure float_raise(i: TFPUException);
  92. begin
  93. float_raise([i]);
  94. end;
  95. procedure float_raise(i: TFPUExceptionMask);
  96. var
  97. pflags: ^TFPUExceptionMask;
  98. unmasked_flags: TFPUExceptionMask;
  99. Begin
  100. { taking address of threadvar produces somewhat more compact code }
  101. pflags := @softfloat_exception_flags;
  102. pflags^:=pflags^ + i;
  103. unmasked_flags := pflags^ - softfloat_exception_mask;
  104. { before we raise the exception, we mark it as handled,
  105. this behaviour is similiar to the hardware handler in SignalToRunerror }
  106. SysResetFPU;
  107. if (float_flag_invalid in unmasked_flags) then
  108. HandleError(207)
  109. else if (float_flag_divbyzero in unmasked_flags) then
  110. HandleError(208)
  111. else if (float_flag_overflow in unmasked_flags) then
  112. HandleError(205)
  113. else if (float_flag_underflow in unmasked_flags) then
  114. HandleError(206)
  115. else if (float_flag_inexact in unmasked_flags) then
  116. HandleError(207)
  117. else if (float_flag_denormal in unmasked_flags) then
  118. HandleError(216);
  119. end;
  120. { This function does nothing, but its argument is expected to be an expression
  121. which causes FPE when calculated. If exception is masked, it just returns true,
  122. allowing to use it in expressions. }
  123. function fpe_helper(x: valreal): boolean;
  124. begin
  125. result:=true;
  126. end;
  127. {$ifdef SUPPORT_DOUBLE}
  128. {$ifndef FPC_HAS_FLOAT64HIGH}
  129. {$define FPC_HAS_FLOAT64HIGH}
  130. function float64high(d: double): longint; inline;
  131. begin
  132. result:=float64(d).high;
  133. end;
  134. procedure float64sethigh(var d: double; l: longint); inline;
  135. begin
  136. float64(d).high:=l;
  137. end;
  138. {$endif FPC_HAS_FLOAT64HIGH}
  139. {$ifndef FPC_HAS_FLOAT64LOW}
  140. {$define FPC_HAS_FLOAT64LOW}
  141. function float64low(d: double): longint; inline;
  142. begin
  143. result:=float64(d).low;
  144. end;
  145. procedure float64setlow(var d: double; l: longint); inline;
  146. begin
  147. float64(d).low:=l;
  148. end;
  149. {$endif FPC_HAS_FLOAT64LOW}
  150. {$endif SUPPORT_DOUBLE}
  151. {$ifndef FPC_SYSTEM_HAS_TRUNC}
  152. {$ifdef SUPPORT_DOUBLE}
  153. { based on softfloat float64_to_int64_round_to_zero }
  154. function fpc_trunc_real(d : valreal) : int64; compilerproc;
  155. var
  156. aExp, shiftCount : smallint;
  157. aSig : int64;
  158. z : int64;
  159. a: float64;
  160. begin
  161. a:=float64(d);
  162. aSig:=(int64(a.high and $000fffff) shl 32) or longword(a.low);
  163. aExp:=(a.high shr 20) and $7FF;
  164. if aExp<>0 then
  165. aSig:=aSig or $0010000000000000;
  166. shiftCount:= aExp-$433;
  167. if 0<=shiftCount then
  168. begin
  169. if aExp>=$43e then
  170. begin
  171. if (a.high<>longint($C3E00000)) or (a.low<>0) then
  172. begin
  173. fpe_helper(zero/zero);
  174. if (longint(a.high)>=0) or ((aExp=$7FF) and
  175. (aSig<>$0010000000000000 )) then
  176. begin
  177. result:=$7FFFFFFFFFFFFFFF;
  178. exit;
  179. end;
  180. end;
  181. result:=$8000000000000000;
  182. exit;
  183. end;
  184. z:=aSig shl shiftCount;
  185. end
  186. else
  187. begin
  188. if aExp<$3fe then
  189. begin
  190. result:=0;
  191. exit;
  192. end;
  193. z:=aSig shr -shiftCount;
  194. {
  195. if (aSig shl (shiftCount and 63))<>0 then
  196. float_exception_flags |= float_flag_inexact;
  197. }
  198. end;
  199. if longint(a.high)<0 then
  200. z:=-z;
  201. result:=z;
  202. end;
  203. {$else SUPPORT_DOUBLE}
  204. { based on softfloat float32_to_int64_round_to_zero }
  205. Function fpc_trunc_real( d: valreal ): int64; compilerproc;
  206. Var
  207. a : float32;
  208. aExp, shiftCount : smallint;
  209. aSig : longint;
  210. aSig64, z : int64;
  211. Begin
  212. a := float32(d);
  213. aSig := a and $007FFFFF;
  214. aExp := (a shr 23) and $FF;
  215. shiftCount := aExp - $BE;
  216. if ( 0 <= shiftCount ) then
  217. Begin
  218. if ( a <> Float32($DF000000) ) then
  219. Begin
  220. fpe_helper( zero/zero );
  221. if ( (longint(a)>=0) or ( ( aExp = $FF ) and (aSig<>0) ) ) then
  222. Begin
  223. result:=$7fffffffffffffff;
  224. exit;
  225. end;
  226. End;
  227. result:=$8000000000000000;
  228. exit;
  229. End
  230. else
  231. if ( aExp <= $7E ) then
  232. Begin
  233. result := 0;
  234. exit;
  235. End;
  236. aSig64 := int64( aSig or $00800000 ) shl 40;
  237. z := aSig64 shr ( - shiftCount );
  238. if ( longint(a)<0 ) then z := - z;
  239. result := z;
  240. End;
  241. {$endif SUPPORT_DOUBLE}
  242. {$endif not FPC_SYSTEM_HAS_TRUNC}
  243. {$ifndef FPC_SYSTEM_HAS_INT}
  244. {$ifdef SUPPORT_DOUBLE}
  245. { straight Pascal translation of the code for __trunc() in }
  246. { the file sysdeps/libm-ieee754/s_trunc.c of glibc (JM) }
  247. function fpc_int_real(d: ValReal): ValReal;compilerproc;
  248. var
  249. i0, j0: longint;
  250. i1: cardinal;
  251. sx: longint;
  252. f64 : float64;
  253. begin
  254. f64:=float64(d);
  255. i0 := f64.high;
  256. i1 := cardinal(f64.low);
  257. sx := i0 and $80000000;
  258. j0 := ((i0 shr 20) and $7ff) - $3ff;
  259. if (j0 < 20) then
  260. begin
  261. if (j0 < 0) then
  262. begin
  263. { the magnitude of the number is < 1 so the result is +-0. }
  264. f64.high := sx;
  265. f64.low := 0;
  266. end
  267. else
  268. begin
  269. f64.high := sx or (i0 and not($fffff shr j0));
  270. f64.low := 0;
  271. end
  272. end
  273. else if (j0 > 51) then
  274. begin
  275. if (j0 = $400) then
  276. { d is inf or NaN }
  277. exit(d + d); { don't know why they do this (JM) }
  278. end
  279. else
  280. begin
  281. f64.high := i0;
  282. f64.low := longint(i1 and not(cardinal($ffffffff) shr (j0 - 20)));
  283. end;
  284. result:=double(f64);
  285. end;
  286. {$else SUPPORT_DOUBLE}
  287. function fpc_int_real(d : ValReal) : ValReal;compilerproc;
  288. begin
  289. { this will be correct since real = single in the case of }
  290. { the motorola version of the compiler... }
  291. result:=ValReal(trunc(d));
  292. end;
  293. {$endif SUPPORT_DOUBLE}
  294. {$endif not FPC_SYSTEM_HAS_INT}
  295. {$ifndef FPC_SYSTEM_HAS_ABS}
  296. function fpc_abs_real(d : ValReal) : ValReal;compilerproc;
  297. begin
  298. if (d<0.0) then
  299. result := -d
  300. else
  301. result := d ;
  302. end;
  303. {$endif not FPC_SYSTEM_HAS_ABS}
  304. {$ifndef SYSTEM_HAS_FREXP}
  305. procedure frexp(X: Real; out Mantissa: Real; out Exponent: longint);
  306. {* frexp() extracts the exponent from x. It returns an integer *}
  307. {* power of two to expnt and the significand between 0.5 and 1 *}
  308. {* to y. Thus x = y * 2**expn. *}
  309. begin
  310. exponent:=0;
  311. if (abs(x)<0.5) then
  312. While (abs(x)<0.5) do
  313. begin
  314. x := x*2;
  315. Dec(exponent);
  316. end
  317. else
  318. While (abs(x)>1) do
  319. begin
  320. x := x/2;
  321. Inc(exponent);
  322. end;
  323. Mantissa := x;
  324. end;
  325. {$endif not SYSTEM_HAS_FREXP}
  326. {$ifndef SYSTEM_HAS_LDEXP}
  327. {$ifdef SUPPORT_DOUBLE}
  328. { ldexpd function adapted from DAMath library (C) Copyright 2013 Wolfgang Ehrhardt }
  329. function ldexp( x: Real; N: Integer):Real;
  330. {* ldexp() multiplies x by 2**n. *}
  331. var
  332. i: integer;
  333. begin
  334. i := (float64high(x) and $7ff00000) shr 20;
  335. {if +-INF, NaN, 0 or if e=0 return d}
  336. if (i=$7FF) or (N=0) or (x=0.0) then
  337. ldexp := x
  338. else if i=0 then {Denormal: result = d*2^54*2^(e-54)}
  339. ldexp := ldexp(x*H2_54, N-54)
  340. else
  341. begin
  342. N := N+i;
  343. if N>$7FE then { overflow }
  344. begin
  345. if x>0.0 then
  346. ldexp := 2.0*huge
  347. else
  348. ldexp := (-2.0)*huge;
  349. end
  350. else if N<1 then
  351. begin
  352. {underflow or denormal}
  353. if N<-53 then
  354. ldexp := 0.0
  355. else
  356. begin
  357. {Denormal: result = d*2^(e+54)/2^54}
  358. inc(N,54);
  359. float64sethigh(x,(float64high(x) and $800FFFFF) or (longint(N) shl 20));
  360. ldexp := x/H2_54;
  361. end;
  362. end
  363. else
  364. begin
  365. float64sethigh(x,(float64high(x) and $800FFFFF) or (longint(N) shl 20));
  366. ldexp := x;
  367. end;
  368. end;
  369. end;
  370. {$else SUPPORT_DOUBLE}
  371. function ldexp( x: Real; N: Integer):Real;
  372. {* ldexp() multiplies x by 2**n. *}
  373. var r : Real;
  374. begin
  375. R := 1;
  376. if N>0 then
  377. while N>0 do
  378. begin
  379. R:=R*2;
  380. Dec(N);
  381. end
  382. else
  383. while N<0 do
  384. begin
  385. R:=R/2;
  386. Inc(N);
  387. end;
  388. ldexp := x * R;
  389. end;
  390. {$endif SUPPORT_DOUBLE}
  391. {$endif not SYSTEM_HAS_LDEXP}
  392. function floord(x: double): double; inline;
  393. begin
  394. result := int(x);
  395. if result>x then
  396. result := result - 1.0;
  397. end;
  398. {*
  399. * ====================================================
  400. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  401. *
  402. * Developed at SunPro, a Sun Microsystems, Inc. business.
  403. * Permission to use, copy, modify, and distribute this
  404. * software is freely granted, provided that this notice
  405. * is preserved.
  406. * ====================================================
  407. *
  408. * Pascal port of this routine comes from DAMath library
  409. * (C) Copyright 2013 Wolfgang Ehrhardt
  410. *
  411. * k_rem_pio2 return the last three bits of N with y = x - N*pi/2
  412. * so that |y| < pi/2.
  413. *
  414. * The method is to compute the integer (mod 8) and fraction parts of
  415. * (2/pi)*x without doing the full multiplication. In general we
  416. * skip the part of the product that are known to be a huge integer
  417. * (more accurately, = 0 mod 8 ). Thus the number of operations are
  418. * independent of the exponent of the input.
  419. *
  420. * (2/pi) is represented by an array of 24-bit integers in ipio2[].
  421. *
  422. * Input parameters:
  423. * x[] The input value (must be positive) is broken into nx
  424. * pieces of 24-bit integers in double precision format.
  425. * x[i] will be the i-th 24 bit of x. The scaled exponent
  426. * of x[0] is given in input parameter e0 (i.e., x[0]*2^e0
  427. * match x's up to 24 bits.
  428. *
  429. * Example of breaking a double positive z into x[0]+x[1]+x[2]:
  430. * e0 = ilogb(z)-23
  431. * z = scalbn(z,-e0)
  432. * for i = 0,1,2
  433. * x[i] = floor(z)
  434. * z = (z-x[i])*2**24
  435. *
  436. *
  437. * y[] output result in an array of double precision numbers.
  438. * The dimension of y[] is:
  439. * 24-bit precision 1
  440. * 53-bit precision 2
  441. * 64-bit precision 2
  442. * 113-bit precision 3
  443. * The actual value is the sum of them. Thus for 113-bit
  444. * precison, one may have to do something like:
  445. *
  446. * long double t,w,r_head, r_tail;
  447. * t = (long double)y[2] + (long double)y[1];
  448. * w = (long double)y[0];
  449. * r_head = t+w;
  450. * r_tail = w - (r_head - t);
  451. *
  452. * e0 The exponent of x[0]. Must be <= 16360 or you need to
  453. * expand the ipio2 table.
  454. *
  455. * nx dimension of x[]
  456. *
  457. * prec an integer indicating the precision:
  458. * 0 24 bits (single)
  459. * 1 53 bits (double)
  460. * 2 64 bits (extended)
  461. * 3 113 bits (quad)
  462. *
  463. * Here is the description of some local variables:
  464. *
  465. * jk jk+1 is the initial number of terms of ipio2[] needed
  466. * in the computation. The recommended value is 2,3,4,
  467. * 6 for single, double, extended,and quad.
  468. *
  469. * jz local integer variable indicating the number of
  470. * terms of ipio2[] used.
  471. *
  472. * jx nx - 1
  473. *
  474. * jv index for pointing to the suitable ipio2[] for the
  475. * computation. In general, we want
  476. * ( 2^e0*x[0] * ipio2[jv-1]*2^(-24jv) )/8
  477. * is an integer. Thus
  478. * e0-3-24*jv >= 0 or (e0-3)/24 >= jv
  479. * Hence jv = max(0,(e0-3)/24).
  480. *
  481. * jp jp+1 is the number of terms in PIo2[] needed, jp = jk.
  482. *
  483. * q[] double array with integral value, representing the
  484. * 24-bits chunk of the product of x and 2/pi.
  485. *
  486. * q0 the corresponding exponent of q[0]. Note that the
  487. * exponent for q[i] would be q0-24*i.
  488. *
  489. * PIo2[] double precision array, obtained by cutting pi/2
  490. * into 24 bits chunks.
  491. *
  492. * f[] ipio2[] in floating point
  493. *
  494. * iq[] integer array by breaking up q[] in 24-bits chunk.
  495. *
  496. * fq[] final product of x*(2/pi) in fq[0],..,fq[jk]
  497. *
  498. * ih integer. If >0 it indicates q[] is >= 0.5, hence
  499. * it also indicates the *sign* of the result.
  500. *}
  501. {PIo2[] double array, obtained by cutting pi/2 into 24 bits chunks.}
  502. const
  503. PIo2chunked: array[0..7] of double = (
  504. 1.57079625129699707031e+00, { 0x3FF921FB, 0x40000000 }
  505. 7.54978941586159635335e-08, { 0x3E74442D, 0x00000000 }
  506. 5.39030252995776476554e-15, { 0x3CF84698, 0x80000000 }
  507. 3.28200341580791294123e-22, { 0x3B78CC51, 0x60000000 }
  508. 1.27065575308067607349e-29, { 0x39F01B83, 0x80000000 }
  509. 1.22933308981111328932e-36, { 0x387A2520, 0x40000000 }
  510. 2.73370053816464559624e-44, { 0x36E38222, 0x80000000 }
  511. 2.16741683877804819444e-51 { 0x3569F31D, 0x00000000 }
  512. );
  513. {Table of constants for 2/pi, 396 Hex digits (476 decimal) of 2/pi }
  514. ipio2: array[0..65] of longint = (
  515. $A2F983, $6E4E44, $1529FC, $2757D1, $F534DD, $C0DB62,
  516. $95993C, $439041, $FE5163, $ABDEBB, $C561B7, $246E3A,
  517. $424DD2, $E00649, $2EEA09, $D1921C, $FE1DEB, $1CB129,
  518. $A73EE8, $8235F5, $2EBB44, $84E99C, $7026B4, $5F7E41,
  519. $3991D6, $398353, $39F49C, $845F8B, $BDF928, $3B1FF8,
  520. $97FFDE, $05980F, $EF2F11, $8B5A0A, $6D1F6D, $367ECF,
  521. $27CB09, $B74F46, $3F669E, $5FEA2D, $7527BA, $C7EBE5,
  522. $F17B3D, $0739F7, $8A5292, $EA6BFB, $5FB11F, $8D5D08,
  523. $560330, $46FC7B, $6BABF0, $CFBC20, $9AF436, $1DA9E3,
  524. $91615E, $E61B08, $659985, $5F14A0, $68408D, $FFD880,
  525. $4D7327, $310606, $1556CA, $73A8C9, $60E27B, $C08C6B);
  526. init_jk: array[0..3] of integer = (2,3,4,6); {initial value for jk}
  527. two24: double = 16777216.0; {2^24}
  528. twon24: double = 5.9604644775390625e-08; {1/2^24}
  529. type
  530. TDA02 = array[0..2] of double; { 3 elements is enough for float128 }
  531. { inline to make use of the fact prec is always 2. }
  532. function k_rem_pio2(const x: TDA02; out y: TDA02; e0, nx, prec: integer): sizeint; inline;
  533. var
  534. i,ih,j,jz,jx,jv,jp,jk,carry,k,n,q0: longint;
  535. t: longint;
  536. iq: array[0..19] of longint;
  537. f,fq,q: array[0..19] of double;
  538. z,fw: double;
  539. begin
  540. {initialize jk}
  541. jk := init_jk[prec];
  542. jp := jk;
  543. {determine jx,jv,q0, note that 3>q0}
  544. jx := nx-1;
  545. jv := (e0-3) div 24; if jv<0 then jv := 0;
  546. q0 := e0-24*(jv+1);
  547. {set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk]}
  548. j := jv-jx;
  549. for i:=0 to jx+jk do
  550. begin
  551. if j<0 then f[i] := 0.0 else f[i] := ipio2[j];
  552. inc(j);
  553. end;
  554. {compute q[0],q[1],...q[jk]}
  555. for i:=0 to jk do
  556. begin
  557. fw := 0.0;
  558. for j:=0 to jx do
  559. fw := fw + x[j]*f[jx+i-j];
  560. q[i] := fw;
  561. end;
  562. jz := jk;
  563. repeat
  564. {distill q[] into iq[] reversingly}
  565. i := 0;
  566. z := q[jz];
  567. for j:=jz downto 1 do
  568. begin
  569. fw := trunc(twon24*z);
  570. iq[i] := trunc(z-two24*fw);
  571. z := q[j-1]+fw;
  572. inc(i);
  573. end;
  574. {compute n}
  575. z := ldexp(z,q0); {actual value of z}
  576. z := z - 8.0*floord(z*0.125); {trim off integer >= 8}
  577. n := trunc(z);
  578. z := z - n;
  579. ih := 0;
  580. if q0>0 then
  581. begin
  582. {need iq[jz-1] to determine n}
  583. t := (iq[jz-1] shr (24-q0));
  584. inc(n,t);
  585. dec(iq[jz-1], t shl (24-q0));
  586. ih := iq[jz-1] shr (23-q0);
  587. end
  588. else if q0=0 then
  589. ih := iq[jz-1] shr 23
  590. else if z>=0.5 then
  591. ih := 2;
  592. if ih>0 then {q > 0.5}
  593. begin
  594. inc(n);
  595. carry := 0;
  596. for i:=0 to jz-1 do
  597. begin
  598. {compute 1-q}
  599. t := iq[i];
  600. if carry=0 then
  601. begin
  602. if t<>0 then
  603. begin
  604. carry := 1;
  605. iq[i] := $1000000 - t;
  606. end
  607. end
  608. else
  609. iq[i] := $ffffff - t;
  610. end;
  611. if q0>0 then
  612. begin
  613. {rare case: chance is 1 in 12}
  614. case q0 of
  615. 1: iq[jz-1] := iq[jz-1] and $7fffff;
  616. 2: iq[jz-1] := iq[jz-1] and $3fffff;
  617. end;
  618. end;
  619. if ih=2 then
  620. begin
  621. z := 1.0 - z;
  622. if carry<>0 then
  623. z := z - ldexp(1.0,q0);
  624. end;
  625. end;
  626. {check if recomputation is needed}
  627. if z<>0.0 then
  628. break;
  629. t := 0;
  630. for i:=jz-1 downto jk do
  631. t := t or iq[i];
  632. if t<>0 then
  633. break;
  634. {need recomputation}
  635. k := 1;
  636. while iq[jk-k]=0 do {k = no. of terms needed}
  637. inc(k);
  638. for i:=jz+1 to jz+k do
  639. begin
  640. {add q[jz+1] to q[jz+k]}
  641. f[jx+i] := ipio2[jv+i];
  642. fw := 0.0;
  643. for j:=0 to jx do
  644. fw := fw + x[j]*f[jx+i-j];
  645. q[i] := fw;
  646. end;
  647. inc(jz,k);
  648. until False;
  649. {chop off zero terms}
  650. if z=0.0 then
  651. begin
  652. repeat
  653. dec(jz);
  654. dec(q0,24);
  655. until iq[jz]<>0;
  656. end
  657. else
  658. begin
  659. {break z into 24-bit if necessary}
  660. z := ldexp(z,-q0);
  661. if z>=two24 then
  662. begin
  663. fw := trunc(twon24*z);
  664. iq[jz] := trunc(z-two24*fw);
  665. inc(jz);
  666. inc(q0,24);
  667. iq[jz] := trunc(fw);
  668. end
  669. else
  670. iq[jz] := trunc(z);
  671. end;
  672. {convert integer "bit" chunk to floating-point value}
  673. fw := ldexp(1.0,q0);
  674. for i:=jz downto 0 do
  675. begin
  676. q[i] := fw*iq[i];
  677. fw := fw*twon24;
  678. end;
  679. {compute PIo2[0,...,jp]*q[jz,...,0]}
  680. for i:=jz downto 0 do
  681. begin
  682. fw :=0.0;
  683. k := 0;
  684. while (k<=jp) and (k<=jz-i) do
  685. begin
  686. fw := fw + double(PIo2chunked[k])*(q[i+k]);
  687. inc(k);
  688. end;
  689. fq[jz-i] := fw;
  690. end;
  691. {compress fq[] into y[]}
  692. case prec of
  693. 0:
  694. begin
  695. fw := 0.0;
  696. for i:=jz downto 0 do
  697. fw := fw + fq[i];
  698. if ih=0 then
  699. y[0] := fw
  700. else
  701. y[0] := -fw;
  702. end;
  703. 1, 2:
  704. begin
  705. fw := 0.0;
  706. for i:=jz downto 0 do
  707. fw := fw + fq[i];
  708. if ih=0 then
  709. y[0] := fw
  710. else
  711. y[0] := -fw;
  712. fw := fq[0]-fw;
  713. for i:=1 to jz do
  714. fw := fw + fq[i];
  715. if ih=0 then
  716. y[1] := fw
  717. else
  718. y[1] := -fw;
  719. end;
  720. 3:
  721. begin
  722. {painful}
  723. for i:=jz downto 1 do
  724. begin
  725. fw := fq[i-1]+fq[i];
  726. fq[i] := fq[i]+(fq[i-1]-fw);
  727. fq[i-1]:= fw;
  728. end;
  729. for i:=jz downto 2 do
  730. begin
  731. fw := fq[i-1]+fq[i];
  732. fq[i] := fq[i]+(fq[i-1]-fw);
  733. fq[i-1]:= fw;
  734. end;
  735. fw := 0.0;
  736. for i:=jz downto 2 do
  737. fw := fw + fq[i];
  738. if ih=0 then
  739. begin
  740. y[0] := fq[0];
  741. y[1] := fq[1];
  742. y[2] := fw;
  743. end
  744. else
  745. begin
  746. y[0] := -fq[0];
  747. y[1] := -fq[1];
  748. y[2] := -fw;
  749. end;
  750. end;
  751. end;
  752. k_rem_pio2 := n and 7;
  753. end;
  754. { Argument reduction of x: z = x - n*Pi/2, |z| <= Pi/4, result = n mod 8.}
  755. { Uses Payne/Hanek if |x| >= lossth, Cody/Waite otherwise}
  756. function rem_pio2_unlikely(x: double; out z: double): sizeint;
  757. var
  758. e0,nx: longint;
  759. tx,ty: TDA02;
  760. begin
  761. z := abs(x);
  762. e0 := (float64high(z) shr 20)-1046;
  763. if (e0 = ($7ff-1046)) then { z is Inf or NaN }
  764. begin
  765. {$push} {$optimization nofastmath}
  766. z := x - x;
  767. {$pop}
  768. exit(0);
  769. end;
  770. float64sethigh(z,float64high(z) - (e0 shl 20));
  771. tx[0] := trunc(z);
  772. z := (z-tx[0])*two24;
  773. tx[1] := trunc(z);
  774. tx[2] := (z-tx[1])*two24;
  775. nx := 3;
  776. while (tx[nx-1]=0.0) do dec(nx); { skip zero terms }
  777. result := k_rem_pio2(tx,ty,e0,nx,2);
  778. z := ty[0] + ty[1];
  779. if x<0 then
  780. begin
  781. result := (-result) and 7;
  782. z := -z;
  783. end;
  784. end;
  785. function rem_pio2(x: double; out z: double): sizeint;
  786. const
  787. tol = double(2.384185791015625E-7); {lossth*eps_d}
  788. DP1 = double(7.85398125648498535156E-1);
  789. DP2 = double(3.77489470793079817668E-8);
  790. DP3 = double(2.69515142907905952645E-15);
  791. var
  792. i: longint;
  793. y: double;
  794. begin
  795. y := abs(x);
  796. if (y < PIO4) then
  797. begin
  798. z := x;
  799. exit(0);
  800. end
  801. else if (y < lossth) then
  802. begin
  803. y := floord(x/PIO4);
  804. i := trunc(y - 16.0*floord(y*0.0625));
  805. if odd(i) then
  806. begin
  807. inc(i);
  808. y := y + 1.0;
  809. end;
  810. z := ((x - y * DP1) - y * DP2) - y * DP3;
  811. {If x is near a multiple of Pi/2, the C/W relative error may be large.}
  812. {In this case redo the calculation with the Payne/Hanek algorithm. }
  813. if abs(z) > tol then
  814. exit(i shr 1 and 7);
  815. end;
  816. result := rem_pio2_unlikely(x, z);
  817. end;
  818. {$ifndef FPC_SYSTEM_HAS_SQR}
  819. function fpc_sqr_real(d : ValReal) : ValReal;compilerproc;{$ifdef MATHINLINE}inline;{$endif}
  820. begin
  821. result := d*d;
  822. end;
  823. {$endif}
  824. {$ifndef FPC_SYSTEM_HAS_SQRT}
  825. function fpc_sqrt_real(d:ValReal):ValReal;compilerproc;
  826. {*****************************************************************}
  827. { Square root }
  828. {*****************************************************************}
  829. { }
  830. { SYNOPSIS: }
  831. { }
  832. { double x, y, sqrt(); }
  833. { }
  834. { y = sqrt( x ); }
  835. { }
  836. { DESCRIPTION: }
  837. { }
  838. { Returns the square root of x. }
  839. { }
  840. { Range reduction involves isolating the power of two of the }
  841. { argument and using a polynomial approximation to obtain }
  842. { a rough value for the square root. Then Heron's iteration }
  843. { is used three times to converge to an accurate value. }
  844. {*****************************************************************}
  845. var e : Longint;
  846. w,z : Real;
  847. begin
  848. if( d <= 0.0 ) then
  849. begin
  850. if d < 0.0 then
  851. result:=zero/zero
  852. else
  853. result := 0.0;
  854. end
  855. else
  856. begin
  857. w := d;
  858. { separate exponent and significand }
  859. frexp( d, z, e );
  860. { approximate square root of number between 0.5 and 1 }
  861. { relative error of approximation = 7.47e-3 }
  862. d := 4.173075996388649989089E-1 + 5.9016206709064458299663E-1 * z;
  863. { adjust for odd powers of 2 }
  864. if odd(e) then
  865. d := d*SQRT2;
  866. { re-insert exponent }
  867. d := ldexp( d, (e div 2) );
  868. { Newton iterations: }
  869. d := 0.5*(d + w/d);
  870. d := 0.5*(d + w/d);
  871. d := 0.5*(d + w/d);
  872. d := 0.5*(d + w/d);
  873. d := 0.5*(d + w/d);
  874. d := 0.5*(d + w/d);
  875. result := d;
  876. end;
  877. end;
  878. {$endif}
  879. {$ifndef FPC_SYSTEM_HAS_EXP}
  880. {$ifdef SUPPORT_DOUBLE}
  881. {
  882. This code was translated from uclib code, the original code
  883. had the following copyright notice:
  884. *
  885. * ====================================================
  886. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  887. *
  888. * Developed at SunPro, a Sun Microsystems, Inc. business.
  889. * Permission to use, copy, modify, and distribute this
  890. * software is freely granted, provided that this notice
  891. * is preserved.
  892. * ====================================================
  893. *}
  894. {*
  895. * Returns the exponential of x.
  896. *
  897. * Method
  898. * 1. Argument reduction:
  899. * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
  900. * Given x, find r and integer k such that
  901. *
  902. * x = k*ln2 + r, |r| <= 0.5*ln2.
  903. *
  904. * Here r will be represented as r = hi-lo for better
  905. * accuracy.
  906. *
  907. * 2. Approximation of exp(r) by a special rational function on
  908. * the interval [0,0.34658]:
  909. * Write
  910. * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
  911. * We use a special Reme algorithm on [0,0.34658] to generate
  912. * a polynomial of degree 5 to approximate R. The maximum error
  913. * of this polynomial approximation is bounded by 2**-59. In
  914. * other words,
  915. * R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5
  916. * (where z=r*r, and the values of P1 to P5 are listed below)
  917. * and
  918. * | 5 | -59
  919. * | 2.0+P1*z+...+P5*z - R(z) | <= 2
  920. * | |
  921. * The computation of exp(r) thus becomes
  922. * 2*r
  923. * exp(r) = 1 + -------
  924. * R - r
  925. * r*R1(r)
  926. * = 1 + r + ----------- (for better accuracy)
  927. * 2 - R1(r)
  928. * where
  929. 2 4 10
  930. * R1(r) = r - (P1*r + P2*r + ... + P5*r ).
  931. *
  932. * 3. Scale back to obtain exp(x):
  933. * From step 1, we have
  934. * exp(x) = 2^k * exp(r)
  935. *
  936. * Special cases:
  937. * exp(INF) is INF, exp(NaN) is NaN;
  938. * exp(-INF) is 0, and
  939. * for finite argument, only exp(0)=1 is exact.
  940. *
  941. * Accuracy:
  942. * according to an error analysis, the error is always less than
  943. * 1 ulp (unit in the last place).
  944. *
  945. * Misc. info.
  946. * For IEEE double
  947. * if x > 7.09782712893383973096e+02 then exp(x) overflow
  948. * if x < -7.45133219101941108420e+02 then exp(x) underflow
  949. *
  950. * Constants:
  951. * The hexadecimal values are the intended ones for the following
  952. * constants. The decimal values may be used, provided that the
  953. * compiler will convert from decimal to binary accurately enough
  954. * to produce the hexadecimal values shown.
  955. *
  956. }
  957. function fpc_exp_real(d: ValReal):ValReal;compilerproc;
  958. const
  959. halF : array[0..1] of double = (0.5,-0.5);
  960. twom1000: double = 9.33263618503218878990e-302; { 2**-1000=0x01700000,0}
  961. o_threshold: double = 7.09782712893383973096e+02; { 0x40862E42, 0xFEFA39EF }
  962. u_threshold: double = -7.45133219101941108420e+02; { 0xc0874910, 0xD52D3051 }
  963. ln2HI : array[0..1] of double = ( 6.93147180369123816490e-01, { 0x3fe62e42, 0xfee00000 }
  964. -6.93147180369123816490e-01); { 0xbfe62e42, 0xfee00000 }
  965. ln2LO : array[0..1] of double = (1.90821492927058770002e-10, { 0x3dea39ef, 0x35793c76 }
  966. -1.90821492927058770002e-10); { 0xbdea39ef, 0x35793c76 }
  967. invln2: double = 1.44269504088896338700e+00; { 0x3ff71547, 0x652b82fe }
  968. P1: double = 1.66666666666666019037e-01; { 0x3FC55555, 0x5555553E }
  969. P2: double = -2.77777777770155933842e-03; { 0xBF66C16C, 0x16BEBD93 }
  970. P3: double = 6.61375632143793436117e-05; { 0x3F11566A, 0xAF25DE2C }
  971. P4: double = -1.65339022054652515390e-06; { 0xBEBBBD41, 0xC5D26BF1 }
  972. P5: double = 4.13813679705723846039e-08; { 0x3E663769, 0x72BEA4D0 }
  973. var
  974. c,hi,lo,t,y : double;
  975. k,xsb : longint;
  976. hx,hy,lx : dword;
  977. begin
  978. hi:=0.0;
  979. lo:=0.0;
  980. k:=0;
  981. hx:=float64high(d);
  982. xsb := (hx shr 31) and 1; { sign bit of d }
  983. hx := hx and $7fffffff; { high word of |d| }
  984. { filter out non-finite argument }
  985. if hx >= $40862E42 then
  986. begin { if |d|>=709.78... }
  987. if hx >= $7ff00000 then
  988. begin
  989. lx:=float64low(d);
  990. if ((hx and $fffff) or lx)<>0 then
  991. begin
  992. result:=d+d; { NaN }
  993. exit;
  994. end
  995. else
  996. begin
  997. if xsb=0 then
  998. result:=d
  999. else
  1000. result:=0.0; { exp(+-inf)=(inf,0) }
  1001. exit;
  1002. end;
  1003. end;
  1004. if d > o_threshold then begin
  1005. result:=huge*huge; { overflow }
  1006. exit;
  1007. end;
  1008. if d < u_threshold then begin
  1009. result:=twom1000*twom1000; { underflow }
  1010. exit;
  1011. end;
  1012. end;
  1013. { argument reduction }
  1014. if hx > $3fd62e42 then
  1015. begin { if |d| > 0.5 ln2 }
  1016. if hx < $3FF0A2B2 then { and |d| < 1.5 ln2 }
  1017. begin
  1018. hi := d-ln2HI[xsb];
  1019. lo:=ln2LO[xsb];
  1020. k := 1-xsb-xsb;
  1021. end
  1022. else
  1023. begin
  1024. k := trunc(invln2*d+halF[xsb]);
  1025. t := k;
  1026. hi := d - t*ln2HI[0]; { t*ln2HI is exact here }
  1027. lo := t*ln2LO[0];
  1028. end;
  1029. d := hi - lo;
  1030. end
  1031. else if hx < $3e300000 then
  1032. begin { when |d|<2**-28 }
  1033. if huge+d>one then
  1034. begin
  1035. result:=one+d;{ trigger inexact }
  1036. exit;
  1037. end;
  1038. end
  1039. else
  1040. k := 0;
  1041. { d is now in primary range }
  1042. t:=d*d;
  1043. c:=d - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
  1044. if k=0 then
  1045. begin
  1046. result:=one-((d*c)/(c-2.0)-d);
  1047. exit;
  1048. end
  1049. else
  1050. y := one-((lo-(d*c)/(2.0-c))-hi);
  1051. if k >= -1021 then
  1052. begin
  1053. hy:=float64high(y);
  1054. float64sethigh(y,longint(hy)+(k shl 20)); { add k to y's exponent }
  1055. result:=y;
  1056. end
  1057. else
  1058. begin
  1059. hy:=float64high(y);
  1060. float64sethigh(y,longint(hy)+((k+1000) shl 20)); { add k to y's exponent }
  1061. result:=y*twom1000;
  1062. end;
  1063. end;
  1064. {$else SUPPORT_DOUBLE}
  1065. function fpc_exp_real(d: ValReal):ValReal;compilerproc;
  1066. {*****************************************************************}
  1067. { Exponential Function }
  1068. {*****************************************************************}
  1069. { }
  1070. { SYNOPSIS: }
  1071. { }
  1072. { double x, y, exp(); }
  1073. { }
  1074. { y = exp( x ); }
  1075. { }
  1076. { DESCRIPTION: }
  1077. { }
  1078. { Returns e (2.71828...) raised to the x power. }
  1079. { }
  1080. { Range reduction is accomplished by separating the argument }
  1081. { into an integer k and fraction f such that }
  1082. { }
  1083. { x k f }
  1084. { e = 2 e. }
  1085. { }
  1086. { A Pade' form of degree 2/3 is used to approximate exp(f)- 1 }
  1087. { in the basic range [-0.5 ln 2, 0.5 ln 2]. }
  1088. {*****************************************************************}
  1089. const P : array[0..2] of Real = (
  1090. 1.26183092834458542160E-4,
  1091. 3.02996887658430129200E-2,
  1092. 1.00000000000000000000E0);
  1093. Q : array[0..3] of Real = (
  1094. 3.00227947279887615146E-6,
  1095. 2.52453653553222894311E-3,
  1096. 2.27266044198352679519E-1,
  1097. 2.00000000000000000005E0);
  1098. C1 = 6.9335937500000000000E-1;
  1099. C2 = 2.1219444005469058277E-4;
  1100. var n : Integer;
  1101. px, qx, xx : Real;
  1102. begin
  1103. if( d > MAXLOG) then
  1104. float_raise(float_flag_overflow)
  1105. else
  1106. if( d < MINLOG ) then
  1107. begin
  1108. float_raise(float_flag_underflow);
  1109. result:=0; { Result if underflow masked }
  1110. end
  1111. else
  1112. begin
  1113. { Express e**x = e**g 2**n }
  1114. { = e**g e**( n loge(2) ) }
  1115. { = e**( g + n loge(2) ) }
  1116. px := d * LOG2E;
  1117. qx := Trunc( px + 0.5 ); { Trunc() truncates toward -infinity. }
  1118. n := Trunc(qx);
  1119. d := d - qx * C1;
  1120. d := d + qx * C2;
  1121. { rational approximation for exponential }
  1122. { of the fractional part: }
  1123. { e**x - 1 = 2x P(x**2)/( Q(x**2) - P(x**2) ) }
  1124. xx := d * d;
  1125. px := d * ((P[0] * xx + P[1]) * xx + P[2]);
  1126. d := px/( (((Q[0] * xx + Q[1]) * xx + Q[2]) * xx + Q[3]) - px );
  1127. d := 2 * d + 1.0;
  1128. d := ldexp( d, n );
  1129. result := d;
  1130. end;
  1131. end;
  1132. {$endif SUPPORT_DOUBLE}
  1133. {$endif}
  1134. {$ifndef FPC_SYSTEM_HAS_ROUND}
  1135. function fpc_round_real(d : ValReal) : int64;compilerproc;
  1136. var
  1137. tmp: double;
  1138. j0: longint;
  1139. hx: longword;
  1140. sx: longint;
  1141. const
  1142. H2_52: array[0..1] of double = (
  1143. 4.50359962737049600000e+15,
  1144. -4.50359962737049600000e+15
  1145. );
  1146. Begin
  1147. { This basically calculates trunc((d+2**52)-2**52) }
  1148. hx:=float64high(d);
  1149. j0:=((hx shr 20) and $7ff) - $3ff;
  1150. sx:=hx shr 31;
  1151. hx:=(hx and $fffff) or $100000;
  1152. if j0>=52 then { No fraction bits, already integer }
  1153. begin
  1154. if j0>=63 then { Overflow, let trunc() raise an exception }
  1155. exit(trunc(d)) { and/or return +/-MaxInt64 if it's masked }
  1156. else
  1157. result:=((int64(hx) shl 32) or dword(float64low(d))) shl (j0-52);
  1158. end
  1159. else
  1160. begin
  1161. { Rounding happens here. It is important that the expression is not
  1162. optimized by selecting a larger type to store 'tmp'. }
  1163. tmp:=H2_52[sx]+d;
  1164. d:=tmp-H2_52[sx];
  1165. hx:=float64high(d);
  1166. j0:=((hx shr 20) and $7ff)-$3ff;
  1167. hx:=(hx and $fffff) or $100000;
  1168. if j0<=20 then
  1169. begin
  1170. if j0<0 then
  1171. exit(0)
  1172. else { more than 32 fraction bits, low dword discarded }
  1173. result:=hx shr (20-j0);
  1174. end
  1175. else
  1176. result:=(int64(hx) shl (j0-20)) or (float64low(d) shr (52-j0));
  1177. end;
  1178. if sx<>0 then
  1179. result:=-result;
  1180. end;
  1181. {$endif FPC_SYSTEM_HAS_ROUND}
  1182. {$ifndef FPC_SYSTEM_HAS_LN}
  1183. function fpc_ln_real(d:ValReal):ValReal;compilerproc;
  1184. {
  1185. This code was translated from uclib code, the original code
  1186. had the following copyright notice:
  1187. *
  1188. * ====================================================
  1189. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  1190. *
  1191. * Developed at SunPro, a Sun Microsystems, Inc. business.
  1192. * Permission to use, copy, modify, and distribute this
  1193. * software is freely granted, provided that this notice
  1194. * is preserved.
  1195. * ====================================================
  1196. *}
  1197. {*****************************************************************}
  1198. { Natural Logarithm }
  1199. {*****************************************************************}
  1200. {*
  1201. * SYNOPSIS:
  1202. *
  1203. * double x, y, log();
  1204. *
  1205. * y = ln( x );
  1206. *
  1207. * DESCRIPTION:
  1208. *
  1209. * Returns the base e (2.718...) logarithm of x.
  1210. *
  1211. * Method :
  1212. * 1. Argument Reduction: find k and f such that
  1213. * x = 2^k * (1+f),
  1214. * where sqrt(2)/2 < 1+f < sqrt(2) .
  1215. *
  1216. * 2. Approximation of log(1+f).
  1217. * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
  1218. * = 2s + 2/3 s**3 + 2/5 s**5 + .....,
  1219. * = 2s + s*R
  1220. * We use a special Reme algorithm on [0,0.1716] to generate
  1221. * a polynomial of degree 14 to approximate R The maximum error
  1222. * of this polynomial approximation is bounded by 2**-58.45. In
  1223. * other words,
  1224. * 2 4 6 8 10 12 14
  1225. * R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s
  1226. * (the values of Lg1 to Lg7 are listed in the program)
  1227. * and
  1228. * | 2 14 | -58.45
  1229. * | Lg1*s +...+Lg7*s - R(z) | <= 2
  1230. * | |
  1231. * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
  1232. * In order to guarantee error in log below 1ulp, we compute log
  1233. * by
  1234. * log(1+f) = f - s*(f - R) (if f is not too large)
  1235. * log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy)
  1236. *
  1237. * 3. Finally, log(x) = k*ln2 + log(1+f).
  1238. * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
  1239. * Here ln2 is split into two floating point number:
  1240. * ln2_hi + ln2_lo,
  1241. * where n*ln2_hi is always exact for |n| < 2000.
  1242. *
  1243. * Special cases:
  1244. * log(x) is NaN with signal if x < 0 (including -INF) ;
  1245. * log(+INF) is +INF; log(0) is -INF with signal;
  1246. * log(NaN) is that NaN with no signal.
  1247. *
  1248. * Accuracy:
  1249. * according to an error analysis, the error is always less than
  1250. * 1 ulp (unit in the last place).
  1251. *}
  1252. const
  1253. ln2_hi: double = 6.93147180369123816490e-01; { 3fe62e42 fee00000 }
  1254. ln2_lo: double = 1.90821492927058770002e-10; { 3dea39ef 35793c76 }
  1255. two54: double = 1.80143985094819840000e+16; { 43500000 00000000 }
  1256. Lg1: double = 6.666666666666735130e-01; { 3FE55555 55555593 }
  1257. Lg2: double = 3.999999999940941908e-01; { 3FD99999 9997FA04 }
  1258. Lg3: double = 2.857142874366239149e-01; { 3FD24924 94229359 }
  1259. Lg4: double = 2.222219843214978396e-01; { 3FCC71C5 1D8E78AF }
  1260. Lg5: double = 1.818357216161805012e-01; { 3FC74664 96CB03DE }
  1261. Lg6: double = 1.531383769920937332e-01; { 3FC39A09 D078C69F }
  1262. Lg7: double = 1.479819860511658591e-01; { 3FC2F112 DF3E5244 }
  1263. var
  1264. hfsq,f,s,z,R,w,t1,t2,dk: double;
  1265. k,hx,i,j: longint;
  1266. lx: longword;
  1267. {$push}
  1268. { if we have to check manually fpu exceptions, then force the exit statements here to
  1269. throw one }
  1270. {$CHECKFPUEXCEPTIONS+}
  1271. { turn off fastmath as it converts zero/zero into 1 and thus not raising an exception }
  1272. {$OPTIMIZATION NOFASTMATH}
  1273. begin
  1274. hx := float64high(d);
  1275. lx := float64low(d);
  1276. k := 0;
  1277. if (hx < $00100000) then { x < 2**-1022 }
  1278. begin
  1279. if (((hx and $7fffffff) or longint(lx))=0) then
  1280. exit(-two54/zero); { log(+-0)=-inf }
  1281. if (hx<0) then
  1282. exit(zero/zero); { log(-#) = NaN }
  1283. dec(k, 54); d := d * two54; { subnormal number, scale up x }
  1284. hx := float64high(d);
  1285. end;
  1286. if (hx >= $7ff00000) then
  1287. exit(d+d);
  1288. {$pop}
  1289. inc(k, (hx shr 20)-1023);
  1290. hx := hx and $000fffff;
  1291. i := (hx + $95f64) and $100000;
  1292. float64sethigh(d,hx or (i xor $3ff00000)); { normalize x or x/2 }
  1293. inc(k, (i shr 20));
  1294. f := d-1.0;
  1295. if (($000fffff and (2+hx))<3) then { |f| < 2**-20 }
  1296. begin
  1297. if (f=zero) then
  1298. begin
  1299. if (k=0) then
  1300. exit(zero)
  1301. else
  1302. begin
  1303. dk := k;
  1304. exit(dk*ln2_hi+dk*ln2_lo);
  1305. end;
  1306. end;
  1307. R := f*f*(0.5-0.33333333333333333*f);
  1308. if (k=0) then
  1309. exit(f-R)
  1310. else
  1311. begin
  1312. dk := k;
  1313. exit(dk*ln2_hi-((R-dk*ln2_lo)-f));
  1314. end;
  1315. end;
  1316. s := f/(2.0+f);
  1317. dk := k;
  1318. z := s*s;
  1319. i := hx-$6147a;
  1320. w := z*z;
  1321. j := $6b851-hx;
  1322. t1 := w*(Lg2+w*(Lg4+w*Lg6));
  1323. t2 := z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7)));
  1324. i := i or j;
  1325. R := t2+t1;
  1326. if (i>0) then
  1327. begin
  1328. hfsq := 0.5*f*f;
  1329. if (k=0) then
  1330. result := f-(hfsq-s*(hfsq+R))
  1331. else
  1332. result := dk*ln2_hi-((hfsq-(s*(hfsq+R)+dk*ln2_lo))-f);
  1333. end
  1334. else
  1335. begin
  1336. if (k=0) then
  1337. result := f-s*(f-R)
  1338. else
  1339. result := dk*ln2_hi-((s*(f-R)-dk*ln2_lo)-f);
  1340. end;
  1341. end;
  1342. {$endif}
  1343. {$ifndef FPC_SYSTEM_HAS_SIN}
  1344. function fpc_Sin_real(d:ValReal):ValReal;compilerproc;
  1345. {*****************************************************************}
  1346. { Circular Sine }
  1347. {*****************************************************************}
  1348. { }
  1349. { SYNOPSIS: }
  1350. { }
  1351. { double x, y, sin(); }
  1352. { }
  1353. { y = sin( x ); }
  1354. { }
  1355. { DESCRIPTION: }
  1356. { }
  1357. { Range reduction is into intervals of pi/4. The reduction }
  1358. { error is nearly eliminated by contriving an extended }
  1359. { precision modular arithmetic. }
  1360. { }
  1361. { Two polynomial approximating functions are employed. }
  1362. { Between 0 and pi/4 the sine is approximated by }
  1363. { x + x**3 P(x**2). }
  1364. { Between pi/4 and pi/2 the cosine is represented as }
  1365. { 1 - x**2 Q(x**2). }
  1366. {*****************************************************************}
  1367. var z, zz : Real;
  1368. j : sizeint;
  1369. begin
  1370. { This seemingly useless condition ensures that sin(-0.0)=-0.0 }
  1371. if (d=0.0) then
  1372. exit(d);
  1373. j := rem_pio2(d,z);
  1374. zz := z * z;
  1375. if j and 1<>0 then { j and 3 = 1 or j and 3 = 3 }
  1376. result := 1.0 - zz * 0.5 + zz * zz * (((((coscof[0] * zz + coscof[1]) * zz + coscof[2]) * zz + coscof[3]) * zz + coscof[4]) * zz + coscof[5])
  1377. else
  1378. result := z + zz * z * (((((sincof[0] * zz + sincof[1]) * zz + sincof[2]) * zz + sincof[3]) * zz + sincof[4]) * zz + sincof[5]);
  1379. if j and 2<>0 then { j and 3 = 2 or j and 3 = 3 }
  1380. result := -result;
  1381. end;
  1382. {$endif}
  1383. {$ifndef FPC_SYSTEM_HAS_COS}
  1384. function fpc_Cos_real(d:ValReal):ValReal;compilerproc;
  1385. {*****************************************************************}
  1386. { Circular cosine }
  1387. {*****************************************************************}
  1388. { }
  1389. { Circular cosine }
  1390. { }
  1391. { SYNOPSIS: }
  1392. { }
  1393. { double x, y, cos(); }
  1394. { }
  1395. { y = cos( x ); }
  1396. { }
  1397. { DESCRIPTION: }
  1398. { }
  1399. { Range reduction is into intervals of pi/4. The reduction }
  1400. { error is nearly eliminated by contriving an extended }
  1401. { precision modular arithmetic. }
  1402. { }
  1403. { Two polynomial approximating functions are employed. }
  1404. { Between 0 and pi/4 the cosine is approximated by }
  1405. { 1 - x**2 Q(x**2). }
  1406. { Between pi/4 and pi/2 the sine is represented as }
  1407. { x + x**3 P(x**2). }
  1408. {*****************************************************************}
  1409. var y, z, zz : Real;
  1410. j : sizeint;
  1411. begin
  1412. j := rem_pio2(d,z);
  1413. zz := z * z;
  1414. if j and 1<>0 then { j and 3 = 1 or j and 3 = 3 }
  1415. result := z + zz * z * (((((sincof[0] * zz + sincof[1]) * zz + sincof[2]) * zz + sincof[3]) * zz + sincof[4]) * zz + sincof[5])
  1416. else
  1417. result := 1.0 - zz * 0.5 + zz * zz * (((((coscof[0] * zz + coscof[1]) * zz + coscof[2]) * zz + coscof[3]) * zz + coscof[4]) * zz + coscof[5]);
  1418. if (j+1) and 2<>0 then { j and 3 = 1 or j and 3 = 2 }
  1419. result := -result;
  1420. end;
  1421. {$endif}
  1422. {$ifndef FPC_SYSTEM_HAS_ARCTAN}
  1423. function fpc_ArcTan_real(d:ValReal):ValReal;compilerproc;
  1424. {
  1425. This code was translated from uclibc code, the original code
  1426. had the following copyright notice:
  1427. *
  1428. * ====================================================
  1429. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  1430. *
  1431. * Developed at SunPro, a Sun Microsystems, Inc. business.
  1432. * Permission to use, copy, modify, and distribute this
  1433. * software is freely granted, provided that this notice
  1434. * is preserved.
  1435. * ====================================================
  1436. *}
  1437. {********************************************************************}
  1438. { Inverse circular tangent (arctangent) }
  1439. {********************************************************************}
  1440. { }
  1441. { SYNOPSIS: }
  1442. { }
  1443. { double x, y, atan(); }
  1444. { }
  1445. { y = atan( x ); }
  1446. { }
  1447. { DESCRIPTION: }
  1448. { }
  1449. { Returns radian angle between -pi/2 and +pi/2 whose tangent }
  1450. { is x. }
  1451. { }
  1452. { Method }
  1453. { 1. Reduce x to positive by atan(x) = -atan(-x). }
  1454. { 2. According to the integer k=4t+0.25 chopped, t=x, the argument }
  1455. { is further reduced to one of the following intervals and the }
  1456. { arctangent of t is evaluated by the corresponding formula: }
  1457. { }
  1458. { [0,7/16] atan(x) = t-t^3*(a1+t^2*(a2+...(a10+t^2*a11)...) }
  1459. { [7/16,11/16] atan(x) = atan(1/2) + atan( (t-0.5)/(1+t/2) ) }
  1460. { [11/16.19/16] atan(x) = atan( 1 ) + atan( (t-1)/(1+t) ) }
  1461. { [19/16,39/16] atan(x) = atan(3/2) + atan( (t-1.5)/(1+1.5t) ) }
  1462. { [39/16,INF] atan(x) = atan(INF) + atan( -1/t ) }
  1463. {********************************************************************}
  1464. const
  1465. atanhi: array [0..3] of double = (
  1466. 4.63647609000806093515e-01, { atan(0.5)hi 0x3FDDAC67, 0x0561BB4F }
  1467. 7.85398163397448278999e-01, { atan(1.0)hi 0x3FE921FB, 0x54442D18 }
  1468. 9.82793723247329054082e-01, { atan(1.5)hi 0x3FEF730B, 0xD281F69B }
  1469. 1.57079632679489655800e+00 { atan(inf)hi 0x3FF921FB, 0x54442D18 }
  1470. );
  1471. atanlo: array [0..3] of double = (
  1472. 2.26987774529616870924e-17, { atan(0.5)lo 0x3C7A2B7F, 0x222F65E2 }
  1473. 3.06161699786838301793e-17, { atan(1.0)lo 0x3C81A626, 0x33145C07 }
  1474. 1.39033110312309984516e-17, { atan(1.5)lo 0x3C700788, 0x7AF0CBBD }
  1475. 6.12323399573676603587e-17 { atan(inf)lo 0x3C91A626, 0x33145C07 }
  1476. );
  1477. aT: array[0..10] of double = (
  1478. 3.33333333333329318027e-01, { 0x3FD55555, 0x5555550D }
  1479. -1.99999999998764832476e-01, { 0xBFC99999, 0x9998EBC4 }
  1480. 1.42857142725034663711e-01, { 0x3FC24924, 0x920083FF }
  1481. -1.11111104054623557880e-01, { 0xBFBC71C6, 0xFE231671 }
  1482. 9.09088713343650656196e-02, { 0x3FB745CD, 0xC54C206E }
  1483. -7.69187620504482999495e-02, { 0xBFB3B0F2, 0xAF749A6D }
  1484. 6.66107313738753120669e-02, { 0x3FB10D66, 0xA0D03D51 }
  1485. -5.83357013379057348645e-02, { 0xBFADDE2D, 0x52DEFD9A }
  1486. 4.97687799461593236017e-02, { 0x3FA97B4B, 0x24760DEB }
  1487. -3.65315727442169155270e-02, { 0xBFA2B444, 0x2C6A6C2F }
  1488. 1.62858201153657823623e-02 { 0x3F90AD3A, 0xE322DA11 }
  1489. );
  1490. var
  1491. w,s1,s2,z: double;
  1492. ix,hx,id: longint;
  1493. low: longword;
  1494. begin
  1495. hx:=float64high(d);
  1496. ix := hx and $7fffffff;
  1497. if (ix>=$44100000) then { if |x| >= 2^66 }
  1498. begin
  1499. low:=float64low(d);
  1500. if (ix > $7ff00000) or ((ix = $7ff00000) and (low<>0)) then
  1501. exit(d+d); { NaN }
  1502. if (hx>0) then
  1503. exit(atanhi[3]+atanlo[3])
  1504. else
  1505. exit(-atanhi[3]-atanlo[3]);
  1506. end;
  1507. if (ix < $3fdc0000) then { |x| < 0.4375 }
  1508. begin
  1509. if (ix < $3e200000) then { |x| < 2^-29 }
  1510. begin
  1511. if (huge+d>one) then exit(d); { raise inexact }
  1512. end;
  1513. id := -1;
  1514. end
  1515. else
  1516. begin
  1517. d := abs(d);
  1518. if (ix < $3ff30000) then { |x| < 1.1875 }
  1519. begin
  1520. if (ix < $3fe60000) then { 7/16 <=|x|<11/16 }
  1521. begin
  1522. id := 0; d := (2.0*d-one)/(2.0+d);
  1523. end
  1524. else { 11/16<=|x|< 19/16 }
  1525. begin
  1526. id := 1; d := (d-one)/(d+one);
  1527. end
  1528. end
  1529. else
  1530. begin
  1531. if (ix < $40038000) then { |x| < 2.4375 }
  1532. begin
  1533. id := 2; d := (d-1.5)/(one+1.5*d);
  1534. end
  1535. else { 2.4375 <= |x| < 2^66 }
  1536. begin
  1537. id := 3; d := -1.0/d;
  1538. end;
  1539. end;
  1540. end;
  1541. { end of argument reduction }
  1542. z := d*d;
  1543. w := z*z;
  1544. { break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly }
  1545. s1 := z*(aT[0]+w*(aT[2]+w*(aT[4]+w*(aT[6]+w*(aT[8]+w*aT[10])))));
  1546. s2 := w*(aT[1]+w*(aT[3]+w*(aT[5]+w*(aT[7]+w*aT[9]))));
  1547. if (id<0) then
  1548. result := d - d*(s1+s2)
  1549. else
  1550. begin
  1551. z := atanhi[id] - ((d*(s1+s2) - atanlo[id]) - d);
  1552. if hx<0 then
  1553. result := -z
  1554. else
  1555. result := z;
  1556. end;
  1557. end;
  1558. {$endif}
  1559. {$ifndef FPC_SYSTEM_HAS_FRAC}
  1560. {$push}
  1561. {$ifndef VER3_2}
  1562. { if we have to check manually fpu exceptions, then force the result assignment statement here to
  1563. throw one }
  1564. {$CHECKFPUEXCEPTIONS+}
  1565. { turn off fastmath as it converts zero/zero into 0 and thus not raising an exception }
  1566. {$OPTIMIZATION NOFASTMATH}
  1567. {$endif VER3_2}
  1568. function fpc_frac_real(d : ValReal) : ValReal;compilerproc;
  1569. begin
  1570. { Nan or +/-Inf }
  1571. if (float64high(d) and $7ff00000)=$7ff00000 then
  1572. { return NaN }
  1573. {$ifdef VER3_2}
  1574. { fix bootstrapping with 3.2 on arm-linux }
  1575. result := (d-d)/0.0
  1576. {$else VER3_2}
  1577. result := zero/zero
  1578. {$endif VER3_2}
  1579. else
  1580. result := d - Int(d);
  1581. end;
  1582. {$pop}
  1583. {$endif}
  1584. {$ifdef FPC_INCLUDE_SOFTWARE_INT64_TO_DOUBLE}
  1585. {$ifndef FPC_SYSTEM_HAS_QWORD_TO_DOUBLE}
  1586. function fpc_qword_to_double(q : qword): double; compilerproc;
  1587. begin
  1588. result:=dword(q and $ffffffff)+dword(q shr 32)*double(4294967296.0);
  1589. end;
  1590. {$endif FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  1591. {$ifndef FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  1592. function fpc_int64_to_double(i : int64): double; compilerproc;
  1593. begin
  1594. result:=dword(i and $ffffffff)+longint(i shr 32)*double(4294967296.0);
  1595. end;
  1596. {$endif FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  1597. {$endif FPC_INCLUDE_SOFTWARE_INT64_TO_DOUBLE}
  1598. {$ifdef SUPPORT_DOUBLE}
  1599. {****************************************************************************
  1600. Helper routines to support old TP styled reals
  1601. ****************************************************************************}
  1602. {$ifndef FPC_SYSTEM_HAS_REAL2DOUBLE}
  1603. function real2double(r : real48) : double;
  1604. var
  1605. res : array[0..7] of byte;
  1606. exponent : word;
  1607. begin
  1608. { check for zero }
  1609. if r[0]=0 then
  1610. begin
  1611. real2double:=0.0;
  1612. exit;
  1613. end;
  1614. { copy mantissa }
  1615. res[0]:=0;
  1616. res[1]:=r[1] shl 5;
  1617. res[2]:=(r[1] shr 3) or (r[2] shl 5);
  1618. res[3]:=(r[2] shr 3) or (r[3] shl 5);
  1619. res[4]:=(r[3] shr 3) or (r[4] shl 5);
  1620. res[5]:=(r[4] shr 3) or (r[5] and $7f) shl 5;
  1621. res[6]:=(r[5] and $7f) shr 3;
  1622. { copy exponent }
  1623. { correct exponent: }
  1624. exponent:=(word(r[0])+(1023-129));
  1625. res[6]:=res[6] or ((exponent and $f) shl 4);
  1626. res[7]:=exponent shr 4;
  1627. { set sign }
  1628. res[7]:=res[7] or (r[5] and $80);
  1629. real2double:=double(res);
  1630. end;
  1631. {$endif FPC_SYSTEM_HAS_REAL2DOUBLE}
  1632. {$endif SUPPORT_DOUBLE}
  1633. {$ifdef SUPPORT_EXTENDED}
  1634. { fast 10^n routine }
  1635. function FPower10(val: Extended; Power: Longint): Extended;
  1636. const
  1637. pow32 : array[0..31] of extended =
  1638. (
  1639. 1e0,1e1,1e2,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,
  1640. 1e11,1e12,1e13,1e14,1e15,1e16,1e17,1e18,1e19,1e20,
  1641. 1e21,1e22,1e23,1e24,1e25,1e26,1e27,1e28,1e29,1e30,
  1642. 1e31
  1643. );
  1644. pow512 : array[0..15] of extended =
  1645. (
  1646. 1,1e32,1e64,1e96,1e128,1e160,1e192,1e224,
  1647. 1e256,1e288,1e320,1e352,1e384,1e416,1e448,
  1648. 1e480
  1649. );
  1650. pow4096 : array[0..9] of extended =
  1651. (1,1e512,1e1024,1e1536,
  1652. 1e2048,1e2560,1e3072,1e3584,
  1653. 1e4096,1e4608
  1654. );
  1655. negpow32 : array[0..31] of extended =
  1656. (
  1657. 1e-0,1e-1,1e-2,1e-3,1e-4,1e-5,1e-6,1e-7,1e-8,1e-9,1e-10,
  1658. 1e-11,1e-12,1e-13,1e-14,1e-15,1e-16,1e-17,1e-18,1e-19,1e-20,
  1659. 1e-21,1e-22,1e-23,1e-24,1e-25,1e-26,1e-27,1e-28,1e-29,1e-30,
  1660. 1e-31
  1661. );
  1662. negpow512 : array[0..15] of extended =
  1663. (
  1664. 0,1e-32,1e-64,1e-96,1e-128,1e-160,1e-192,1e-224,
  1665. 1e-256,1e-288,1e-320,1e-352,1e-384,1e-416,1e-448,
  1666. 1e-480
  1667. );
  1668. negpow4096 : array[0..9] of extended =
  1669. (
  1670. 0,1e-512,1e-1024,1e-1536,
  1671. 1e-2048,1e-2560,1e-3072,1e-3584,
  1672. 1e-4096,1e-4608
  1673. );
  1674. begin
  1675. if Power<0 then
  1676. begin
  1677. Power:=-Power;
  1678. result:=val*negpow32[Power and $1f];
  1679. power:=power shr 5;
  1680. if power<>0 then
  1681. begin
  1682. result:=result*negpow512[Power and $f];
  1683. power:=power shr 4;
  1684. if power<>0 then
  1685. begin
  1686. if power<=9 then
  1687. result:=result*negpow4096[Power]
  1688. else
  1689. result:=1.0/0.0;
  1690. end;
  1691. end;
  1692. end
  1693. else
  1694. begin
  1695. result:=val*pow32[Power and $1f];
  1696. power:=power shr 5;
  1697. if power<>0 then
  1698. begin
  1699. result:=result*pow512[Power and $f];
  1700. power:=power shr 4;
  1701. if power<>0 then
  1702. begin
  1703. if power<=9 then
  1704. result:=result*pow4096[Power]
  1705. else
  1706. result:=1.0/0.0;
  1707. end;
  1708. end;
  1709. end;
  1710. end;
  1711. {$endif SUPPORT_EXTENDED}
  1712. {$if defined(SUPPORT_EXTENDED) or defined(FPC_SOFT_FPUX80)}
  1713. function TExtended80Rec.Mantissa(IncludeHiddenBit: Boolean = False) : QWord;
  1714. begin
  1715. if IncludeHiddenbit then
  1716. Result:=Frac
  1717. else
  1718. Result:=Frac and $7fffffffffffffff;
  1719. end;
  1720. function TExtended80Rec.Fraction : Extended;
  1721. begin
  1722. {$ifdef SUPPORT_EXTENDED}
  1723. Result:=system.frac(Value);
  1724. {$else}
  1725. Result:=Frac / Double (1 shl 63) / 2.0;
  1726. {$endif}
  1727. end;
  1728. function TExtended80Rec.Exponent : Longint;
  1729. var
  1730. E: QWord;
  1731. begin
  1732. Result := 0;
  1733. E := GetExp;
  1734. if (0<E) and (E<2*Bias+1) then
  1735. Result:=Exp-Bias
  1736. else if (Exp=0) and (Frac<>0) then
  1737. Result:=-(Bias-1);
  1738. end;
  1739. function TExtended80Rec.GetExp : QWord;
  1740. begin
  1741. Result:=_Exp and $7fff;
  1742. end;
  1743. procedure TExtended80Rec.SetExp(e : QWord);
  1744. begin
  1745. _Exp:=(_Exp and $8000) or (e and $7fff);
  1746. end;
  1747. function TExtended80Rec.GetSign : Boolean;
  1748. begin
  1749. Result:=(_Exp and $8000)<>0;
  1750. end;
  1751. procedure TExtended80Rec.SetSign(s : Boolean);
  1752. begin
  1753. _Exp:=(_Exp and $7ffff) or (ord(s) shl 15);
  1754. end;
  1755. {
  1756. Based on information taken from http://en.wikipedia.org/wiki/Extended_precision#x86_Extended_Precision_Format
  1757. }
  1758. function TExtended80Rec.SpecialType : TFloatSpecial;
  1759. const
  1760. Denormal : array[boolean] of TFloatSpecial = (fsDenormal,fsNDenormal);
  1761. begin
  1762. case Exp of
  1763. 0:
  1764. begin
  1765. if Mantissa=0 then
  1766. begin
  1767. if Sign then
  1768. Result:=fsNZero
  1769. else
  1770. Result:=fsZero
  1771. end
  1772. else
  1773. Result:=Denormal[Sign];
  1774. end;
  1775. $7fff:
  1776. case (Frac shr 62) and 3 of
  1777. 0,1:
  1778. Result:=fsInvalidOp;
  1779. 2:
  1780. begin
  1781. if (Frac and $3fffffffffffffff)=0 then
  1782. begin
  1783. if Sign then
  1784. Result:=fsNInf
  1785. else
  1786. Result:=fsInf;
  1787. end
  1788. else
  1789. Result:=fsNaN;
  1790. end;
  1791. 3:
  1792. Result:=fsNaN;
  1793. end
  1794. else
  1795. begin
  1796. if (Frac and $8000000000000000)=0 then
  1797. Result:=fsInvalidOp
  1798. else
  1799. begin
  1800. if Sign then
  1801. Result:=fsNegative
  1802. else
  1803. Result:=fsPositive;
  1804. end;
  1805. end;
  1806. end;
  1807. end;
  1808. procedure TExtended80Rec.BuildUp(const _Sign: Boolean; const _Mantissa: QWord; const _Exponent: Longint);
  1809. begin
  1810. {$ifdef SUPPORT_EXTENDED}
  1811. Value := 0.0;
  1812. {$else SUPPORT_EXTENDED}
  1813. FillChar(Value, SizeOf(Value),0);
  1814. {$endif SUPPORT_EXTENDED}
  1815. if (_Mantissa=0) and (_Exponent=0) then
  1816. SetExp(0)
  1817. else
  1818. SetExp(_Exponent + Bias);
  1819. SetSign(_Sign);
  1820. Frac := _Mantissa;
  1821. end;
  1822. {$endif SUPPORT_EXTENDED or FPC_SOFT_FPUX80}
  1823. {$ifdef SUPPORT_DOUBLE}
  1824. function TDoubleRec.Mantissa(IncludeHiddenBit: Boolean = False) : QWord;
  1825. begin
  1826. Result:=(Data and $fffffffffffff);
  1827. if (Result=0) and (GetExp=0) then Exit;
  1828. if IncludeHiddenBit then Result := Result or $10000000000000; //add the hidden bit
  1829. end;
  1830. function TDoubleRec.Fraction : ValReal;
  1831. begin
  1832. Result:=system.frac(Value);
  1833. end;
  1834. function TDoubleRec.Exponent : Longint;
  1835. var
  1836. E: QWord;
  1837. begin
  1838. Result := 0;
  1839. E := GetExp;
  1840. if (0<E) and (E<2*Bias+1) then
  1841. Result:=Exp-Bias
  1842. else if (Exp=0) and (Frac<>0) then
  1843. Result:=-(Bias-1);
  1844. end;
  1845. function TDoubleRec.GetExp : QWord;
  1846. begin
  1847. Result:=(Data and $7ff0000000000000) shr 52;
  1848. end;
  1849. procedure TDoubleRec.SetExp(e : QWord);
  1850. begin
  1851. Data:=(Data and $800fffffffffffff) or ((e and $7ff) shl 52);
  1852. end;
  1853. function TDoubleRec.GetSign : Boolean;
  1854. begin
  1855. Result:=(Data and $8000000000000000)<>0;
  1856. end;
  1857. procedure TDoubleRec.SetSign(s : Boolean);
  1858. begin
  1859. Data:=(Data and $7fffffffffffffff) or (QWord(ord(s)) shl 63);
  1860. end;
  1861. function TDoubleRec.GetFrac : QWord;
  1862. begin
  1863. Result := Data and $fffffffffffff;
  1864. end;
  1865. procedure TDoubleRec.SetFrac(e : QWord);
  1866. begin
  1867. Data:=(Data and $fff0000000000000) or (e and $fffffffffffff);
  1868. end;
  1869. {
  1870. Based on information taken from http://en.wikipedia.org/wiki/Double_precision#x86_Extended_Precision_Format
  1871. }
  1872. function TDoubleRec.SpecialType : TFloatSpecial;
  1873. const
  1874. Denormal : array[boolean] of TFloatSpecial = (fsDenormal,fsNDenormal);
  1875. begin
  1876. case Exp of
  1877. 0:
  1878. begin
  1879. if Mantissa=0 then
  1880. begin
  1881. if Sign then
  1882. Result:=fsNZero
  1883. else
  1884. Result:=fsZero
  1885. end
  1886. else
  1887. Result:=Denormal[Sign];
  1888. end;
  1889. $7ff:
  1890. if Mantissa=0 then
  1891. begin
  1892. if Sign then
  1893. Result:=fsNInf
  1894. else
  1895. Result:=fsInf;
  1896. end
  1897. else
  1898. Result:=fsNaN;
  1899. else
  1900. begin
  1901. if Sign then
  1902. Result:=fsNegative
  1903. else
  1904. Result:=fsPositive;
  1905. end;
  1906. end;
  1907. end;
  1908. procedure TDoubleRec.BuildUp(const _Sign: Boolean; const _Mantissa: QWord; const _Exponent: Longint);
  1909. begin
  1910. Value := 0.0;
  1911. SetSign(_Sign);
  1912. if (_Mantissa=0) and (_Exponent=0) then
  1913. Exit //SetExp(0)
  1914. else
  1915. SetExp(_Exponent + Bias);
  1916. SetFrac(_Mantissa and $fffffffffffff); //clear top bit
  1917. end;
  1918. {$endif SUPPORT_DOUBLE}
  1919. {$ifdef SUPPORT_SINGLE}
  1920. function TSingleRec.Mantissa(IncludeHiddenBit: Boolean = False) : QWord;
  1921. begin
  1922. Result:=(Data and $7fffff);
  1923. if (Result=0) and (GetExp=0) then Exit;
  1924. if IncludeHiddenBit then Result:=Result or $800000; //add the hidden bit
  1925. end;
  1926. function TSingleRec.Fraction : ValReal;
  1927. begin
  1928. Result:=system.frac(Value);
  1929. end;
  1930. function TSingleRec.Exponent : Longint;
  1931. var
  1932. E: QWord;
  1933. begin
  1934. Result := 0;
  1935. E := GetExp;
  1936. if (0<E) and (E<2*Bias+1) then
  1937. Result:=Exp-Bias
  1938. else if (Exp=0) and (Frac<>0) then
  1939. Result:=-(Bias-1);
  1940. end;
  1941. function TSingleRec.GetExp : QWord;
  1942. begin
  1943. Result:=(Data and $7f800000) shr 23;
  1944. end;
  1945. procedure TSingleRec.SetExp(e : QWord);
  1946. begin
  1947. Data:=(Data and $807fffff) or ((e and $ff) shl 23);
  1948. end;
  1949. function TSingleRec.GetSign : Boolean;
  1950. begin
  1951. Result:=(Data and $80000000)<>0;
  1952. end;
  1953. procedure TSingleRec.SetSign(s : Boolean);
  1954. begin
  1955. Data:=(Data and $7fffffff) or (DWord(ord(s)) shl 31);
  1956. end;
  1957. function TSingleRec.GetFrac : QWord;
  1958. begin
  1959. Result:=Data and $7fffff;
  1960. end;
  1961. procedure TSingleRec.SetFrac(e : QWord);
  1962. begin
  1963. Data:=(Data and $ff800000) or (e and $7fffff);
  1964. end;
  1965. {
  1966. Based on information taken from http://en.wikipedia.org/wiki/Single_precision#x86_Extended_Precision_Format
  1967. }
  1968. function TSingleRec.SpecialType : TFloatSpecial;
  1969. const
  1970. Denormal : array[boolean] of TFloatSpecial = (fsDenormal,fsNDenormal);
  1971. begin
  1972. case Exp of
  1973. 0:
  1974. begin
  1975. if Mantissa=0 then
  1976. begin
  1977. if Sign then
  1978. Result:=fsNZero
  1979. else
  1980. Result:=fsZero
  1981. end
  1982. else
  1983. Result:=Denormal[Sign];
  1984. end;
  1985. $ff:
  1986. if Mantissa=0 then
  1987. begin
  1988. if Sign then
  1989. Result:=fsNInf
  1990. else
  1991. Result:=fsInf;
  1992. end
  1993. else
  1994. Result:=fsNaN;
  1995. else
  1996. begin
  1997. if Sign then
  1998. Result:=fsNegative
  1999. else
  2000. Result:=fsPositive;
  2001. end;
  2002. end;
  2003. end;
  2004. procedure TSingleRec.BuildUp(const _Sign: Boolean; const _Mantissa: QWord; const _Exponent: Longint);
  2005. begin
  2006. Value := 0.0;
  2007. SetSign(_Sign);
  2008. if (_Mantissa=0) and (_Exponent=0) then
  2009. Exit //SetExp(0)
  2010. else
  2011. SetExp(_Exponent + Bias);
  2012. SetFrac(_Mantissa and $7fffff); //clear top bit
  2013. end;
  2014. {$endif SUPPORT_SINGLE}