genmath.inc 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2001 by Several contributors
  4. Generic mathemtical 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. {$goto on}
  34. type
  35. TabCoef = array[0..6] of Real;
  36. { also necessary for Int() on systems with 64bit floats (JM) }
  37. {$ifndef FPC_SYSTEM_HAS_float64}
  38. {$ifdef ENDIAN_LITTLE}
  39. float64 = packed record
  40. low: longint;
  41. high: longint;
  42. end;
  43. {$else}
  44. float64 = packed record
  45. high: longint;
  46. low: longint;
  47. end;
  48. {$endif}
  49. {$endif FPC_SYSTEM_HAS_float64}
  50. const
  51. PIO2 = 1.57079632679489661923; { pi/2 }
  52. PIO4 = 7.85398163397448309616E-1; { pi/4 }
  53. SQRT2 = 1.41421356237309504880; { sqrt(2) }
  54. SQRTH = 7.07106781186547524401E-1; { sqrt(2)/2 }
  55. LOG2E = 1.4426950408889634073599; { 1/log(2) }
  56. SQ2OPI = 7.9788456080286535587989E-1; { sqrt( 2/pi )}
  57. LOGE2 = 6.93147180559945309417E-1; { log(2) }
  58. LOGSQ2 = 3.46573590279972654709E-1; { log(2)/2 }
  59. THPIO4 = 2.35619449019234492885; { 3*pi/4 }
  60. TWOOPI = 6.36619772367581343075535E-1; { 2/pi }
  61. lossth = 1.073741824e9;
  62. MAXLOG = 8.8029691931113054295988E1; { log(2**127) }
  63. MINLOG = -8.872283911167299960540E1; { log(2**-128) }
  64. DP1 = 7.85398125648498535156E-1;
  65. DP2 = 3.77489470793079817668E-8;
  66. DP3 = 2.69515142907905952645E-15;
  67. const sincof : TabCoef = (
  68. 1.58962301576546568060E-10,
  69. -2.50507477628578072866E-8,
  70. 2.75573136213857245213E-6,
  71. -1.98412698295895385996E-4,
  72. 8.33333333332211858878E-3,
  73. -1.66666666666666307295E-1, 0);
  74. coscof : TabCoef = (
  75. -1.13585365213876817300E-11,
  76. 2.08757008419747316778E-9,
  77. -2.75573141792967388112E-7,
  78. 2.48015872888517045348E-5,
  79. -1.38888888888730564116E-3,
  80. 4.16666666666665929218E-2, 0);
  81. {$ifndef FPC_SYSTEM_HAS_TRUNC}
  82. {$ifndef FPC_SYSTEM_HAS_float32}
  83. type
  84. float32 = longint;
  85. {$endif FPC_SYSTEM_HAS_float32}
  86. {$ifndef FPC_SYSTEM_HAS_flag}
  87. type
  88. flag = byte;
  89. {$endif FPC_SYSTEM_HAS_flag}
  90. {$ifndef FPC_SYSTEM_HAS_extractFloat64Frac0}
  91. Function extractFloat64Frac0(const a: float64): longint;
  92. Begin
  93. extractFloat64Frac0 := a.high and $000FFFFF;
  94. End;
  95. {$endif not FPC_SYSTEM_HAS_extractFloat64Frac0}
  96. {$ifndef FPC_SYSTEM_HAS_extractFloat64Frac1}
  97. Function extractFloat64Frac1(const a: float64): longint;
  98. Begin
  99. extractFloat64Frac1 := a.low;
  100. End;
  101. {$endif not FPC_SYSTEM_HAS_extractFloat64Frac1}
  102. {$ifndef FPC_SYSTEM_HAS_extractFloat64Exp}
  103. Function extractFloat64Exp(const a: float64): smallint;
  104. Begin
  105. extractFloat64Exp:= ( a.high shr 20 ) AND $7FF;
  106. End;
  107. {$endif not FPC_SYSTEM_HAS_extractFloat64Exp}
  108. {$ifndef FPC_SYSTEM_HAS_extractFloat64Frac}
  109. Function extractFloat64Frac(const a: float64): int64;
  110. Begin
  111. extractFloat64Frac:=int64(a) and $000FFFFFFFFFFFFF;
  112. End;
  113. {$endif not FPC_SYSTEM_HAS_extractFloat64Frac}
  114. {$ifndef FPC_SYSTEM_HAS_extractFloat64Sign}
  115. Function extractFloat64Sign(const a: float64) : flag;
  116. Begin
  117. extractFloat64Sign := a.high shr 31;
  118. End;
  119. {$endif not FPC_SYSTEM_HAS_extractFloat64Sign}
  120. Procedure shortShift64Left(a0:longint; a1:longint; count:smallint; VAR z0Ptr:longint; VAR z1Ptr:longint );
  121. Begin
  122. z1Ptr := a1 shl count;
  123. if count = 0 then
  124. z0Ptr := a0
  125. else
  126. z0Ptr := ( a0 shl count ) OR ( a1 shr ( ( - count ) AND 31 ) );
  127. End;
  128. function float64_to_int32_round_to_zero(a: float64 ): longint;
  129. Var
  130. aSign: flag;
  131. aExp, shiftCount: smallint;
  132. aSig0, aSig1, absZ, aSigExtra: longint;
  133. z: longint;
  134. Begin
  135. aSig1 := extractFloat64Frac1( a );
  136. aSig0 := extractFloat64Frac0( a );
  137. aExp := extractFloat64Exp( a );
  138. aSign := extractFloat64Sign( a );
  139. shiftCount := aExp - $413;
  140. if 0<=shiftCount then
  141. Begin
  142. if (aExp=$7FF) and ((aSig0 or aSig1)<>0) then
  143. HandleError(207);
  144. shortShift64Left(aSig0 OR $00100000, aSig1, shiftCount, absZ, aSigExtra );
  145. End
  146. else
  147. Begin
  148. if aExp<$3FF then
  149. begin
  150. float64_to_int32_round_to_zero := 0;
  151. exit;
  152. end;
  153. aSig0 := aSig0 or $00100000;
  154. aSigExtra := ( aSig0 shl ( shiftCount and 31 ) ) OR aSig1;
  155. absZ := aSig0 shr ( - shiftCount );
  156. End;
  157. if aSign<>0 then
  158. z:=-absZ
  159. else
  160. z:=absZ;
  161. if ((aSign<>0) xor (z<0)) AND (z<>0) then
  162. HandleError(207);
  163. float64_to_int32_round_to_zero := z;
  164. End;
  165. function float64_to_int64_round_to_zero(a : float64) : int64;
  166. var
  167. aSign : flag;
  168. aExp, shiftCount : smallint;
  169. aSig : int64;
  170. z : int64;
  171. begin
  172. aSig:=extractFloat64Frac(a);
  173. aExp:=extractFloat64Exp(a);
  174. aSign:=extractFloat64Sign(a);
  175. if aExp<>0 then
  176. aSig:=aSig or $0010000000000000;
  177. shiftCount:= aExp-$433;
  178. if 0<=shiftCount then
  179. begin
  180. if aExp>=$43e then
  181. begin
  182. if int64(a)<>$C3E0000000000000 then
  183. HandleError(207);
  184. { pascal doesn't know Inf for int64 }
  185. HandleError(207);
  186. end;
  187. z:=aSig shl shiftCount;
  188. end
  189. else
  190. begin
  191. if aExp<$3fe then
  192. begin
  193. result:=0;
  194. exit;
  195. end;
  196. z:=aSig shr -shiftCount;
  197. {
  198. if (aSig shl (shiftCount and 63))<>0 then
  199. float_exception_flags |= float_flag_inexact;
  200. }
  201. end;
  202. if aSign<>0 then
  203. z:=-z;
  204. result:=z;
  205. end;
  206. {$ifndef FPC_SYSTEM_HAS_ExtractFloat32Frac}
  207. Function ExtractFloat32Frac(a : Float32) : longint;
  208. Begin
  209. ExtractFloat32Frac := A AND $007FFFFF;
  210. End;
  211. {$endif not FPC_SYSTEM_HAS_ExtractFloat32Frac}
  212. {$ifndef FPC_SYSTEM_HAS_extractFloat32Exp}
  213. Function extractFloat32Exp( a: float32 ): smallint;
  214. Begin
  215. extractFloat32Exp := (a shr 23) AND $FF;
  216. End;
  217. {$endif not FPC_SYSTEM_HAS_extractFloat32Exp}
  218. {$ifndef FPC_SYSTEM_HAS_extractFloat32Sign}
  219. Function extractFloat32Sign( a: float32 ): Flag;
  220. Begin
  221. extractFloat32Sign := a shr 31;
  222. End;
  223. {$endif not FPC_SYSTEM_HAS_extractFloat32Sign}
  224. Function float32_to_int32_round_to_zero( a: Float32 ): longint;
  225. Var
  226. aSign : flag;
  227. aExp, shiftCount : smallint;
  228. aSig : longint;
  229. z : longint;
  230. Begin
  231. aSig := extractFloat32Frac( a );
  232. aExp := extractFloat32Exp( a );
  233. aSign := extractFloat32Sign( a );
  234. shiftCount := aExp - $9E;
  235. if ( 0 <= shiftCount ) then
  236. Begin
  237. if ( a <> Float32($CF000000) ) then
  238. Begin
  239. if ( (aSign=0) or ( ( aExp = $FF ) and (aSig<>0) ) ) then
  240. Begin
  241. HandleError(207);
  242. exit;
  243. end;
  244. End;
  245. HandleError(207);
  246. exit;
  247. End
  248. else
  249. if ( aExp <= $7E ) then
  250. Begin
  251. float32_to_int32_round_to_zero := 0;
  252. exit;
  253. End;
  254. aSig := ( aSig or $00800000 ) shl 8;
  255. z := aSig shr ( - shiftCount );
  256. if ( aSign<>0 ) then z := - z;
  257. float32_to_int32_round_to_zero := z;
  258. End;
  259. function fpc_trunc_real(d : ValReal) : int64;compilerproc;
  260. var
  261. {$ifdef cpuarm}
  262. l: longint;
  263. {$endif cpuarm}
  264. f32 : float32;
  265. f64 : float64;
  266. Begin
  267. { in emulation mode the real is equal to a single }
  268. { otherwise in fpu mode, it is equal to a double }
  269. { extended is not supported yet. }
  270. if sizeof(D) > 8 then
  271. HandleError(255);
  272. if sizeof(D)=8 then
  273. begin
  274. move(d,f64,sizeof(f64));
  275. {$ifdef cpuarm}
  276. { the arm fpu has a strange opinion how a double has to be stored }
  277. l:=f64.low;
  278. f64.low:=f64.high;
  279. f64.high:=l;
  280. {$endif cpuarm}
  281. result:=float64_to_int64_round_to_zero(f64);
  282. end
  283. else
  284. begin
  285. move(d,f32,sizeof(f32));
  286. result:=float32_to_int32_round_to_zero(f32);
  287. end;
  288. end;
  289. {$endif not FPC_SYSTEM_HAS_TRUNC}
  290. {$ifndef FPC_SYSTEM_HAS_INT}
  291. {$ifdef SUPPORT_DOUBLE}
  292. { straight Pascal translation of the code for __trunc() in }
  293. { the file sysdeps/libm-ieee754/s_trunc.c of glibc (JM) }
  294. function fpc_int_real(d: ValReal): ValReal;compilerproc;
  295. var
  296. i0, j0: longint;
  297. i1: cardinal;
  298. sx: longint;
  299. f64 : float64;
  300. begin
  301. f64:=float64(d);
  302. {$ifdef cpuarm}
  303. { the arm fpu has a strange opinion how a double has to be stored }
  304. i0:=f64.low;
  305. f64.low:=f64.high;
  306. f64.high:=i0;
  307. {$endif cpuarm}
  308. i0 := f64.high;
  309. i1 := cardinal(f64.low);
  310. sx := i0 and $80000000;
  311. j0 := ((i0 shr 20) and $7ff) - $3ff;
  312. if (j0 < 20) then
  313. begin
  314. if (j0 < 0) then
  315. begin
  316. { the magnitude of the number is < 1 so the result is +-0. }
  317. f64.high := sx;
  318. f64.low := 0;
  319. end
  320. else
  321. begin
  322. f64.high := sx or (i0 and not($fffff shr j0));
  323. f64.low := 0;
  324. end
  325. end
  326. else if (j0 > 51) then
  327. begin
  328. if (j0 = $400) then
  329. { d is inf or NaN }
  330. exit(d + d); { don't know why they do this (JM) }
  331. end
  332. else
  333. begin
  334. f64.high := i0;
  335. f64.low := longint(i1 and not(cardinal($ffffffff) shr (j0 - 20)));
  336. end;
  337. {$ifdef cpuarm}
  338. { the arm fpu has a strange opinion how a double has to be stored }
  339. i0:=f64.low;
  340. f64.low:=f64.high;
  341. f64.high:=i0;
  342. {$endif cpuarm}
  343. result:=double(f64);
  344. end;
  345. {$else SUPPORT_DOUBLE}
  346. function fpc_int_real(d : ValReal) : ValReal;compilerproc;
  347. begin
  348. { this will be correct since real = single in the case of }
  349. { the motorola version of the compiler... }
  350. result:=ValReal(trunc(d));
  351. end;
  352. {$endif SUPPORT_DOUBLE}
  353. {$endif not FPC_SYSTEM_HAS_INT}
  354. {$ifndef FPC_SYSTEM_HAS_ABS}
  355. function fpc_abs_real(d : ValReal) : ValReal;compilerproc;
  356. begin
  357. if (d<0.0) then
  358. result := -d
  359. else
  360. result := d ;
  361. end;
  362. {$endif not FPC_SYSTEM_HAS_ABS}
  363. {$ifndef SYSTEM_HAS_FREXP}
  364. function frexp(x:Real; out e:Integer ):Real;
  365. {* frexp() extracts the exponent from x. It returns an integer *}
  366. {* power of two to expnt and the significand between 0.5 and 1 *}
  367. {* to y. Thus x = y * 2**expn. *}
  368. begin
  369. e :=0;
  370. if (abs(x)<0.5) then
  371. While (abs(x)<0.5) do
  372. begin
  373. x := x*2;
  374. Dec(e);
  375. end
  376. else
  377. While (abs(x)>1) do
  378. begin
  379. x := x/2;
  380. Inc(e);
  381. end;
  382. frexp := x;
  383. end;
  384. {$endif not SYSTEM_HAS_FREXP}
  385. {$ifndef SYSTEM_HAS_LDEXP}
  386. function ldexp( x: Real; N: Integer):Real;
  387. {* ldexp() multiplies x by 2**n. *}
  388. var r : Real;
  389. begin
  390. R := 1;
  391. if N>0 then
  392. while N>0 do
  393. begin
  394. R:=R*2;
  395. Dec(N);
  396. end
  397. else
  398. while N<0 do
  399. begin
  400. R:=R/2;
  401. Inc(N);
  402. end;
  403. ldexp := x * R;
  404. end;
  405. {$endif not SYSTEM_HAS_LDEXP}
  406. function polevl(var x:Real; var Coef:TabCoef; N:Integer):Real;
  407. {*****************************************************************}
  408. { Evaluate polynomial }
  409. {*****************************************************************}
  410. { }
  411. { SYNOPSIS: }
  412. { }
  413. { int N; }
  414. { double x, y, coef[N+1], polevl[]; }
  415. { }
  416. { y = polevl( x, coef, N ); }
  417. { }
  418. { DESCRIPTION: }
  419. { }
  420. { Evaluates polynomial of degree N: }
  421. { }
  422. { 2 N }
  423. { y = C + C x + C x +...+ C x }
  424. { 0 1 2 N }
  425. { }
  426. { Coefficients are stored in reverse order: }
  427. { }
  428. { coef[0] = C , ..., coef[N] = C . }
  429. { N 0 }
  430. { }
  431. { The function p1evl() assumes that coef[N] = 1.0 and is }
  432. { omitted from the array. Its calling arguments are }
  433. { otherwise the same as polevl(). }
  434. { }
  435. { SPEED: }
  436. { }
  437. { In the interest of speed, there are no checks for out }
  438. { of bounds arithmetic. This routine is used by most of }
  439. { the functions in the library. Depending on available }
  440. { equipment features, the user may wish to rewrite the }
  441. { program in microcode or assembly language. }
  442. {*****************************************************************}
  443. var ans : Real;
  444. i : Integer;
  445. begin
  446. ans := Coef[0];
  447. for i:=1 to N do
  448. ans := ans * x + Coef[i];
  449. polevl:=ans;
  450. end;
  451. function p1evl(var x:Real; var Coef:TabCoef; N:Integer):Real;
  452. { }
  453. { Evaluate polynomial when coefficient of x is 1.0. }
  454. { Otherwise same as polevl. }
  455. { }
  456. var
  457. ans : Real;
  458. i : Integer;
  459. begin
  460. ans := x + Coef[0];
  461. for i:=1 to N-1 do
  462. ans := ans * x + Coef[i];
  463. p1evl := ans;
  464. end;
  465. {$ifndef FPC_SYSTEM_HAS_SQR}
  466. function fpc_sqr_real(d : ValReal) : ValReal;compilerproc;{$ifdef MATHINLINE}inline;{$endif}
  467. begin
  468. result := d*d;
  469. end;
  470. {$endif}
  471. {$ifndef FPC_SYSTEM_HAS_PI}
  472. function fpc_pi_real : ValReal;compilerproc;{$ifdef MATHINLINE}inline;{$endif}
  473. begin
  474. result := 3.1415926535897932385;
  475. end;
  476. {$endif}
  477. {$ifndef FPC_SYSTEM_HAS_SQRT}
  478. function fpc_sqrt_real(d:ValReal):ValReal;compilerproc;
  479. {*****************************************************************}
  480. { Square root }
  481. {*****************************************************************}
  482. { }
  483. { SYNOPSIS: }
  484. { }
  485. { double x, y, sqrt(); }
  486. { }
  487. { y = sqrt( x ); }
  488. { }
  489. { DESCRIPTION: }
  490. { }
  491. { Returns the square root of x. }
  492. { }
  493. { Range reduction involves isolating the power of two of the }
  494. { argument and using a polynomial approximation to obtain }
  495. { a rough value for the square root. Then Heron's iteration }
  496. { is used three times to converge to an accurate value. }
  497. {*****************************************************************}
  498. var e : Integer;
  499. w,z : Real;
  500. begin
  501. if( d <= 0.0 ) then
  502. begin
  503. if( d < 0.0 ) then
  504. HandleError(207);
  505. result := 0.0;
  506. end
  507. else
  508. begin
  509. w := d;
  510. { separate exponent and significand }
  511. z := frexp( d, e );
  512. { approximate square root of number between 0.5 and 1 }
  513. { relative error of approximation = 7.47e-3 }
  514. d := 4.173075996388649989089E-1 + 5.9016206709064458299663E-1 * z;
  515. { adjust for odd powers of 2 }
  516. if odd(e) then
  517. d := d*SQRT2;
  518. { re-insert exponent }
  519. d := ldexp( d, (e div 2) );
  520. { Newton iterations: }
  521. d := 0.5*(d + w/d);
  522. d := 0.5*(d + w/d);
  523. d := 0.5*(d + w/d);
  524. d := 0.5*(d + w/d);
  525. d := 0.5*(d + w/d);
  526. d := 0.5*(d + w/d);
  527. result := d;
  528. end;
  529. end;
  530. {$endif}
  531. {$ifndef FPC_SYSTEM_HAS_EXP}
  532. {$ifdef SUPPORT_DOUBLE}
  533. {
  534. This code was translated from uclib code, the original code
  535. had the following copyright notice:
  536. *
  537. * ====================================================
  538. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  539. *
  540. * Developed at SunPro, a Sun Microsystems, Inc. business.
  541. * Permission to use, copy, modify, and distribute this
  542. * software is freely granted, provided that this notice
  543. * is preserved.
  544. * ====================================================
  545. *}
  546. {*
  547. * Returns the exponential of x.
  548. *
  549. * Method
  550. * 1. Argument reduction:
  551. * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
  552. * Given x, find r and integer k such that
  553. *
  554. * x = k*ln2 + r, |r| <= 0.5*ln2.
  555. *
  556. * Here r will be represented as r = hi-lo for better
  557. * accuracy.
  558. *
  559. * 2. Approximation of exp(r) by a special rational function on
  560. * the interval [0,0.34658]:
  561. * Write
  562. * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
  563. * We use a special Reme algorithm on [0,0.34658] to generate
  564. * a polynomial of degree 5 to approximate R. The maximum error
  565. * of this polynomial approximation is bounded by 2**-59. In
  566. * other words,
  567. * R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5
  568. * (where z=r*r, and the values of P1 to P5 are listed below)
  569. * and
  570. * | 5 | -59
  571. * | 2.0+P1*z+...+P5*z - R(z) | <= 2
  572. * | |
  573. * The computation of exp(r) thus becomes
  574. * 2*r
  575. * exp(r) = 1 + -------
  576. * R - r
  577. * r*R1(r)
  578. * = 1 + r + ----------- (for better accuracy)
  579. * 2 - R1(r)
  580. * where
  581. * 2 4 10
  582. * R1(r) = r - (P1*r + P2*r + ... + P5*r ).
  583. *
  584. * 3. Scale back to obtain exp(x):
  585. * From step 1, we have
  586. * exp(x) = 2^k * exp(r)
  587. *
  588. * Special cases:
  589. * exp(INF) is INF, exp(NaN) is NaN;
  590. * exp(-INF) is 0, and
  591. * for finite argument, only exp(0)=1 is exact.
  592. *
  593. * Accuracy:
  594. * according to an error analysis, the error is always less than
  595. * 1 ulp (unit in the last place).
  596. *
  597. * Misc. info.
  598. * For IEEE double
  599. * if x > 7.09782712893383973096e+02 then exp(x) overflow
  600. * if x < -7.45133219101941108420e+02 then exp(x) underflow
  601. *
  602. * Constants:
  603. * The hexadecimal values are the intended ones for the following
  604. * constants. The decimal values may be used, provided that the
  605. * compiler will convert from decimal to binary accurately enough
  606. * to produce the hexadecimal values shown.
  607. *
  608. }
  609. function fpc_exp_real(d: ValReal):ValReal;compilerproc;
  610. const
  611. one = 1.0;
  612. halF : array[0..1] of double = (0.5,-0.5);
  613. huge = 1.0e+300;
  614. twom1000 = 9.33263618503218878990e-302; { 2**-1000=0x01700000,0}
  615. o_threshold = 7.09782712893383973096e+02; { 0x40862E42, 0xFEFA39EF }
  616. u_threshold = -7.45133219101941108420e+02; { 0xc0874910, 0xD52D3051 }
  617. ln2HI : array[0..1] of double = ( 6.93147180369123816490e-01, { 0x3fe62e42, 0xfee00000 }
  618. -6.93147180369123816490e-01); { 0xbfe62e42, 0xfee00000 }
  619. ln2LO : array[0..1] of double = (1.90821492927058770002e-10, { 0x3dea39ef, 0x35793c76 }
  620. -1.90821492927058770002e-10); { 0xbdea39ef, 0x35793c76 }
  621. invln2 = 1.44269504088896338700e+00; { 0x3ff71547, 0x652b82fe }
  622. P1 = 1.66666666666666019037e-01; { 0x3FC55555, 0x5555553E }
  623. P2 = -2.77777777770155933842e-03; { 0xBF66C16C, 0x16BEBD93 }
  624. P3 = 6.61375632143793436117e-05; { 0x3F11566A, 0xAF25DE2C }
  625. P4 = -1.65339022054652515390e-06; { 0xBEBBBD41, 0xC5D26BF1 }
  626. P5 = 4.13813679705723846039e-08; { 0x3E663769, 0x72BEA4D0 }
  627. var
  628. c,hi,lo,t,y : double;
  629. k,xsb : longint;
  630. hx,hy,lx : dword;
  631. begin
  632. hi:=0.0;
  633. lo:=0.0;
  634. k:=0;
  635. hx:=float64(d).high;
  636. xsb := (hx shr 31) and 1; { sign bit of d }
  637. hx := hx and $7fffffff; { high word of |d| }
  638. { filter out non-finite argument }
  639. if hx >= $40862E42 then
  640. begin { if |d|>=709.78... }
  641. if hx >= $7ff00000 then
  642. begin
  643. lx:=float64(d).low;
  644. if ((hx and $fffff) or lx)<>0 then
  645. begin
  646. result:=d+d; { NaN }
  647. exit;
  648. end
  649. else
  650. begin
  651. if xsb=0 then
  652. result:=d
  653. else
  654. result:=0.0; { exp(+-inf)=begininf,0end }
  655. exit;
  656. end;
  657. end;
  658. if d > o_threshold then
  659. begin
  660. result:=huge*huge; { overflow }
  661. exit;
  662. end;
  663. if d < u_threshold then
  664. begin
  665. result:=twom1000*twom1000; { underflow }
  666. exit;
  667. end;
  668. end;
  669. { argument reduction }
  670. if hx > $3fd62e42 then
  671. begin { if |d| > 0.5 ln2 }
  672. if hx < $3FF0A2B2 then { and |d| < 1.5 ln2 }
  673. begin
  674. hi := d-ln2HI[xsb];
  675. lo:=ln2LO[xsb];
  676. k := 1-xsb-xsb;
  677. end
  678. else
  679. begin
  680. k := round(invln2*d+halF[xsb]);
  681. t := k;
  682. hi := d - t*ln2HI[0]; { t*ln2HI is exact here }
  683. lo := t*ln2LO[0];
  684. end;
  685. d := hi - lo;
  686. end
  687. else if hx < $3e300000 then
  688. begin { when |d|<2**-28 }
  689. if huge+d>one then
  690. begin
  691. result:=one+d;{ trigger inexact }
  692. exit;
  693. end;
  694. end
  695. else
  696. k := 0;
  697. { d is now in primary range }
  698. t:=d*d;
  699. c:=d - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
  700. if k=0 then
  701. begin
  702. result:=one-((d*c)/(c-2.0)-d);
  703. exit;
  704. end
  705. else
  706. y := one-((lo-(d*c)/(2.0-c))-hi);
  707. if k >= -1021 then
  708. begin
  709. hy:=float64(y).high;
  710. float64(y).high:=hy+(k shl 20); { add k to y's exponent }
  711. result:=y;
  712. end
  713. else
  714. begin
  715. hy:=float64(y).high;
  716. float64(y).high:=hy+((k+1000) shl 20); { add k to y's exponent }
  717. result:=y*twom1000;
  718. end;
  719. end;
  720. {$else SUPPORT_DOUBLE}
  721. function fpc_exp_real(d: ValReal):ValReal;compilerproc;
  722. {*****************************************************************}
  723. { Exponential Function }
  724. {*****************************************************************}
  725. { }
  726. { SYNOPSIS: }
  727. { }
  728. { double x, y, exp(); }
  729. { }
  730. { y = exp( x ); }
  731. { }
  732. { DESCRIPTION: }
  733. { }
  734. { Returns e (2.71828...) raised to the x power. }
  735. { }
  736. { Range reduction is accomplished by separating the argument }
  737. { into an integer k and fraction f such that }
  738. { }
  739. { x k f }
  740. { e = 2 e. }
  741. { }
  742. { A Pade' form of degree 2/3 is used to approximate exp(f)- 1 }
  743. { in the basic range [-0.5 ln 2, 0.5 ln 2]. }
  744. {*****************************************************************}
  745. const P : TabCoef = (
  746. 1.26183092834458542160E-4,
  747. 3.02996887658430129200E-2,
  748. 1.00000000000000000000E0, 0, 0, 0, 0);
  749. Q : TabCoef = (
  750. 3.00227947279887615146E-6,
  751. 2.52453653553222894311E-3,
  752. 2.27266044198352679519E-1,
  753. 2.00000000000000000005E0, 0 ,0 ,0);
  754. C1 = 6.9335937500000000000E-1;
  755. C2 = 2.1219444005469058277E-4;
  756. var n : Integer;
  757. px, qx, xx : Real;
  758. begin
  759. if( d > MAXLOG) then
  760. HandleError(205)
  761. else
  762. if( d < MINLOG ) then
  763. begin
  764. HandleError(205);
  765. end
  766. else
  767. begin
  768. { Express e**x = e**g 2**n }
  769. { = e**g e**( n loge(2) ) }
  770. { = e**( g + n loge(2) ) }
  771. px := d * LOG2E;
  772. qx := Trunc( px + 0.5 ); { Trunc() truncates toward -infinity. }
  773. n := Trunc(qx);
  774. d := d - qx * C1;
  775. d := d + qx * C2;
  776. { rational approximation for exponential }
  777. { of the fractional part: }
  778. { e**x - 1 = 2x P(x**2)/( Q(x**2) - P(x**2) ) }
  779. xx := d * d;
  780. px := d * polevl( xx, P, 2 );
  781. d := px/( polevl( xx, Q, 3 ) - px );
  782. d := ldexp( d, 1 );
  783. d := d + 1.0;
  784. d := ldexp( d, n );
  785. result := d;
  786. end;
  787. end;
  788. {$endif SUPPORT_DOUBLE}
  789. {$endif}
  790. {$ifndef FPC_SYSTEM_HAS_ROUND}
  791. function fpc_round_real(d : ValReal) : int64;compilerproc;
  792. var
  793. fr: Real;
  794. tr: Int64;
  795. Begin
  796. fr := abs(Frac(d));
  797. tr := Trunc(d);
  798. if fr > 0.5 then
  799. if d >= 0 then
  800. result:=tr+1
  801. else
  802. result:=tr-1
  803. else
  804. if fr < 0.5 then
  805. result:=tr
  806. else { fr = 0.5 }
  807. { check sign to decide ... }
  808. { as in Turbo Pascal... }
  809. if d >= 0.0 then
  810. result:=tr+1
  811. else
  812. result:=tr;
  813. end;
  814. {$endif FPC_SYSTEM_HAS_EXP}
  815. {$ifdef FPC_CURRENCY_IS_INT64}
  816. function trunc(c : currency) : int64;
  817. type
  818. tmyrec = record
  819. i: int64;
  820. end;
  821. begin
  822. result := int64(tmyrec(c)) div 10000
  823. end;
  824. function trunc(c : comp) : int64;
  825. begin
  826. result := c
  827. end;
  828. function round(c : currency) : int64;
  829. type
  830. tmyrec = record
  831. i: int64;
  832. end;
  833. var
  834. rem, absrem: longint;
  835. begin
  836. { (int64(tmyrec(c))(+/-)5000) div 10000 can overflow }
  837. result := int64(tmyrec(c)) div 10000;
  838. rem := int64(tmyrec(c)) - result * 10000;
  839. absrem := abs(rem);
  840. if (absrem > 5000) or
  841. ((absrem = 5000) and
  842. (rem > 0)) then
  843. if (rem > 0) then
  844. inc(result)
  845. else
  846. dec(result);
  847. end;
  848. function round(c : comp) : int64;
  849. begin
  850. result := c
  851. end;
  852. {$endif FPC_CURRENCY_IS_INT64}
  853. {$ifndef FPC_SYSTEM_HAS_LN}
  854. function fpc_ln_real(d:ValReal):ValReal;compilerproc;
  855. {*****************************************************************}
  856. { Natural Logarithm }
  857. {*****************************************************************}
  858. { }
  859. { SYNOPSIS: }
  860. { }
  861. { double x, y, log(); }
  862. { }
  863. { y = ln( x ); }
  864. { }
  865. { DESCRIPTION: }
  866. { }
  867. { Returns the base e (2.718...) logarithm of x. }
  868. { }
  869. { The argument is separated into its exponent and fractional }
  870. { parts. If the exponent is between -1 and +1, the logarithm }
  871. { of the fraction is approximated by }
  872. { }
  873. { log(1+x) = x - 0.5 x**2 + x**3 P(x)/Q(x). }
  874. { }
  875. { Otherwise, setting z = 2(x-1)/x+1), }
  876. { }
  877. { log(x) = z + z**3 P(z)/Q(z). }
  878. { }
  879. {*****************************************************************}
  880. const P : TabCoef = (
  881. { Coefficients for log(1+x) = x - x**2/2 + x**3 P(x)/Q(x)
  882. 1/sqrt(2) <= x < sqrt(2) }
  883. 4.58482948458143443514E-5,
  884. 4.98531067254050724270E-1,
  885. 6.56312093769992875930E0,
  886. 2.97877425097986925891E1,
  887. 6.06127134467767258030E1,
  888. 5.67349287391754285487E1,
  889. 1.98892446572874072159E1);
  890. Q : TabCoef = (
  891. 1.50314182634250003249E1,
  892. 8.27410449222435217021E1,
  893. 2.20664384982121929218E2,
  894. 3.07254189979530058263E2,
  895. 2.14955586696422947765E2,
  896. 5.96677339718622216300E1, 0);
  897. { Coefficients for log(x) = z + z**3 P(z)/Q(z),
  898. where z = 2(x-1)/(x+1)
  899. 1/sqrt(2) <= x < sqrt(2) }
  900. R : TabCoef = (
  901. -7.89580278884799154124E-1,
  902. 1.63866645699558079767E1,
  903. -6.41409952958715622951E1, 0, 0, 0, 0);
  904. S : TabCoef = (
  905. -3.56722798256324312549E1,
  906. 3.12093766372244180303E2,
  907. -7.69691943550460008604E2, 0, 0, 0, 0);
  908. var e : Integer;
  909. z, y : Real;
  910. Label Ldone;
  911. begin
  912. if( d <= 0.0 ) then
  913. HandleError(207);
  914. d := frexp( d, e );
  915. { logarithm using log(x) = z + z**3 P(z)/Q(z),
  916. where z = 2(x-1)/x+1) }
  917. if( (e > 2) or (e < -2) ) then
  918. begin
  919. if( d < SQRTH ) then
  920. begin
  921. { 2( 2x-1 )/( 2x+1 ) }
  922. Dec(e, 1);
  923. z := d - 0.5;
  924. y := 0.5 * z + 0.5;
  925. end
  926. else
  927. begin
  928. { 2 (x-1)/(x+1) }
  929. z := d - 0.5;
  930. z := z - 0.5;
  931. y := 0.5 * d + 0.5;
  932. end;
  933. d := z / y;
  934. { /* rational form */ }
  935. z := d*d;
  936. z := d + d * ( z * polevl( z, R, 2 ) / p1evl( z, S, 3 ) );
  937. goto ldone;
  938. end;
  939. { logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) }
  940. if( d < SQRTH ) then
  941. begin
  942. Dec(e, 1);
  943. d := ldexp( d, 1 ) - 1.0; { 2x - 1 }
  944. end
  945. else
  946. d := d - 1.0;
  947. { rational form }
  948. z := d*d;
  949. y := d * ( z * polevl( d, P, 6 ) / p1evl( d, Q, 6 ) );
  950. y := y - ldexp( z, -1 ); { y - 0.5 * z }
  951. z := d + y;
  952. ldone:
  953. { recombine with exponent term }
  954. if( e <> 0 ) then
  955. begin
  956. y := e;
  957. z := z - y * 2.121944400546905827679e-4;
  958. z := z + y * 0.693359375;
  959. end;
  960. result:= z;
  961. end;
  962. {$endif}
  963. {$ifndef FPC_SYSTEM_HAS_SIN}
  964. function fpc_Sin_real(d:ValReal):ValReal;compilerproc;
  965. {*****************************************************************}
  966. { Circular Sine }
  967. {*****************************************************************}
  968. { }
  969. { SYNOPSIS: }
  970. { }
  971. { double x, y, sin(); }
  972. { }
  973. { y = sin( x ); }
  974. { }
  975. { DESCRIPTION: }
  976. { }
  977. { Range reduction is into intervals of pi/4. The reduction }
  978. { error is nearly eliminated by contriving an extended }
  979. { precision modular arithmetic. }
  980. { }
  981. { Two polynomial approximating functions are employed. }
  982. { Between 0 and pi/4 the sine is approximated by }
  983. { x + x**3 P(x**2). }
  984. { Between pi/4 and pi/2 the cosine is represented as }
  985. { 1 - x**2 Q(x**2). }
  986. {*****************************************************************}
  987. var y, z, zz : Real;
  988. j, sign : Integer;
  989. begin
  990. { make argument positive but save the sign }
  991. sign := 1;
  992. if( d < 0 ) then
  993. begin
  994. d := -d;
  995. sign := -1;
  996. end;
  997. { above this value, approximate towards 0 }
  998. if( d > lossth ) then
  999. begin
  1000. result := 0.0;
  1001. exit;
  1002. end;
  1003. y := Trunc( d/PIO4 ); { integer part of x/PIO4 }
  1004. { strip high bits of integer part to prevent integer overflow }
  1005. z := ldexp( y, -4 );
  1006. z := Trunc(z); { integer part of y/8 }
  1007. z := y - ldexp( z, 4 ); { y - 16 * (y/16) }
  1008. j := Trunc(z); { convert to integer for tests on the phase angle }
  1009. { map zeros to origin }
  1010. { typecast is to avoid "can't determine which overloaded function }
  1011. { to call" }
  1012. if odd( longint(j) ) then
  1013. begin
  1014. inc(j);
  1015. y := y + 1.0;
  1016. end;
  1017. j := j and 7; { octant modulo 360 degrees }
  1018. { reflect in x axis }
  1019. if( j > 3) then
  1020. begin
  1021. sign := -sign;
  1022. dec(j, 4);
  1023. end;
  1024. { Extended precision modular arithmetic }
  1025. z := ((d - y * DP1) - y * DP2) - y * DP3;
  1026. zz := z * z;
  1027. if( (j=1) or (j=2) ) then
  1028. y := 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 )
  1029. else
  1030. { y = z + z * (zz * polevl( zz, sincof, 5 )); }
  1031. y := z + z * z * z * polevl( zz, sincof, 5 );
  1032. if(sign < 0) then
  1033. y := -y;
  1034. result := y;
  1035. end;
  1036. {$endif}
  1037. {$ifndef FPC_SYSTEM_HAS_COS}
  1038. function fpc_Cos_real(d:ValReal):ValReal;compilerproc;
  1039. {*****************************************************************}
  1040. { Circular cosine }
  1041. {*****************************************************************}
  1042. { }
  1043. { Circular cosine }
  1044. { }
  1045. { SYNOPSIS: }
  1046. { }
  1047. { double x, y, cos(); }
  1048. { }
  1049. { y = cos( x ); }
  1050. { }
  1051. { DESCRIPTION: }
  1052. { }
  1053. { Range reduction is into intervals of pi/4. The reduction }
  1054. { error is nearly eliminated by contriving an extended }
  1055. { precision modular arithmetic. }
  1056. { }
  1057. { Two polynomial approximating functions are employed. }
  1058. { Between 0 and pi/4 the cosine is approximated by }
  1059. { 1 - x**2 Q(x**2). }
  1060. { Between pi/4 and pi/2 the sine is represented as }
  1061. { x + x**3 P(x**2). }
  1062. {*****************************************************************}
  1063. var y, z, zz : Real;
  1064. j, sign : Integer;
  1065. i : LongInt;
  1066. begin
  1067. { make argument positive }
  1068. sign := 1;
  1069. if( d < 0 ) then
  1070. d := -d;
  1071. { above this value, round towards zero }
  1072. if( d > lossth ) then
  1073. begin
  1074. result := 0.0;
  1075. exit;
  1076. end;
  1077. y := Trunc( d/PIO4 );
  1078. z := ldexp( y, -4 );
  1079. z := Trunc(z); { integer part of y/8 }
  1080. z := y - ldexp( z, 4 ); { y - 16 * (y/16) }
  1081. { integer and fractional part modulo one octant }
  1082. i := Trunc(z);
  1083. if odd( i ) then { map zeros to origin }
  1084. begin
  1085. inc(i);
  1086. y := y + 1.0;
  1087. end;
  1088. j := i and 07;
  1089. if( j > 3) then
  1090. begin
  1091. dec(j,4);
  1092. sign := -sign;
  1093. end;
  1094. if( j > 1 ) then
  1095. sign := -sign;
  1096. { Extended precision modular arithmetic }
  1097. z := ((d - y * DP1) - y * DP2) - y * DP3;
  1098. zz := z * z;
  1099. if( (j=1) or (j=2) ) then
  1100. { y = z + z * (zz * polevl( zz, sincof, 5 )); }
  1101. y := z + z * z * z * polevl( zz, sincof, 5 )
  1102. else
  1103. y := 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 );
  1104. if(sign < 0) then
  1105. y := -y;
  1106. result := y ;
  1107. end;
  1108. {$endif}
  1109. {$ifndef FPC_SYSTEM_HAS_ARCTAN}
  1110. function fpc_ArcTan_real(d:ValReal):ValReal;compilerproc;
  1111. {*****************************************************************}
  1112. { Inverse circular tangent (arctangent) }
  1113. {*****************************************************************}
  1114. { }
  1115. { SYNOPSIS: }
  1116. { }
  1117. { double x, y, atan(); }
  1118. { }
  1119. { y = atan( x ); }
  1120. { }
  1121. { DESCRIPTION: }
  1122. { }
  1123. { Returns radian angle between -pi/2 and +pi/2 whose tangent }
  1124. { is x. }
  1125. { }
  1126. { Range reduction is from four intervals into the interval }
  1127. { from zero to tan( pi/8 ). The approximant uses a rational }
  1128. { function of degree 3/4 of the form x + x**3 P(x)/Q(x). }
  1129. {*****************************************************************}
  1130. const P : TabCoef = (
  1131. -8.40980878064499716001E-1,
  1132. -8.83860837023772394279E0,
  1133. -2.18476213081316705724E1,
  1134. -1.48307050340438946993E1, 0, 0, 0);
  1135. Q : TabCoef = (
  1136. 1.54974124675307267552E1,
  1137. 6.27906555762653017263E1,
  1138. 9.22381329856214406485E1,
  1139. 4.44921151021319438465E1, 0, 0, 0);
  1140. { tan( 3*pi/8 ) }
  1141. T3P8 = 2.41421356237309504880;
  1142. { tan( pi/8 ) }
  1143. TP8 = 0.41421356237309504880;
  1144. var y,z : Real;
  1145. Sign : Integer;
  1146. begin
  1147. { make argument positive and save the sign }
  1148. sign := 1;
  1149. if( d < 0.0 ) then
  1150. begin
  1151. sign := -1;
  1152. d := -d;
  1153. end;
  1154. { range reduction }
  1155. if( d > T3P8 ) then
  1156. begin
  1157. y := PIO2;
  1158. d := -( 1.0/d );
  1159. end
  1160. else if( d > TP8 ) then
  1161. begin
  1162. y := PIO4;
  1163. d := (d-1.0)/(d+1.0);
  1164. end
  1165. else
  1166. y := 0.0;
  1167. { rational form in x**2 }
  1168. z := d * d;
  1169. y := y + ( polevl( z, P, 3 ) / p1evl( z, Q, 4 ) ) * z * d + d;
  1170. if( sign < 0 ) then
  1171. y := -y;
  1172. result := y;
  1173. end;
  1174. {$endif}
  1175. {$ifndef FPC_SYSTEM_HAS_FRAC}
  1176. function fpc_frac_real(d : ValReal) : ValReal;compilerproc;
  1177. begin
  1178. result := d - Int(d);
  1179. end;
  1180. {$endif}
  1181. {$ifdef FPC_INCLUDE_SOFTWARE_INT64_TO_DOUBLE}
  1182. {$ifndef FPC_SYSTEM_HAS_QWORD_TO_DOUBLE}
  1183. function fpc_qword_to_double(q : qword): double; compilerproc;
  1184. begin
  1185. result:=dword(q and $ffffffff)+dword(q shr 32)*4294967296.0;
  1186. end;
  1187. {$endif FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  1188. {$ifndef FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  1189. function fpc_int64_to_double(i : int64): double; compilerproc;
  1190. begin
  1191. if i<0 then
  1192. result:=-double(qword(-i))
  1193. else
  1194. result:=qword(i);
  1195. end;
  1196. {$endif FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  1197. {$endif FPC_INCLUDE_SOFTWARE_INT64_TO_DOUBLE}
  1198. {$ifdef SUPPORT_DOUBLE}
  1199. {****************************************************************************
  1200. Helper routines to support old TP styled reals
  1201. ****************************************************************************}
  1202. {$ifndef FPC_SYSTEM_HAS_REAL2DOUBLE}
  1203. function real2double(r : real48) : double;
  1204. var
  1205. res : array[0..7] of byte;
  1206. exponent : word;
  1207. begin
  1208. { copy mantissa }
  1209. res[0]:=0;
  1210. res[1]:=r[1] shl 5;
  1211. res[2]:=(r[1] shr 3) or (r[2] shl 5);
  1212. res[3]:=(r[2] shr 3) or (r[3] shl 5);
  1213. res[4]:=(r[3] shr 3) or (r[4] shl 5);
  1214. res[5]:=(r[4] shr 3) or (r[5] and $7f) shl 5;
  1215. res[6]:=(r[5] and $7f) shr 3;
  1216. { copy exponent }
  1217. { correct exponent: }
  1218. exponent:=(word(r[0])+(1023-129));
  1219. res[6]:=res[6] or ((exponent and $f) shl 4);
  1220. res[7]:=exponent shr 4;
  1221. { set sign }
  1222. res[7]:=res[7] or (r[5] and $80);
  1223. real2double:=double(res);
  1224. end;
  1225. {$endif FPC_SYSTEM_HAS_REAL2DOUBLE}
  1226. {$endif SUPPORT_DOUBLE}
  1227. {$ifdef SUPPORT_EXTENDED}
  1228. { fast 10^n routine }
  1229. function FPower10(val: Extended; Power: Longint): Extended;
  1230. const
  1231. pow32 : array[0..31] of extended =
  1232. (
  1233. 1e0,1e1,1e2,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,
  1234. 1e11,1e12,1e13,1e14,1e15,1e16,1e17,1e18,1e19,1e20,
  1235. 1e21,1e22,1e23,1e24,1e25,1e26,1e27,1e28,1e29,1e30,
  1236. 1e31
  1237. );
  1238. pow512 : array[0..15] of extended =
  1239. (
  1240. 1,1e32,1e64,1e96,1e128,1e160,1e192,1e224,
  1241. 1e256,1e288,1e320,1e352,1e384,1e416,1e448,
  1242. 1e480
  1243. );
  1244. pow4096 : array[0..9] of extended =
  1245. (1,1e512,1e1024,1e1536,
  1246. 1e2048,1e2560,1e3072,1e3584,
  1247. 1e4096,1e4608
  1248. );
  1249. negpow32 : array[0..31] of extended =
  1250. (
  1251. 1e-0,1e-1,1e-2,1e-3,1e-4,1e-5,1e-6,1e-7,1e-8,1e-9,1e-10,
  1252. 1e-11,1e-12,1e-13,1e-14,1e-15,1e-16,1e-17,1e-18,1e-19,1e-20,
  1253. 1e-21,1e-22,1e-23,1e-24,1e-25,1e-26,1e-27,1e-28,1e-29,1e-30,
  1254. 1e-31
  1255. );
  1256. negpow512 : array[0..15] of extended =
  1257. (
  1258. 0,1e-32,1e-64,1e-96,1e-128,1e-160,1e-192,1e-224,
  1259. 1e-256,1e-288,1e-320,1e-352,1e-384,1e-416,1e-448,
  1260. 1e-480
  1261. );
  1262. negpow4096 : array[0..9] of extended =
  1263. (
  1264. 0,1e-512,1e-1024,1e-1536,
  1265. 1e-2048,1e-2560,1e-3072,1e-3584,
  1266. 1e-4096,1e-4608
  1267. );
  1268. begin
  1269. if Power<0 then
  1270. begin
  1271. Power:=-Power;
  1272. result:=val*negpow32[Power and $1f];
  1273. power:=power shr 5;
  1274. if power<>0 then
  1275. begin
  1276. result:=result*negpow512[Power and $f];
  1277. power:=power shr 4;
  1278. if power<>0 then
  1279. begin
  1280. if power<=9 then
  1281. result:=result*negpow4096[Power]
  1282. else
  1283. result:=1.0/0.0;
  1284. end;
  1285. end;
  1286. end
  1287. else
  1288. begin
  1289. result:=val*pow32[Power and $1f];
  1290. power:=power shr 5;
  1291. if power<>0 then
  1292. begin
  1293. result:=result*pow512[Power and $f];
  1294. power:=power shr 4;
  1295. if power<>0 then
  1296. begin
  1297. if power<=9 then
  1298. result:=result*pow4096[Power]
  1299. else
  1300. result:=1.0/0.0;
  1301. end;
  1302. end;
  1303. end;
  1304. end;
  1305. {$endif SUPPORT_EXTENDED}