genmath.inc 71 KB

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