genmath.inc 61 KB

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