genmath.inc 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861
  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. type
  34. TabCoef = array[0..6] of Real;
  35. { also necessary for Int() on systems with 64bit floats (JM) }
  36. {$ifndef FPC_SYSTEM_HAS_float64}
  37. {$ifdef ENDIAN_LITTLE}
  38. float64 = record
  39. {$ifndef FPC_DOUBLE_HILO_SWAPPED}
  40. low,high: longint;
  41. {$else}
  42. high,low: longint;
  43. {$endif FPC_DOUBLE_HILO_SWAPPED}
  44. end;
  45. {$else}
  46. float64 = record
  47. {$ifndef FPC_DOUBLE_HILO_SWAPPED}
  48. high,low: longint;
  49. {$else}
  50. low,high: longint;
  51. {$endif FPC_DOUBLE_HILO_SWAPPED}
  52. end;
  53. {$endif}
  54. {$endif FPC_SYSTEM_HAS_float64}
  55. const
  56. PIO2 = 1.57079632679489661923; { pi/2 }
  57. PIO4 = 7.85398163397448309616E-1; { pi/4 }
  58. SQRT2 = 1.41421356237309504880; { sqrt(2) }
  59. SQRTH = 7.07106781186547524401E-1; { sqrt(2)/2 }
  60. LOG2E = 1.4426950408889634073599; { 1/log(2) }
  61. SQ2OPI = 7.9788456080286535587989E-1; { sqrt( 2/pi )}
  62. LOGE2 = 6.93147180559945309417E-1; { log(2) }
  63. LOGSQ2 = 3.46573590279972654709E-1; { log(2)/2 }
  64. THPIO4 = 2.35619449019234492885; { 3*pi/4 }
  65. TWOOPI = 6.36619772367581343075535E-1; { 2/pi }
  66. lossth = 1.073741824e9;
  67. MAXLOG = 8.8029691931113054295988E1; { log(2**127) }
  68. MINLOG = -8.872283911167299960540E1; { log(2**-128) }
  69. DP1 = 7.85398125648498535156E-1;
  70. DP2 = 3.77489470793079817668E-8;
  71. DP3 = 2.69515142907905952645E-15;
  72. {$if not defined(FPC_SYSTEM_HAS_SIN) or not defined(FPC_SYSTEM_HAS_COS)}
  73. const sincof : TabCoef = (
  74. 1.58962301576546568060E-10,
  75. -2.50507477628578072866E-8,
  76. 2.75573136213857245213E-6,
  77. -1.98412698295895385996E-4,
  78. 8.33333333332211858878E-3,
  79. -1.66666666666666307295E-1, 0);
  80. coscof : TabCoef = (
  81. -1.13585365213876817300E-11,
  82. 2.08757008419747316778E-9,
  83. -2.75573141792967388112E-7,
  84. 2.48015872888517045348E-5,
  85. -1.38888888888730564116E-3,
  86. 4.16666666666665929218E-2, 0);
  87. {$endif}
  88. {*
  89. -------------------------------------------------------------------------------
  90. Raises the exceptions specified by `flags'. Floating-point traps can be
  91. defined here if desired. It is currently not possible for such a trap
  92. to substitute a result value. If traps are not implemented, this routine
  93. should be simply `softfloat_exception_flags |= flags;'.
  94. -------------------------------------------------------------------------------
  95. *}
  96. procedure float_raise(i: shortint);
  97. var
  98. pflags: pbyte;
  99. unmasked_flags: byte;
  100. Begin
  101. { taking address of threadvar produces somewhat more compact code }
  102. pflags := @softfloat_exception_flags;
  103. pflags^ := pflags^ or i;
  104. unmasked_flags := pflags^ and (not softfloat_exception_mask);
  105. if (unmasked_flags and float_flag_invalid) <> 0 then
  106. HandleError(207)
  107. else
  108. if (unmasked_flags and float_flag_divbyzero) <> 0 then
  109. HandleError(200)
  110. else
  111. if (unmasked_flags and float_flag_overflow) <> 0 then
  112. HandleError(205)
  113. else
  114. if (unmasked_flags and float_flag_underflow) <> 0 then
  115. HandleError(206)
  116. else
  117. if (unmasked_flags and float_flag_inexact) <> 0 then
  118. HandleError(207);
  119. end;
  120. {$ifdef SUPPORT_DOUBLE}
  121. {$ifndef FPC_HAS_FLOAT64HIGH}
  122. {$define FPC_HAS_FLOAT64HIGH}
  123. function float64high(d: double): longint; inline;
  124. begin
  125. result:=float64(d).high;
  126. end;
  127. procedure float64sethigh(var d: double; l: longint); inline;
  128. begin
  129. float64(d).high:=l;
  130. end;
  131. {$endif FPC_HAS_FLOAT64HIGH}
  132. {$ifndef FPC_HAS_FLOAT64LOW}
  133. {$define FPC_HAS_FLOAT64LOW}
  134. function float64low(d: double): longint; inline;
  135. begin
  136. result:=float64(d).low;
  137. end;
  138. procedure float64setlow(var d: double; l: longint); inline;
  139. begin
  140. float64(d).low:=l;
  141. end;
  142. {$endif FPC_HAS_FLOAT64LOW}
  143. {$endif SUPPORT_DOUBLE}
  144. {$ifndef FPC_SYSTEM_HAS_TRUNC}
  145. {$ifndef FPC_SYSTEM_HAS_float32}
  146. type
  147. float32 = longint;
  148. {$endif FPC_SYSTEM_HAS_float32}
  149. {$ifdef SUPPORT_DOUBLE}
  150. { based on softfloat float64_to_int64_round_to_zero }
  151. function fpc_trunc_real(d : valreal) : int64; compilerproc;
  152. var
  153. aExp, shiftCount : smallint;
  154. aSig : int64;
  155. z : int64;
  156. a: float64;
  157. begin
  158. a:=float64(d);
  159. aSig:=(int64(a.high and $000fffff) shl 32) or longword(a.low);
  160. aExp:=(a.high shr 20) and $7FF;
  161. if aExp<>0 then
  162. aSig:=aSig or $0010000000000000;
  163. shiftCount:= aExp-$433;
  164. if 0<=shiftCount then
  165. begin
  166. if aExp>=$43e then
  167. begin
  168. if (a.high<>$C3E00000) or (a.low<>0) then
  169. begin
  170. float_raise(float_flag_invalid);
  171. if (longint(a.high)>=0) or ((aExp=$7FF) and
  172. (aSig<>$0010000000000000 )) then
  173. begin
  174. result:=$7FFFFFFFFFFFFFFF;
  175. exit;
  176. end;
  177. end;
  178. result:=$8000000000000000;
  179. exit;
  180. end;
  181. z:=aSig shl shiftCount;
  182. end
  183. else
  184. begin
  185. if aExp<$3fe then
  186. begin
  187. result:=0;
  188. exit;
  189. end;
  190. z:=aSig shr -shiftCount;
  191. {
  192. if (aSig shl (shiftCount and 63))<>0 then
  193. float_exception_flags |= float_flag_inexact;
  194. }
  195. end;
  196. if longint(a.high)<0 then
  197. z:=-z;
  198. result:=z;
  199. end;
  200. {$else SUPPORT_DOUBLE}
  201. { based on softfloat float32_to_int64_round_to_zero }
  202. Function fpc_trunc_real( d: valreal ): int64; compilerproc;
  203. Var
  204. a : float32;
  205. aExp, shiftCount : smallint;
  206. aSig : longint;
  207. aSig64, z : int64;
  208. Begin
  209. a := float32(d);
  210. aSig := a and $007FFFFF;
  211. aExp := (a shr 23) and $FF;
  212. shiftCount := aExp - $BE;
  213. if ( 0 <= shiftCount ) then
  214. Begin
  215. if ( a <> Float32($DF000000) ) then
  216. Begin
  217. float_raise( float_flag_invalid );
  218. if ( (longint(a)>=0) or ( ( aExp = $FF ) and (aSig<>0) ) ) then
  219. Begin
  220. result:=$7fffffffffffffff;
  221. exit;
  222. end;
  223. End;
  224. result:=$8000000000000000;
  225. exit;
  226. End
  227. else
  228. if ( aExp <= $7E ) then
  229. Begin
  230. result := 0;
  231. exit;
  232. End;
  233. aSig64 := int64( aSig or $00800000 ) shl 40;
  234. z := aSig64 shr ( - shiftCount );
  235. if ( longint(a)<0 ) then z := - z;
  236. result := z;
  237. End;
  238. {$endif SUPPORT_DOUBLE}
  239. {$endif not FPC_SYSTEM_HAS_TRUNC}
  240. {$ifndef FPC_SYSTEM_HAS_INT}
  241. {$ifdef SUPPORT_DOUBLE}
  242. { straight Pascal translation of the code for __trunc() in }
  243. { the file sysdeps/libm-ieee754/s_trunc.c of glibc (JM) }
  244. function fpc_int_real(d: ValReal): ValReal;compilerproc;
  245. var
  246. i0, j0: longint;
  247. i1: cardinal;
  248. sx: longint;
  249. f64 : float64;
  250. begin
  251. f64:=float64(d);
  252. i0 := f64.high;
  253. i1 := cardinal(f64.low);
  254. sx := i0 and $80000000;
  255. j0 := ((i0 shr 20) and $7ff) - $3ff;
  256. if (j0 < 20) then
  257. begin
  258. if (j0 < 0) then
  259. begin
  260. { the magnitude of the number is < 1 so the result is +-0. }
  261. f64.high := sx;
  262. f64.low := 0;
  263. end
  264. else
  265. begin
  266. f64.high := sx or (i0 and not($fffff shr j0));
  267. f64.low := 0;
  268. end
  269. end
  270. else if (j0 > 51) then
  271. begin
  272. if (j0 = $400) then
  273. { d is inf or NaN }
  274. exit(d + d); { don't know why they do this (JM) }
  275. end
  276. else
  277. begin
  278. f64.high := i0;
  279. f64.low := longint(i1 and not(cardinal($ffffffff) shr (j0 - 20)));
  280. end;
  281. result:=double(f64);
  282. end;
  283. {$else SUPPORT_DOUBLE}
  284. function fpc_int_real(d : ValReal) : ValReal;compilerproc;
  285. begin
  286. { this will be correct since real = single in the case of }
  287. { the motorola version of the compiler... }
  288. result:=ValReal(trunc(d));
  289. end;
  290. {$endif SUPPORT_DOUBLE}
  291. {$endif not FPC_SYSTEM_HAS_INT}
  292. {$ifndef FPC_SYSTEM_HAS_ABS}
  293. function fpc_abs_real(d : ValReal) : ValReal;compilerproc;
  294. begin
  295. if (d<0.0) then
  296. result := -d
  297. else
  298. result := d ;
  299. end;
  300. {$endif not FPC_SYSTEM_HAS_ABS}
  301. {$ifndef SYSTEM_HAS_FREXP}
  302. function frexp(x:Real; out e:Integer ):Real;
  303. {* frexp() extracts the exponent from x. It returns an integer *}
  304. {* power of two to expnt and the significand between 0.5 and 1 *}
  305. {* to y. Thus x = y * 2**expn. *}
  306. begin
  307. e :=0;
  308. if (abs(x)<0.5) then
  309. While (abs(x)<0.5) do
  310. begin
  311. x := x*2;
  312. Dec(e);
  313. end
  314. else
  315. While (abs(x)>1) do
  316. begin
  317. x := x/2;
  318. Inc(e);
  319. end;
  320. frexp := x;
  321. end;
  322. {$endif not SYSTEM_HAS_FREXP}
  323. {$ifndef SYSTEM_HAS_LDEXP}
  324. {$ifdef SUPPORT_DOUBLE}
  325. function ldexp( x: Real; N: Integer):Real;
  326. {* ldexp() multiplies x by 2**n. *}
  327. var
  328. i: integer;
  329. const
  330. H2_54: double = 18014398509481984.0; {2^54}
  331. huge: double = 1e300;
  332. begin
  333. i := (float64high(x) and $7ff00000) shr 20;
  334. {if +-INF, NaN, 0 or if e=0 return d}
  335. if (i=$7FF) or (N=0) or (x=0.0) then
  336. ldexp := x
  337. else if i=0 then {Denormal: result = d*2^54*2^(e-54)}
  338. ldexp := ldexp(x*H2_54, N-54)
  339. else
  340. begin
  341. N := N+i;
  342. if N>$7FE then { overflow }
  343. begin
  344. if x>0.0 then
  345. ldexp := 2.0*huge
  346. else
  347. ldexp := (-2.0)*huge;
  348. end
  349. else if N<1 then
  350. begin
  351. {underflow or denormal}
  352. if N<-53 then
  353. ldexp := 0.0
  354. else
  355. begin
  356. {Denormal: result = d*2^(e+54)/2^54}
  357. inc(N,54);
  358. float64sethigh(x,(float64high(x) and $800FFFFF) or (longint(N) shl 20));
  359. ldexp := x/H2_54;
  360. end;
  361. end
  362. else
  363. begin
  364. float64sethigh(x,(float64high(x) and $800FFFFF) or (longint(N) shl 20));
  365. ldexp := x;
  366. end;
  367. end;
  368. end;
  369. {$else SUPPORT_DOUBLE}
  370. function ldexp( x: Real; N: Integer):Real;
  371. {* ldexp() multiplies x by 2**n. *}
  372. var r : Real;
  373. begin
  374. R := 1;
  375. if N>0 then
  376. while N>0 do
  377. begin
  378. R:=R*2;
  379. Dec(N);
  380. end
  381. else
  382. while N<0 do
  383. begin
  384. R:=R/2;
  385. Inc(N);
  386. end;
  387. ldexp := x * R;
  388. end;
  389. {$endif SUPPORT_DOUBLE}
  390. {$endif not SYSTEM_HAS_LDEXP}
  391. function polevl(var x:Real; var Coef:TabCoef; N:Integer):Real;
  392. {*****************************************************************}
  393. { Evaluate polynomial }
  394. {*****************************************************************}
  395. { }
  396. { SYNOPSIS: }
  397. { }
  398. { int N; }
  399. { double x, y, coef[N+1], polevl[]; }
  400. { }
  401. { y = polevl( x, coef, N ); }
  402. { }
  403. { DESCRIPTION: }
  404. { }
  405. { Evaluates polynomial of degree N: }
  406. { }
  407. { 2 N }
  408. { y = C + C x + C x +...+ C x }
  409. { 0 1 2 N }
  410. { }
  411. { Coefficients are stored in reverse order: }
  412. { }
  413. { coef[0] = C , ..., coef[N] = C . }
  414. { N 0 }
  415. { }
  416. { The function p1evl() assumes that coef[N] = 1.0 and is }
  417. { omitted from the array. Its calling arguments are }
  418. { otherwise the same as polevl(). }
  419. { }
  420. { SPEED: }
  421. { }
  422. { In the interest of speed, there are no checks for out }
  423. { of bounds arithmetic. This routine is used by most of }
  424. { the functions in the library. Depending on available }
  425. { equipment features, the user may wish to rewrite the }
  426. { program in microcode or assembly language. }
  427. {*****************************************************************}
  428. var ans : Real;
  429. i : Integer;
  430. begin
  431. ans := Coef[0];
  432. for i:=1 to N do
  433. ans := ans * x + Coef[i];
  434. polevl:=ans;
  435. end;
  436. function p1evl(var x:Real; var Coef:TabCoef; N:Integer):Real;
  437. { }
  438. { Evaluate polynomial when coefficient of x is 1.0. }
  439. { Otherwise same as polevl. }
  440. { }
  441. var
  442. ans : Real;
  443. i : Integer;
  444. begin
  445. ans := x + Coef[0];
  446. for i:=1 to N-1 do
  447. ans := ans * x + Coef[i];
  448. p1evl := ans;
  449. end;
  450. function floord(x: double): double;
  451. var
  452. t: double;
  453. begin
  454. t := int(x);
  455. if (x>=0.0) or (x=t) then
  456. floord := t
  457. else
  458. floord := t - 1.0;
  459. end;
  460. {*
  461. * ====================================================
  462. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  463. *
  464. * Developed at SunPro, a Sun Microsystems, Inc. business.
  465. * Permission to use, copy, modify, and distribute this
  466. * software is freely granted, provided that this notice
  467. * is preserved.
  468. * ====================================================
  469. *
  470. * Pascal port of this routine comes from DAMath library
  471. * (C) Copyright 2013 Wolfgang Ehrhardt
  472. *
  473. * k_rem_pio2 return the last three bits of N with y = x - N*pi/2
  474. * so that |y| < pi/2.
  475. *
  476. * The method is to compute the integer (mod 8) and fraction parts of
  477. * (2/pi)*x without doing the full multiplication. In general we
  478. * skip the part of the product that are known to be a huge integer
  479. * (more accurately, = 0 mod 8 ). Thus the number of operations are
  480. * independent of the exponent of the input.
  481. *
  482. * (2/pi) is represented by an array of 24-bit integers in ipio2[].
  483. *
  484. * Input parameters:
  485. * x[] The input value (must be positive) is broken into nx
  486. * pieces of 24-bit integers in double precision format.
  487. * x[i] will be the i-th 24 bit of x. The scaled exponent
  488. * of x[0] is given in input parameter e0 (i.e., x[0]*2^e0
  489. * match x's up to 24 bits.
  490. *
  491. * Example of breaking a double positive z into x[0]+x[1]+x[2]:
  492. * e0 = ilogb(z)-23
  493. * z = scalbn(z,-e0)
  494. * for i = 0,1,2
  495. * x[i] = floor(z)
  496. * z = (z-x[i])*2**24
  497. *
  498. *
  499. * y[] output result in an array of double precision numbers.
  500. * The dimension of y[] is:
  501. * 24-bit precision 1
  502. * 53-bit precision 2
  503. * 64-bit precision 2
  504. * 113-bit precision 3
  505. * The actual value is the sum of them. Thus for 113-bit
  506. * precison, one may have to do something like:
  507. *
  508. * long double t,w,r_head, r_tail;
  509. * t = (long double)y[2] + (long double)y[1];
  510. * w = (long double)y[0];
  511. * r_head = t+w;
  512. * r_tail = w - (r_head - t);
  513. *
  514. * e0 The exponent of x[0]. Must be <= 16360 or you need to
  515. * expand the ipio2 table.
  516. *
  517. * nx dimension of x[]
  518. *
  519. * prec an integer indicating the precision:
  520. * 0 24 bits (single)
  521. * 1 53 bits (double)
  522. * 2 64 bits (extended)
  523. * 3 113 bits (quad)
  524. *
  525. * Here is the description of some local variables:
  526. *
  527. * jk jk+1 is the initial number of terms of ipio2[] needed
  528. * in the computation. The recommended value is 2,3,4,
  529. * 6 for single, double, extended,and quad.
  530. *
  531. * jz local integer variable indicating the number of
  532. * terms of ipio2[] used.
  533. *
  534. * jx nx - 1
  535. *
  536. * jv index for pointing to the suitable ipio2[] for the
  537. * computation. In general, we want
  538. * ( 2^e0*x[0] * ipio2[jv-1]*2^(-24jv) )/8
  539. * is an integer. Thus
  540. * e0-3-24*jv >= 0 or (e0-3)/24 >= jv
  541. * Hence jv = max(0,(e0-3)/24).
  542. *
  543. * jp jp+1 is the number of terms in PIo2[] needed, jp = jk.
  544. *
  545. * q[] double array with integral value, representing the
  546. * 24-bits chunk of the product of x and 2/pi.
  547. *
  548. * q0 the corresponding exponent of q[0]. Note that the
  549. * exponent for q[i] would be q0-24*i.
  550. *
  551. * PIo2[] double precision array, obtained by cutting pi/2
  552. * into 24 bits chunks.
  553. *
  554. * f[] ipio2[] in floating point
  555. *
  556. * iq[] integer array by breaking up q[] in 24-bits chunk.
  557. *
  558. * fq[] final product of x*(2/pi) in fq[0],..,fq[jk]
  559. *
  560. * ih integer. If >0 it indicates q[] is >= 0.5, hence
  561. * it also indicates the *sign* of the result.
  562. *}
  563. {PIo2[] double array, obtained by cutting pi/2 into 24 bits chunks.}
  564. const
  565. PIo2chunked: array[0..7] of double = (
  566. 1.57079625129699707031e+00, { 0x3FF921FB, 0x40000000 }
  567. 7.54978941586159635335e-08, { 0x3E74442D, 0x00000000 }
  568. 5.39030252995776476554e-15, { 0x3CF84698, 0x80000000 }
  569. 3.28200341580791294123e-22, { 0x3B78CC51, 0x60000000 }
  570. 1.27065575308067607349e-29, { 0x39F01B83, 0x80000000 }
  571. 1.22933308981111328932e-36, { 0x387A2520, 0x40000000 }
  572. 2.73370053816464559624e-44, { 0x36E38222, 0x80000000 }
  573. 2.16741683877804819444e-51 { 0x3569F31D, 0x00000000 }
  574. );
  575. {Table of constants for 2/pi, 396 Hex digits (476 decimal) of 2/pi }
  576. ipio2: array[0..65] of longint = (
  577. $A2F983, $6E4E44, $1529FC, $2757D1, $F534DD, $C0DB62,
  578. $95993C, $439041, $FE5163, $ABDEBB, $C561B7, $246E3A,
  579. $424DD2, $E00649, $2EEA09, $D1921C, $FE1DEB, $1CB129,
  580. $A73EE8, $8235F5, $2EBB44, $84E99C, $7026B4, $5F7E41,
  581. $3991D6, $398353, $39F49C, $845F8B, $BDF928, $3B1FF8,
  582. $97FFDE, $05980F, $EF2F11, $8B5A0A, $6D1F6D, $367ECF,
  583. $27CB09, $B74F46, $3F669E, $5FEA2D, $7527BA, $C7EBE5,
  584. $F17B3D, $0739F7, $8A5292, $EA6BFB, $5FB11F, $8D5D08,
  585. $560330, $46FC7B, $6BABF0, $CFBC20, $9AF436, $1DA9E3,
  586. $91615E, $E61B08, $659985, $5F14A0, $68408D, $FFD880,
  587. $4D7327, $310606, $1556CA, $73A8C9, $60E27B, $C08C6B);
  588. init_jk: array[0..3] of integer = (2,3,4,6); {initial value for jk}
  589. two24: double = 16777216.0; {2^24}
  590. twon24: double = 5.9604644775390625e-08; {1/2^24}
  591. type
  592. TDA02 = array[0..2] of double; { 3 elements is enough for float128 }
  593. function k_rem_pio2(const x: TDA02; out y: TDA02; e0, nx, prec: integer): sizeint;
  594. var
  595. i,ih,j,jz,jx,jv,jp,jk,carry,k,n,q0: longint;
  596. t: longint;
  597. iq: array[0..19] of longint;
  598. f,fq,q: array[0..19] of double;
  599. z,fw: double;
  600. begin
  601. {initialize jk}
  602. jk := init_jk[prec];
  603. jp := jk;
  604. {determine jx,jv,q0, note that 3>q0}
  605. jx := nx-1;
  606. jv := (e0-3) div 24; if jv<0 then jv := 0;
  607. q0 := e0-24*(jv+1);
  608. {set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk]}
  609. j := jv-jx;
  610. for i:=0 to jx+jk do
  611. begin
  612. if j<0 then f[i] := 0.0 else f[i] := ipio2[j];
  613. inc(j);
  614. end;
  615. {compute q[0],q[1],...q[jk]}
  616. for i:=0 to jk do
  617. begin
  618. fw := 0.0;
  619. for j:=0 to jx do
  620. fw := fw + x[j]*f[jx+i-j];
  621. q[i] := fw;
  622. end;
  623. jz := jk;
  624. repeat
  625. {distill q[] into iq[] reversingly}
  626. i := 0;
  627. z := q[jz];
  628. for j:=jz downto 1 do
  629. begin
  630. fw := trunc(twon24*z);
  631. iq[i] := trunc(z-two24*fw);
  632. z := q[j-1]+fw;
  633. inc(i);
  634. end;
  635. {compute n}
  636. z := ldexp(z,q0); {actual value of z}
  637. z := z - 8.0*floord(z*0.125); {trim off integer >= 8}
  638. n := trunc(z);
  639. z := z - n;
  640. ih := 0;
  641. if q0>0 then
  642. begin
  643. {need iq[jz-1] to determine n}
  644. t := (iq[jz-1] shr (24-q0));
  645. inc(n,t);
  646. dec(iq[jz-1], t shl (24-q0));
  647. ih := iq[jz-1] shr (23-q0);
  648. end
  649. else if q0=0 then
  650. ih := iq[jz-1] shr 23
  651. else if z>=0.5 then
  652. ih := 2;
  653. if ih>0 then {q > 0.5}
  654. begin
  655. inc(n);
  656. carry := 0;
  657. for i:=0 to jz-1 do
  658. begin
  659. {compute 1-q}
  660. t := iq[i];
  661. if carry=0 then
  662. begin
  663. if t<>0 then
  664. begin
  665. carry := 1;
  666. iq[i] := $1000000 - t;
  667. end
  668. end
  669. else
  670. iq[i] := $ffffff - t;
  671. end;
  672. if q0>0 then
  673. begin
  674. {rare case: chance is 1 in 12}
  675. case q0 of
  676. 1: iq[jz-1] := iq[jz-1] and $7fffff;
  677. 2: iq[jz-1] := iq[jz-1] and $3fffff;
  678. end;
  679. end;
  680. if ih=2 then
  681. begin
  682. z := 1.0 - z;
  683. if carry<>0 then
  684. z := z - ldexp(1.0,q0);
  685. end;
  686. end;
  687. {check if recomputation is needed}
  688. if z<>0.0 then
  689. break;
  690. t := 0;
  691. for i:=jz-1 downto jk do
  692. t := t or iq[i];
  693. if t<>0 then
  694. break;
  695. {need recomputation}
  696. k := 1;
  697. while iq[jk-k]=0 do {k = no. of terms needed}
  698. inc(k);
  699. for i:=jz+1 to jz+k do
  700. begin
  701. {add q[jz+1] to q[jz+k]}
  702. f[jx+i] := ipio2[jv+i];
  703. fw := 0.0;
  704. for j:=0 to jx do
  705. fw := fw + x[j]*f[jx+i-j];
  706. q[i] := fw;
  707. end;
  708. inc(jz,k);
  709. until False;
  710. {chop off zero terms}
  711. if z=0.0 then
  712. begin
  713. repeat
  714. dec(jz);
  715. dec(q0,24);
  716. until iq[jz]<>0;
  717. end
  718. else
  719. begin
  720. {break z into 24-bit if necessary}
  721. z := ldexp(z,-q0);
  722. if z>=two24 then
  723. begin
  724. fw := trunc(twon24*z);
  725. iq[jz] := trunc(z-two24*fw);
  726. inc(jz);
  727. inc(q0,24);
  728. iq[jz] := trunc(fw);
  729. end
  730. else
  731. iq[jz] := trunc(z);
  732. end;
  733. {convert integer "bit" chunk to floating-point value}
  734. fw := ldexp(1.0,q0);
  735. for i:=jz downto 0 do
  736. begin
  737. q[i] := fw*iq[i];
  738. fw := fw*twon24;
  739. end;
  740. {compute PIo2[0,...,jp]*q[jz,...,0]}
  741. for i:=jz downto 0 do
  742. begin
  743. fw :=0.0;
  744. k := 0;
  745. while (k<=jp) and (k<=jz-i) do
  746. begin
  747. fw := fw + double(PIo2chunked[k])*(q[i+k]);
  748. inc(k);
  749. end;
  750. fq[jz-i] := fw;
  751. end;
  752. {compress fq[] into y[]}
  753. case prec of
  754. 0:
  755. begin
  756. fw := 0.0;
  757. for i:=jz downto 0 do
  758. fw := fw + fq[i];
  759. if ih=0 then
  760. y[0] := fw
  761. else
  762. y[0] := -fw;
  763. end;
  764. 1, 2:
  765. begin
  766. fw := 0.0;
  767. for i:=jz downto 0 do
  768. fw := fw + fq[i];
  769. if ih=0 then
  770. y[0] := fw
  771. else
  772. y[0] := -fw;
  773. fw := fq[0]-fw;
  774. for i:=1 to jz do
  775. fw := fw + fq[i];
  776. if ih=0 then
  777. y[1] := fw
  778. else
  779. y[1] := -fw;
  780. end;
  781. 3:
  782. begin
  783. {painful}
  784. for i:=jz downto 1 do
  785. begin
  786. fw := fq[i-1]+fq[i];
  787. fq[i] := fq[i]+(fq[i-1]-fw);
  788. fq[i-1]:= fw;
  789. end;
  790. for i:=jz downto 2 do
  791. begin
  792. fw := fq[i-1]+fq[i];
  793. fq[i] := fq[i]+(fq[i-1]-fw);
  794. fq[i-1]:= fw;
  795. end;
  796. fw := 0.0;
  797. for i:=jz downto 2 do
  798. fw := fw + fq[i];
  799. if ih=0 then
  800. begin
  801. y[0] := fq[0];
  802. y[1] := fq[1];
  803. y[2] := fw;
  804. end
  805. else
  806. begin
  807. y[0] := -fq[0];
  808. y[1] := -fq[1];
  809. y[2] := -fw;
  810. end;
  811. end;
  812. end;
  813. k_rem_pio2 := n and 7;
  814. end;
  815. { Argument reduction of x: z = x - n*Pi/2, |z| <= Pi/4, result = n mod 8.}
  816. { Uses Payne/Hanek if |x| >= lossth, Cody/Waite otherwise}
  817. function rem_pio2(x: double; out z: double): sizeint;
  818. const
  819. tol: double = 2.384185791015625E-7; {lossth*eps_d}
  820. var
  821. i,e0,nx: longint;
  822. y: double;
  823. tx,ty: TDA02;
  824. begin
  825. y := abs(x);
  826. if (y < PIO4) then
  827. begin
  828. z := x;
  829. result := 0;
  830. exit;
  831. end
  832. else if (y < lossth) then
  833. begin
  834. y := floord(x/PIO4);
  835. i := trunc(y - 16.0*floord(y*0.0625));
  836. if odd(i) then
  837. begin
  838. inc(i);
  839. y := y + 1.0;
  840. end;
  841. z := ((x - y * DP1) - y * DP2) - y * DP3;
  842. result := (i shr 1) and 7;
  843. {If x is near a multiple of Pi/2, the C/W relative error may be large.}
  844. {In this case redo the calculation with the Payne/Hanek algorithm. }
  845. if abs(z) > tol then
  846. exit;
  847. end;
  848. z := abs(x);
  849. e0 := (float64high(z) shr 20)-1046;
  850. float64sethigh(z,float64high(z) - (e0 shl 20));
  851. tx[0] := trunc(z);
  852. z := (z-tx[0])*two24;
  853. tx[1] := trunc(z);
  854. tx[2] := (z-tx[1])*two24;
  855. nx := 3;
  856. while (tx[nx-1]=0.0) do dec(nx); { skip zero terms }
  857. result := k_rem_pio2(tx,ty,e0,nx,2);
  858. if (x<0) then
  859. begin
  860. result := (-result) and 7;
  861. z := -ty[0] - ty[1];
  862. end
  863. else
  864. z := ty[0] + ty[1];
  865. end;
  866. {$ifndef FPC_SYSTEM_HAS_SQR}
  867. function fpc_sqr_real(d : ValReal) : ValReal;compilerproc;{$ifdef MATHINLINE}inline;{$endif}
  868. begin
  869. result := d*d;
  870. end;
  871. {$endif}
  872. {$ifndef FPC_SYSTEM_HAS_PI}
  873. function fpc_pi_real : ValReal;compilerproc;{$ifdef MATHINLINE}inline;{$endif}
  874. begin
  875. result := 3.1415926535897932385;
  876. end;
  877. {$endif}
  878. {$ifndef FPC_SYSTEM_HAS_SQRT}
  879. function fpc_sqrt_real(d:ValReal):ValReal;compilerproc;
  880. {*****************************************************************}
  881. { Square root }
  882. {*****************************************************************}
  883. { }
  884. { SYNOPSIS: }
  885. { }
  886. { double x, y, sqrt(); }
  887. { }
  888. { y = sqrt( x ); }
  889. { }
  890. { DESCRIPTION: }
  891. { }
  892. { Returns the square root of x. }
  893. { }
  894. { Range reduction involves isolating the power of two of the }
  895. { argument and using a polynomial approximation to obtain }
  896. { a rough value for the square root. Then Heron's iteration }
  897. { is used three times to converge to an accurate value. }
  898. {*****************************************************************}
  899. var e : Integer;
  900. w,z : Real;
  901. begin
  902. if( d <= 0.0 ) then
  903. begin
  904. if d < 0.0 then begin
  905. float_raise(float_flag_invalid);
  906. d := 0/0;
  907. end;
  908. result := 0.0;
  909. end
  910. else
  911. begin
  912. w := d;
  913. { separate exponent and significand }
  914. z := frexp( d, e );
  915. { approximate square root of number between 0.5 and 1 }
  916. { relative error of approximation = 7.47e-3 }
  917. d := 4.173075996388649989089E-1 + 5.9016206709064458299663E-1 * z;
  918. { adjust for odd powers of 2 }
  919. if odd(e) then
  920. d := d*SQRT2;
  921. { re-insert exponent }
  922. d := ldexp( d, (e div 2) );
  923. { Newton iterations: }
  924. d := 0.5*(d + w/d);
  925. d := 0.5*(d + w/d);
  926. d := 0.5*(d + w/d);
  927. d := 0.5*(d + w/d);
  928. d := 0.5*(d + w/d);
  929. d := 0.5*(d + w/d);
  930. result := d;
  931. end;
  932. end;
  933. {$endif}
  934. {$ifndef FPC_SYSTEM_HAS_EXP}
  935. {$ifdef SUPPORT_DOUBLE}
  936. {
  937. This code was translated from uclib code, the original code
  938. had the following copyright notice:
  939. *
  940. * ====================================================
  941. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  942. *
  943. * Developed at SunPro, a Sun Microsystems, Inc. business.
  944. * Permission to use, copy, modify, and distribute this
  945. * software is freely granted, provided that this notice
  946. * is preserved.
  947. * ====================================================
  948. *}
  949. {*
  950. * Returns the exponential of x.
  951. *
  952. * Method
  953. * 1. Argument reduction:
  954. * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
  955. * Given x, find r and integer k such that
  956. *
  957. * x = k*ln2 + r, |r| <= 0.5*ln2.
  958. *
  959. * Here r will be represented as r = hi-lo for better
  960. * accuracy.
  961. *
  962. * 2. Approximation of exp(r) by a special rational function on
  963. * the interval [0,0.34658]:
  964. * Write
  965. * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
  966. * We use a special Reme algorithm on [0,0.34658] to generate
  967. * a polynomial of degree 5 to approximate R. The maximum error
  968. * of this polynomial approximation is bounded by 2**-59. In
  969. * other words,
  970. * R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5
  971. * (where z=r*r, and the values of P1 to P5 are listed below)
  972. * and
  973. * | 5 | -59
  974. * | 2.0+P1*z+...+P5*z - R(z) | <= 2
  975. * | |
  976. * The computation of exp(r) thus becomes
  977. * 2*r
  978. * exp(r) = 1 + -------
  979. * R - r
  980. * r*R1(r)
  981. * = 1 + r + ----------- (for better accuracy)
  982. * 2 - R1(r)
  983. * where
  984. 2 4 10
  985. * R1(r) = r - (P1*r + P2*r + ... + P5*r ).
  986. *
  987. * 3. Scale back to obtain exp(x):
  988. * From step 1, we have
  989. * exp(x) = 2^k * exp(r)
  990. *
  991. * Special cases:
  992. * exp(INF) is INF, exp(NaN) is NaN;
  993. * exp(-INF) is 0, and
  994. * for finite argument, only exp(0)=1 is exact.
  995. *
  996. * Accuracy:
  997. * according to an error analysis, the error is always less than
  998. * 1 ulp (unit in the last place).
  999. *
  1000. * Misc. info.
  1001. * For IEEE double
  1002. * if x > 7.09782712893383973096e+02 then exp(x) overflow
  1003. * if x < -7.45133219101941108420e+02 then exp(x) underflow
  1004. *
  1005. * Constants:
  1006. * The hexadecimal values are the intended ones for the following
  1007. * constants. The decimal values may be used, provided that the
  1008. * compiler will convert from decimal to binary accurately enough
  1009. * to produce the hexadecimal values shown.
  1010. *
  1011. }
  1012. function fpc_exp_real(d: ValReal):ValReal;compilerproc;
  1013. const
  1014. one: double = 1.0;
  1015. halF : array[0..1] of double = (0.5,-0.5);
  1016. huge: double = 1.0e+300;
  1017. twom1000: double = 9.33263618503218878990e-302; { 2**-1000=0x01700000,0}
  1018. o_threshold: double = 7.09782712893383973096e+02; { 0x40862E42, 0xFEFA39EF }
  1019. u_threshold: double = -7.45133219101941108420e+02; { 0xc0874910, 0xD52D3051 }
  1020. ln2HI : array[0..1] of double = ( 6.93147180369123816490e-01, { 0x3fe62e42, 0xfee00000 }
  1021. -6.93147180369123816490e-01); { 0xbfe62e42, 0xfee00000 }
  1022. ln2LO : array[0..1] of double = (1.90821492927058770002e-10, { 0x3dea39ef, 0x35793c76 }
  1023. -1.90821492927058770002e-10); { 0xbdea39ef, 0x35793c76 }
  1024. invln2: double = 1.44269504088896338700e+00; { 0x3ff71547, 0x652b82fe }
  1025. P1: double = 1.66666666666666019037e-01; { 0x3FC55555, 0x5555553E }
  1026. P2: double = -2.77777777770155933842e-03; { 0xBF66C16C, 0x16BEBD93 }
  1027. P3: double = 6.61375632143793436117e-05; { 0x3F11566A, 0xAF25DE2C }
  1028. P4: double = -1.65339022054652515390e-06; { 0xBEBBBD41, 0xC5D26BF1 }
  1029. P5: double = 4.13813679705723846039e-08; { 0x3E663769, 0x72BEA4D0 }
  1030. var
  1031. c,hi,lo,t,y : double;
  1032. k,xsb : longint;
  1033. hx,hy,lx : dword;
  1034. begin
  1035. hi:=0.0;
  1036. lo:=0.0;
  1037. k:=0;
  1038. hx:=float64high(d);
  1039. xsb := (hx shr 31) and 1; { sign bit of d }
  1040. hx := hx and $7fffffff; { high word of |d| }
  1041. { filter out non-finite argument }
  1042. if hx >= $40862E42 then
  1043. begin { if |d|>=709.78... }
  1044. if hx >= $7ff00000 then
  1045. begin
  1046. lx:=float64low(d);
  1047. if ((hx and $fffff) or lx)<>0 then
  1048. begin
  1049. result:=d+d; { NaN }
  1050. exit;
  1051. end
  1052. else
  1053. begin
  1054. if xsb=0 then
  1055. result:=d
  1056. else
  1057. result:=0.0; { exp(+-inf)=(inf,0) }
  1058. exit;
  1059. end;
  1060. end;
  1061. if d > o_threshold then begin
  1062. result:=huge*huge; { overflow }
  1063. exit;
  1064. end;
  1065. if d < u_threshold then begin
  1066. result:=twom1000*twom1000; { underflow }
  1067. exit;
  1068. end;
  1069. end;
  1070. { argument reduction }
  1071. if hx > $3fd62e42 then
  1072. begin { if |d| > 0.5 ln2 }
  1073. if hx < $3FF0A2B2 then { and |d| < 1.5 ln2 }
  1074. begin
  1075. hi := d-ln2HI[xsb];
  1076. lo:=ln2LO[xsb];
  1077. k := 1-xsb-xsb;
  1078. end
  1079. else
  1080. begin
  1081. k := trunc(invln2*d+halF[xsb]);
  1082. t := k;
  1083. hi := d - t*ln2HI[0]; { t*ln2HI is exact here }
  1084. lo := t*ln2LO[0];
  1085. end;
  1086. d := hi - lo;
  1087. end
  1088. else if hx < $3e300000 then
  1089. begin { when |d|<2**-28 }
  1090. if huge+d>one then
  1091. begin
  1092. result:=one+d;{ trigger inexact }
  1093. exit;
  1094. end;
  1095. end
  1096. else
  1097. k := 0;
  1098. { d is now in primary range }
  1099. t:=d*d;
  1100. c:=d - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
  1101. if k=0 then
  1102. begin
  1103. result:=one-((d*c)/(c-2.0)-d);
  1104. exit;
  1105. end
  1106. else
  1107. y := one-((lo-(d*c)/(2.0-c))-hi);
  1108. if k >= -1021 then
  1109. begin
  1110. hy:=float64high(y);
  1111. float64sethigh(y,longint(hy)+(k shl 20)); { add k to y's exponent }
  1112. result:=y;
  1113. end
  1114. else
  1115. begin
  1116. hy:=float64high(y);
  1117. float64sethigh(y,longint(hy)+((k+1000) shl 20)); { add k to y's exponent }
  1118. result:=y*twom1000;
  1119. end;
  1120. end;
  1121. {$else SUPPORT_DOUBLE}
  1122. function fpc_exp_real(d: ValReal):ValReal;compilerproc;
  1123. {*****************************************************************}
  1124. { Exponential Function }
  1125. {*****************************************************************}
  1126. { }
  1127. { SYNOPSIS: }
  1128. { }
  1129. { double x, y, exp(); }
  1130. { }
  1131. { y = exp( x ); }
  1132. { }
  1133. { DESCRIPTION: }
  1134. { }
  1135. { Returns e (2.71828...) raised to the x power. }
  1136. { }
  1137. { Range reduction is accomplished by separating the argument }
  1138. { into an integer k and fraction f such that }
  1139. { }
  1140. { x k f }
  1141. { e = 2 e. }
  1142. { }
  1143. { A Pade' form of degree 2/3 is used to approximate exp(f)- 1 }
  1144. { in the basic range [-0.5 ln 2, 0.5 ln 2]. }
  1145. {*****************************************************************}
  1146. const P : TabCoef = (
  1147. 1.26183092834458542160E-4,
  1148. 3.02996887658430129200E-2,
  1149. 1.00000000000000000000E0, 0, 0, 0, 0);
  1150. Q : TabCoef = (
  1151. 3.00227947279887615146E-6,
  1152. 2.52453653553222894311E-3,
  1153. 2.27266044198352679519E-1,
  1154. 2.00000000000000000005E0, 0 ,0 ,0);
  1155. C1 = 6.9335937500000000000E-1;
  1156. C2 = 2.1219444005469058277E-4;
  1157. var n : Integer;
  1158. px, qx, xx : Real;
  1159. begin
  1160. if( d > MAXLOG) then
  1161. float_raise(float_flag_overflow)
  1162. else
  1163. if( d < MINLOG ) then
  1164. begin
  1165. float_raise(float_flag_underflow);
  1166. result:=0; { Result if underflow masked }
  1167. end
  1168. else
  1169. begin
  1170. { Express e**x = e**g 2**n }
  1171. { = e**g e**( n loge(2) ) }
  1172. { = e**( g + n loge(2) ) }
  1173. px := d * LOG2E;
  1174. qx := Trunc( px + 0.5 ); { Trunc() truncates toward -infinity. }
  1175. n := Trunc(qx);
  1176. d := d - qx * C1;
  1177. d := d + qx * C2;
  1178. { rational approximation for exponential }
  1179. { of the fractional part: }
  1180. { e**x - 1 = 2x P(x**2)/( Q(x**2) - P(x**2) ) }
  1181. xx := d * d;
  1182. px := d * polevl( xx, P, 2 );
  1183. d := px/( polevl( xx, Q, 3 ) - px );
  1184. d := ldexp( d, 1 );
  1185. d := d + 1.0;
  1186. d := ldexp( d, n );
  1187. result := d;
  1188. end;
  1189. end;
  1190. {$endif SUPPORT_DOUBLE}
  1191. {$endif}
  1192. {$ifndef FPC_SYSTEM_HAS_ROUND}
  1193. function fpc_round_real(d : ValReal) : int64;compilerproc;
  1194. var
  1195. fr: ValReal;
  1196. tr: Int64;
  1197. Begin
  1198. fr := abs(Frac(d));
  1199. tr := Trunc(d);
  1200. result:=0;
  1201. case softfloat_rounding_mode of
  1202. float_round_nearest_even:
  1203. begin
  1204. if fr > 0.5 then
  1205. if d >= 0 then
  1206. result:=tr+1
  1207. else
  1208. result:=tr-1
  1209. else
  1210. if fr < 0.5 then
  1211. result:=tr
  1212. else { fr = 0.5 }
  1213. { check sign to decide ... }
  1214. { as in Turbo Pascal... }
  1215. begin
  1216. if d >= 0.0 then
  1217. result:=tr+1
  1218. else
  1219. result:=tr;
  1220. { round to even }
  1221. result:=result and not(1);
  1222. end;
  1223. end;
  1224. float_round_down:
  1225. if (d >= 0.0) or
  1226. (fr = 0.0) then
  1227. result:=tr
  1228. else
  1229. result:=tr-1;
  1230. float_round_up:
  1231. if (d >= 0.0) and
  1232. (fr <> 0.0) then
  1233. result:=tr+1
  1234. else
  1235. result:=tr;
  1236. float_round_to_zero:
  1237. result:=tr;
  1238. else
  1239. { needed for jvm: result must be initialized on all paths }
  1240. result:=0;
  1241. end;
  1242. end;
  1243. {$endif FPC_SYSTEM_HAS_ROUND}
  1244. {$ifndef FPC_SYSTEM_HAS_LN}
  1245. function fpc_ln_real(d:ValReal):ValReal;compilerproc;
  1246. {*****************************************************************}
  1247. { Natural Logarithm }
  1248. {*****************************************************************}
  1249. { }
  1250. { SYNOPSIS: }
  1251. { }
  1252. { double x, y, log(); }
  1253. { }
  1254. { y = ln( x ); }
  1255. { }
  1256. { DESCRIPTION: }
  1257. { }
  1258. { Returns the base e (2.718...) logarithm of x. }
  1259. { }
  1260. { The argument is separated into its exponent and fractional }
  1261. { parts. If the exponent is between -1 and +1, the logarithm }
  1262. { of the fraction is approximated by }
  1263. { }
  1264. { log(1+x) = x - 0.5 x**2 + x**3 P(x)/Q(x). }
  1265. { }
  1266. { Otherwise, setting z = 2(x-1)/x+1), }
  1267. { }
  1268. { log(x) = z + z**3 P(z)/Q(z). }
  1269. { }
  1270. {*****************************************************************}
  1271. const P : TabCoef = (
  1272. { Coefficients for log(1+x) = x - x**2/2 + x**3 P(x)/Q(x)
  1273. 1/sqrt(2) <= x < sqrt(2) }
  1274. 4.58482948458143443514E-5,
  1275. 4.98531067254050724270E-1,
  1276. 6.56312093769992875930E0,
  1277. 2.97877425097986925891E1,
  1278. 6.06127134467767258030E1,
  1279. 5.67349287391754285487E1,
  1280. 1.98892446572874072159E1);
  1281. Q : TabCoef = (
  1282. 1.50314182634250003249E1,
  1283. 8.27410449222435217021E1,
  1284. 2.20664384982121929218E2,
  1285. 3.07254189979530058263E2,
  1286. 2.14955586696422947765E2,
  1287. 5.96677339718622216300E1, 0);
  1288. { Coefficients for log(x) = z + z**3 P(z)/Q(z),
  1289. where z = 2(x-1)/(x+1)
  1290. 1/sqrt(2) <= x < sqrt(2) }
  1291. R : TabCoef = (
  1292. -7.89580278884799154124E-1,
  1293. 1.63866645699558079767E1,
  1294. -6.41409952958715622951E1, 0, 0, 0, 0);
  1295. S : TabCoef = (
  1296. -3.56722798256324312549E1,
  1297. 3.12093766372244180303E2,
  1298. -7.69691943550460008604E2, 0, 0, 0, 0);
  1299. var e : Integer;
  1300. z, y : Real;
  1301. begin
  1302. if( d <= 0.0 ) then
  1303. begin
  1304. float_raise(float_flag_invalid);
  1305. exit;
  1306. end;
  1307. d := frexp( d, e );
  1308. { logarithm using log(x) = z + z**3 P(z)/Q(z),
  1309. where z = 2(x-1)/x+1) }
  1310. if( (e > 2) or (e < -2) ) then
  1311. begin
  1312. if( d < SQRTH ) then
  1313. begin
  1314. { 2( 2x-1 )/( 2x+1 ) }
  1315. Dec(e, 1);
  1316. z := d - 0.5;
  1317. y := 0.5 * z + 0.5;
  1318. end
  1319. else
  1320. begin
  1321. { 2 (x-1)/(x+1) }
  1322. z := d - 0.5;
  1323. z := z - 0.5;
  1324. y := 0.5 * d + 0.5;
  1325. end;
  1326. d := z / y;
  1327. { /* rational form */ }
  1328. z := d*d;
  1329. z := d + d * ( z * polevl( z, R, 2 ) / p1evl( z, S, 3 ) );
  1330. end
  1331. else
  1332. begin
  1333. { logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) }
  1334. if( d < SQRTH ) then
  1335. begin
  1336. Dec(e, 1);
  1337. d := ldexp( d, 1 ) - 1.0; { 2x - 1 }
  1338. end
  1339. else
  1340. d := d - 1.0;
  1341. { rational form }
  1342. z := d*d;
  1343. y := d * ( z * polevl( d, P, 6 ) / p1evl( d, Q, 6 ) );
  1344. y := y - ldexp( z, -1 ); { y - 0.5 * z }
  1345. z := d + y;
  1346. end;
  1347. { recombine with exponent term }
  1348. if( e <> 0 ) then
  1349. begin
  1350. y := e;
  1351. z := z - y * 2.121944400546905827679e-4;
  1352. z := z + y * 0.693359375;
  1353. end;
  1354. result:= z;
  1355. end;
  1356. {$endif}
  1357. {$ifndef FPC_SYSTEM_HAS_SIN}
  1358. function fpc_Sin_real(d:ValReal):ValReal;compilerproc;
  1359. {*****************************************************************}
  1360. { Circular Sine }
  1361. {*****************************************************************}
  1362. { }
  1363. { SYNOPSIS: }
  1364. { }
  1365. { double x, y, sin(); }
  1366. { }
  1367. { y = sin( x ); }
  1368. { }
  1369. { DESCRIPTION: }
  1370. { }
  1371. { Range reduction is into intervals of pi/4. The reduction }
  1372. { error is nearly eliminated by contriving an extended }
  1373. { precision modular arithmetic. }
  1374. { }
  1375. { Two polynomial approximating functions are employed. }
  1376. { Between 0 and pi/4 the sine is approximated by }
  1377. { x + x**3 P(x**2). }
  1378. { Between pi/4 and pi/2 the cosine is represented as }
  1379. { 1 - x**2 Q(x**2). }
  1380. {*****************************************************************}
  1381. var y, z, zz : Real;
  1382. j : sizeint;
  1383. begin
  1384. j := rem_pio2(d,z) and 3;
  1385. zz := z * z;
  1386. if( (j=1) or (j=3) ) then
  1387. y := 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 )
  1388. else
  1389. { y = z + z * (zz * polevl( zz, sincof, 5 )); }
  1390. y := z + z * z * z * polevl( zz, sincof, 5 );
  1391. if (j > 1) then
  1392. result := -y
  1393. else
  1394. result := y;
  1395. end;
  1396. {$endif}
  1397. {$ifndef FPC_SYSTEM_HAS_COS}
  1398. function fpc_Cos_real(d:ValReal):ValReal;compilerproc;
  1399. {*****************************************************************}
  1400. { Circular cosine }
  1401. {*****************************************************************}
  1402. { }
  1403. { Circular cosine }
  1404. { }
  1405. { SYNOPSIS: }
  1406. { }
  1407. { double x, y, cos(); }
  1408. { }
  1409. { y = cos( x ); }
  1410. { }
  1411. { DESCRIPTION: }
  1412. { }
  1413. { Range reduction is into intervals of pi/4. The reduction }
  1414. { error is nearly eliminated by contriving an extended }
  1415. { precision modular arithmetic. }
  1416. { }
  1417. { Two polynomial approximating functions are employed. }
  1418. { Between 0 and pi/4 the cosine is approximated by }
  1419. { 1 - x**2 Q(x**2). }
  1420. { Between pi/4 and pi/2 the sine is represented as }
  1421. { x + x**3 P(x**2). }
  1422. {*****************************************************************}
  1423. var y, z, zz : Real;
  1424. j : sizeint;
  1425. begin
  1426. j := rem_pio2(d,z) and 3;
  1427. zz := z * z;
  1428. if( (j=1) or (j=3) ) then
  1429. { y = z + z * (zz * polevl( zz, sincof, 5 )); }
  1430. y := z + z * z * z * polevl( zz, sincof, 5 )
  1431. else
  1432. y := 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 );
  1433. if (j = 1) or (j = 2) then
  1434. result := -y
  1435. else
  1436. result := y ;
  1437. end;
  1438. {$endif}
  1439. {$ifndef FPC_SYSTEM_HAS_ARCTAN}
  1440. function fpc_ArcTan_real(d:ValReal):ValReal;compilerproc;
  1441. {
  1442. This code was translated from uclibc code, the original code
  1443. had the following copyright notice:
  1444. *
  1445. * ====================================================
  1446. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  1447. *
  1448. * Developed at SunPro, a Sun Microsystems, Inc. business.
  1449. * Permission to use, copy, modify, and distribute this
  1450. * software is freely granted, provided that this notice
  1451. * is preserved.
  1452. * ====================================================
  1453. *}
  1454. {********************************************************************}
  1455. { Inverse circular tangent (arctangent) }
  1456. {********************************************************************}
  1457. { }
  1458. { SYNOPSIS: }
  1459. { }
  1460. { double x, y, atan(); }
  1461. { }
  1462. { y = atan( x ); }
  1463. { }
  1464. { DESCRIPTION: }
  1465. { }
  1466. { Returns radian angle between -pi/2 and +pi/2 whose tangent }
  1467. { is x. }
  1468. { }
  1469. { Method }
  1470. { 1. Reduce x to positive by atan(x) = -atan(-x). }
  1471. { 2. According to the integer k=4t+0.25 chopped, t=x, the argument }
  1472. { is further reduced to one of the following intervals and the }
  1473. { arctangent of t is evaluated by the corresponding formula: }
  1474. { }
  1475. { [0,7/16] atan(x) = t-t^3*(a1+t^2*(a2+...(a10+t^2*a11)...) }
  1476. { [7/16,11/16] atan(x) = atan(1/2) + atan( (t-0.5)/(1+t/2) ) }
  1477. { [11/16.19/16] atan(x) = atan( 1 ) + atan( (t-1)/(1+t) ) }
  1478. { [19/16,39/16] atan(x) = atan(3/2) + atan( (t-1.5)/(1+1.5t) ) }
  1479. { [39/16,INF] atan(x) = atan(INF) + atan( -1/t ) }
  1480. {********************************************************************}
  1481. const
  1482. atanhi: array [0..3] of double = (
  1483. 4.63647609000806093515e-01, { atan(0.5)hi 0x3FDDAC67, 0x0561BB4F }
  1484. 7.85398163397448278999e-01, { atan(1.0)hi 0x3FE921FB, 0x54442D18 }
  1485. 9.82793723247329054082e-01, { atan(1.5)hi 0x3FEF730B, 0xD281F69B }
  1486. 1.57079632679489655800e+00 { atan(inf)hi 0x3FF921FB, 0x54442D18 }
  1487. );
  1488. atanlo: array [0..3] of double = (
  1489. 2.26987774529616870924e-17, { atan(0.5)lo 0x3C7A2B7F, 0x222F65E2 }
  1490. 3.06161699786838301793e-17, { atan(1.0)lo 0x3C81A626, 0x33145C07 }
  1491. 1.39033110312309984516e-17, { atan(1.5)lo 0x3C700788, 0x7AF0CBBD }
  1492. 6.12323399573676603587e-17 { atan(inf)lo 0x3C91A626, 0x33145C07 }
  1493. );
  1494. aT: array[0..10] of double = (
  1495. 3.33333333333329318027e-01, { 0x3FD55555, 0x5555550D }
  1496. -1.99999999998764832476e-01, { 0xBFC99999, 0x9998EBC4 }
  1497. 1.42857142725034663711e-01, { 0x3FC24924, 0x920083FF }
  1498. -1.11111104054623557880e-01, { 0xBFBC71C6, 0xFE231671 }
  1499. 9.09088713343650656196e-02, { 0x3FB745CD, 0xC54C206E }
  1500. -7.69187620504482999495e-02, { 0xBFB3B0F2, 0xAF749A6D }
  1501. 6.66107313738753120669e-02, { 0x3FB10D66, 0xA0D03D51 }
  1502. -5.83357013379057348645e-02, { 0xBFADDE2D, 0x52DEFD9A }
  1503. 4.97687799461593236017e-02, { 0x3FA97B4B, 0x24760DEB }
  1504. -3.65315727442169155270e-02, { 0xBFA2B444, 0x2C6A6C2F }
  1505. 1.62858201153657823623e-02 { 0x3F90AD3A, 0xE322DA11 }
  1506. );
  1507. one: double = 1.0;
  1508. huge: double = 1.0e300;
  1509. var
  1510. w,s1,s2,z: double;
  1511. ix,hx,id: longint;
  1512. low: longword;
  1513. begin
  1514. hx:=float64high(d);
  1515. ix := hx and $7fffffff;
  1516. if (ix>=$44100000) then { if |x| >= 2^66 }
  1517. begin
  1518. low:=float64low(d);
  1519. if (ix > $7ff00000) or ((ix = $7ff00000) and (low<>0)) then
  1520. exit(d+d); { NaN }
  1521. if (hx>0) then
  1522. exit(atanhi[3]+atanlo[3])
  1523. else
  1524. exit(-atanhi[3]-atanlo[3]);
  1525. end;
  1526. if (ix < $3fdc0000) then { |x| < 0.4375 }
  1527. begin
  1528. if (ix < $3e200000) then { |x| < 2^-29 }
  1529. begin
  1530. if (huge+d>one) then exit(d); { raise inexact }
  1531. end;
  1532. id := -1;
  1533. end
  1534. else
  1535. begin
  1536. d := abs(d);
  1537. if (ix < $3ff30000) then { |x| < 1.1875 }
  1538. begin
  1539. if (ix < $3fe60000) then { 7/16 <=|x|<11/16 }
  1540. begin
  1541. id := 0; d := (2.0*d-one)/(2.0+d);
  1542. end
  1543. else { 11/16<=|x|< 19/16 }
  1544. begin
  1545. id := 1; d := (d-one)/(d+one);
  1546. end
  1547. end
  1548. else
  1549. begin
  1550. if (ix < $40038000) then { |x| < 2.4375 }
  1551. begin
  1552. id := 2; d := (d-1.5)/(one+1.5*d);
  1553. end
  1554. else { 2.4375 <= |x| < 2^66 }
  1555. begin
  1556. id := 3; d := -1.0/d;
  1557. end;
  1558. end;
  1559. end;
  1560. { end of argument reduction }
  1561. z := d*d;
  1562. w := z*z;
  1563. { break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly }
  1564. s1 := z*(aT[0]+w*(aT[2]+w*(aT[4]+w*(aT[6]+w*(aT[8]+w*aT[10])))));
  1565. s2 := w*(aT[1]+w*(aT[3]+w*(aT[5]+w*(aT[7]+w*aT[9]))));
  1566. if (id<0) then
  1567. result := d - d*(s1+s2)
  1568. else
  1569. begin
  1570. z := atanhi[id] - ((d*(s1+s2) - atanlo[id]) - d);
  1571. if hx<0 then
  1572. result := -z
  1573. else
  1574. result := z;
  1575. end;
  1576. end;
  1577. {$endif}
  1578. {$ifndef FPC_SYSTEM_HAS_FRAC}
  1579. function fpc_frac_real(d : ValReal) : ValReal;compilerproc;
  1580. begin
  1581. result := d - Int(d);
  1582. end;
  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. if i<0 then
  1595. result:=-double(qword(-i))
  1596. else
  1597. result:=qword(i);
  1598. end;
  1599. {$endif FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  1600. {$endif FPC_INCLUDE_SOFTWARE_INT64_TO_DOUBLE}
  1601. {$ifdef SUPPORT_DOUBLE}
  1602. {****************************************************************************
  1603. Helper routines to support old TP styled reals
  1604. ****************************************************************************}
  1605. {$ifndef FPC_SYSTEM_HAS_REAL2DOUBLE}
  1606. function real2double(r : real48) : double;
  1607. var
  1608. res : array[0..7] of byte;
  1609. exponent : word;
  1610. begin
  1611. { check for zero }
  1612. if r[0]=0 then
  1613. begin
  1614. real2double:=0.0;
  1615. exit;
  1616. end;
  1617. { copy mantissa }
  1618. res[0]:=0;
  1619. res[1]:=r[1] shl 5;
  1620. res[2]:=(r[1] shr 3) or (r[2] shl 5);
  1621. res[3]:=(r[2] shr 3) or (r[3] shl 5);
  1622. res[4]:=(r[3] shr 3) or (r[4] shl 5);
  1623. res[5]:=(r[4] shr 3) or (r[5] and $7f) shl 5;
  1624. res[6]:=(r[5] and $7f) shr 3;
  1625. { copy exponent }
  1626. { correct exponent: }
  1627. exponent:=(word(r[0])+(1023-129));
  1628. res[6]:=res[6] or ((exponent and $f) shl 4);
  1629. res[7]:=exponent shr 4;
  1630. { set sign }
  1631. res[7]:=res[7] or (r[5] and $80);
  1632. real2double:=double(res);
  1633. end;
  1634. {$endif FPC_SYSTEM_HAS_REAL2DOUBLE}
  1635. {$endif SUPPORT_DOUBLE}
  1636. {$ifdef SUPPORT_EXTENDED}
  1637. { fast 10^n routine }
  1638. function FPower10(val: Extended; Power: Longint): Extended;
  1639. const
  1640. pow32 : array[0..31] of extended =
  1641. (
  1642. 1e0,1e1,1e2,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,
  1643. 1e11,1e12,1e13,1e14,1e15,1e16,1e17,1e18,1e19,1e20,
  1644. 1e21,1e22,1e23,1e24,1e25,1e26,1e27,1e28,1e29,1e30,
  1645. 1e31
  1646. );
  1647. pow512 : array[0..15] of extended =
  1648. (
  1649. 1,1e32,1e64,1e96,1e128,1e160,1e192,1e224,
  1650. 1e256,1e288,1e320,1e352,1e384,1e416,1e448,
  1651. 1e480
  1652. );
  1653. pow4096 : array[0..9] of extended =
  1654. (1,1e512,1e1024,1e1536,
  1655. 1e2048,1e2560,1e3072,1e3584,
  1656. 1e4096,1e4608
  1657. );
  1658. negpow32 : array[0..31] of extended =
  1659. (
  1660. 1e-0,1e-1,1e-2,1e-3,1e-4,1e-5,1e-6,1e-7,1e-8,1e-9,1e-10,
  1661. 1e-11,1e-12,1e-13,1e-14,1e-15,1e-16,1e-17,1e-18,1e-19,1e-20,
  1662. 1e-21,1e-22,1e-23,1e-24,1e-25,1e-26,1e-27,1e-28,1e-29,1e-30,
  1663. 1e-31
  1664. );
  1665. negpow512 : array[0..15] of extended =
  1666. (
  1667. 0,1e-32,1e-64,1e-96,1e-128,1e-160,1e-192,1e-224,
  1668. 1e-256,1e-288,1e-320,1e-352,1e-384,1e-416,1e-448,
  1669. 1e-480
  1670. );
  1671. negpow4096 : array[0..9] of extended =
  1672. (
  1673. 0,1e-512,1e-1024,1e-1536,
  1674. 1e-2048,1e-2560,1e-3072,1e-3584,
  1675. 1e-4096,1e-4608
  1676. );
  1677. begin
  1678. if Power<0 then
  1679. begin
  1680. Power:=-Power;
  1681. result:=val*negpow32[Power and $1f];
  1682. power:=power shr 5;
  1683. if power<>0 then
  1684. begin
  1685. result:=result*negpow512[Power and $f];
  1686. power:=power shr 4;
  1687. if power<>0 then
  1688. begin
  1689. if power<=9 then
  1690. result:=result*negpow4096[Power]
  1691. else
  1692. result:=1.0/0.0;
  1693. end;
  1694. end;
  1695. end
  1696. else
  1697. begin
  1698. result:=val*pow32[Power and $1f];
  1699. power:=power shr 5;
  1700. if power<>0 then
  1701. begin
  1702. result:=result*pow512[Power and $f];
  1703. power:=power shr 4;
  1704. if power<>0 then
  1705. begin
  1706. if power<=9 then
  1707. result:=result*pow4096[Power]
  1708. else
  1709. result:=1.0/0.0;
  1710. end;
  1711. end;
  1712. end;
  1713. end;
  1714. {$endif SUPPORT_EXTENDED}