genmath.inc 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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 FPC_DOUBLE_HILO_SWAPPED}
  262. l: longint;
  263. {$endif FPC_DOUBLE_HILO_SWAPPED}
  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 FPC_DOUBLE_HILO_SWAPPED}
  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 FPC_DOUBLE_HILO_SWAPPED}
  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 FPC_DOUBLE_HILO_SWAPPED}
  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 FPC_DOUBLE_HILO_SWAPPED}
  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 FPC_DOUBLE_HILO_SWAPPED}
  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 FPC_DOUBLE_HILO_SWAPPED}
  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. HandleError(205); { overflow }
  660. if d < u_threshold then
  661. HandleError(206); { underflow }
  662. end;
  663. { argument reduction }
  664. if hx > $3fd62e42 then
  665. begin { if |d| > 0.5 ln2 }
  666. if hx < $3FF0A2B2 then { and |d| < 1.5 ln2 }
  667. begin
  668. hi := d-ln2HI[xsb];
  669. lo:=ln2LO[xsb];
  670. k := 1-xsb-xsb;
  671. end
  672. else
  673. begin
  674. k := round(invln2*d+halF[xsb]);
  675. t := k;
  676. hi := d - t*ln2HI[0]; { t*ln2HI is exact here }
  677. lo := t*ln2LO[0];
  678. end;
  679. d := hi - lo;
  680. end
  681. else if hx < $3e300000 then
  682. begin { when |d|<2**-28 }
  683. if huge+d>one then
  684. begin
  685. result:=one+d;{ trigger inexact }
  686. exit;
  687. end;
  688. end
  689. else
  690. k := 0;
  691. { d is now in primary range }
  692. t:=d*d;
  693. c:=d - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
  694. if k=0 then
  695. begin
  696. result:=one-((d*c)/(c-2.0)-d);
  697. exit;
  698. end
  699. else
  700. y := one-((lo-(d*c)/(2.0-c))-hi);
  701. if k >= -1021 then
  702. begin
  703. hy:=float64(y).high;
  704. float64(y).high:=longint(hy)+(k shl 20); { add k to y's exponent }
  705. result:=y;
  706. end
  707. else
  708. begin
  709. hy:=float64(y).high;
  710. float64(y).high:=longint(hy)+((k+1000) shl 20); { add k to y's exponent }
  711. result:=y*twom1000;
  712. end;
  713. end;
  714. {$else SUPPORT_DOUBLE}
  715. function fpc_exp_real(d: ValReal):ValReal;compilerproc;
  716. {*****************************************************************}
  717. { Exponential Function }
  718. {*****************************************************************}
  719. { }
  720. { SYNOPSIS: }
  721. { }
  722. { double x, y, exp(); }
  723. { }
  724. { y = exp( x ); }
  725. { }
  726. { DESCRIPTION: }
  727. { }
  728. { Returns e (2.71828...) raised to the x power. }
  729. { }
  730. { Range reduction is accomplished by separating the argument }
  731. { into an integer k and fraction f such that }
  732. { }
  733. { x k f }
  734. { e = 2 e. }
  735. { }
  736. { A Pade' form of degree 2/3 is used to approximate exp(f)- 1 }
  737. { in the basic range [-0.5 ln 2, 0.5 ln 2]. }
  738. {*****************************************************************}
  739. const P : TabCoef = (
  740. 1.26183092834458542160E-4,
  741. 3.02996887658430129200E-2,
  742. 1.00000000000000000000E0, 0, 0, 0, 0);
  743. Q : TabCoef = (
  744. 3.00227947279887615146E-6,
  745. 2.52453653553222894311E-3,
  746. 2.27266044198352679519E-1,
  747. 2.00000000000000000005E0, 0 ,0 ,0);
  748. C1 = 6.9335937500000000000E-1;
  749. C2 = 2.1219444005469058277E-4;
  750. var n : Integer;
  751. px, qx, xx : Real;
  752. begin
  753. if( d > MAXLOG) then
  754. HandleError(205)
  755. else
  756. if( d < MINLOG ) then
  757. begin
  758. HandleError(205);
  759. end
  760. else
  761. begin
  762. { Express e**x = e**g 2**n }
  763. { = e**g e**( n loge(2) ) }
  764. { = e**( g + n loge(2) ) }
  765. px := d * LOG2E;
  766. qx := Trunc( px + 0.5 ); { Trunc() truncates toward -infinity. }
  767. n := Trunc(qx);
  768. d := d - qx * C1;
  769. d := d + qx * C2;
  770. { rational approximation for exponential }
  771. { of the fractional part: }
  772. { e**x - 1 = 2x P(x**2)/( Q(x**2) - P(x**2) ) }
  773. xx := d * d;
  774. px := d * polevl( xx, P, 2 );
  775. d := px/( polevl( xx, Q, 3 ) - px );
  776. d := ldexp( d, 1 );
  777. d := d + 1.0;
  778. d := ldexp( d, n );
  779. result := d;
  780. end;
  781. end;
  782. {$endif SUPPORT_DOUBLE}
  783. {$endif}
  784. {$ifndef FPC_SYSTEM_HAS_ROUND}
  785. function fpc_round_real(d : ValReal) : int64;compilerproc;
  786. var
  787. fr: Real;
  788. tr: Int64;
  789. Begin
  790. fr := abs(Frac(d));
  791. tr := Trunc(d);
  792. if fr > 0.5 then
  793. if d >= 0 then
  794. result:=tr+1
  795. else
  796. result:=tr-1
  797. else
  798. if fr < 0.5 then
  799. result:=tr
  800. else { fr = 0.5 }
  801. { check sign to decide ... }
  802. { as in Turbo Pascal... }
  803. if d >= 0.0 then
  804. result:=tr+1
  805. else
  806. result:=tr;
  807. end;
  808. {$endif FPC_SYSTEM_HAS_EXP}
  809. {$ifdef FPC_CURRENCY_IS_INT64}
  810. function trunc(c : currency) : int64;
  811. type
  812. tmyrec = record
  813. i: int64;
  814. end;
  815. begin
  816. result := int64(tmyrec(c)) div 10000
  817. end;
  818. function trunc(c : comp) : int64;
  819. begin
  820. result := c
  821. end;
  822. function round(c : currency) : int64;
  823. type
  824. tmyrec = record
  825. i: int64;
  826. end;
  827. var
  828. rem, absrem: longint;
  829. begin
  830. { (int64(tmyrec(c))(+/-)5000) div 10000 can overflow }
  831. result := int64(tmyrec(c)) div 10000;
  832. rem := int64(tmyrec(c)) - result * 10000;
  833. absrem := abs(rem);
  834. if (absrem > 5000) or
  835. ((absrem = 5000) and
  836. (rem > 0)) then
  837. if (rem > 0) then
  838. inc(result)
  839. else
  840. dec(result);
  841. end;
  842. function round(c : comp) : int64;
  843. begin
  844. result := c
  845. end;
  846. {$endif FPC_CURRENCY_IS_INT64}
  847. {$ifndef FPC_SYSTEM_HAS_LN}
  848. function fpc_ln_real(d:ValReal):ValReal;compilerproc;
  849. {*****************************************************************}
  850. { Natural Logarithm }
  851. {*****************************************************************}
  852. { }
  853. { SYNOPSIS: }
  854. { }
  855. { double x, y, log(); }
  856. { }
  857. { y = ln( x ); }
  858. { }
  859. { DESCRIPTION: }
  860. { }
  861. { Returns the base e (2.718...) logarithm of x. }
  862. { }
  863. { The argument is separated into its exponent and fractional }
  864. { parts. If the exponent is between -1 and +1, the logarithm }
  865. { of the fraction is approximated by }
  866. { }
  867. { log(1+x) = x - 0.5 x**2 + x**3 P(x)/Q(x). }
  868. { }
  869. { Otherwise, setting z = 2(x-1)/x+1), }
  870. { }
  871. { log(x) = z + z**3 P(z)/Q(z). }
  872. { }
  873. {*****************************************************************}
  874. const P : TabCoef = (
  875. { Coefficients for log(1+x) = x - x**2/2 + x**3 P(x)/Q(x)
  876. 1/sqrt(2) <= x < sqrt(2) }
  877. 4.58482948458143443514E-5,
  878. 4.98531067254050724270E-1,
  879. 6.56312093769992875930E0,
  880. 2.97877425097986925891E1,
  881. 6.06127134467767258030E1,
  882. 5.67349287391754285487E1,
  883. 1.98892446572874072159E1);
  884. Q : TabCoef = (
  885. 1.50314182634250003249E1,
  886. 8.27410449222435217021E1,
  887. 2.20664384982121929218E2,
  888. 3.07254189979530058263E2,
  889. 2.14955586696422947765E2,
  890. 5.96677339718622216300E1, 0);
  891. { Coefficients for log(x) = z + z**3 P(z)/Q(z),
  892. where z = 2(x-1)/(x+1)
  893. 1/sqrt(2) <= x < sqrt(2) }
  894. R : TabCoef = (
  895. -7.89580278884799154124E-1,
  896. 1.63866645699558079767E1,
  897. -6.41409952958715622951E1, 0, 0, 0, 0);
  898. S : TabCoef = (
  899. -3.56722798256324312549E1,
  900. 3.12093766372244180303E2,
  901. -7.69691943550460008604E2, 0, 0, 0, 0);
  902. var e : Integer;
  903. z, y : Real;
  904. Label Ldone;
  905. begin
  906. if( d <= 0.0 ) then
  907. HandleError(207);
  908. d := frexp( d, e );
  909. { logarithm using log(x) = z + z**3 P(z)/Q(z),
  910. where z = 2(x-1)/x+1) }
  911. if( (e > 2) or (e < -2) ) then
  912. begin
  913. if( d < SQRTH ) then
  914. begin
  915. { 2( 2x-1 )/( 2x+1 ) }
  916. Dec(e, 1);
  917. z := d - 0.5;
  918. y := 0.5 * z + 0.5;
  919. end
  920. else
  921. begin
  922. { 2 (x-1)/(x+1) }
  923. z := d - 0.5;
  924. z := z - 0.5;
  925. y := 0.5 * d + 0.5;
  926. end;
  927. d := z / y;
  928. { /* rational form */ }
  929. z := d*d;
  930. z := d + d * ( z * polevl( z, R, 2 ) / p1evl( z, S, 3 ) );
  931. goto ldone;
  932. end;
  933. { logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) }
  934. if( d < SQRTH ) then
  935. begin
  936. Dec(e, 1);
  937. d := ldexp( d, 1 ) - 1.0; { 2x - 1 }
  938. end
  939. else
  940. d := d - 1.0;
  941. { rational form }
  942. z := d*d;
  943. y := d * ( z * polevl( d, P, 6 ) / p1evl( d, Q, 6 ) );
  944. y := y - ldexp( z, -1 ); { y - 0.5 * z }
  945. z := d + y;
  946. ldone:
  947. { recombine with exponent term }
  948. if( e <> 0 ) then
  949. begin
  950. y := e;
  951. z := z - y * 2.121944400546905827679e-4;
  952. z := z + y * 0.693359375;
  953. end;
  954. result:= z;
  955. end;
  956. {$endif}
  957. {$ifndef FPC_SYSTEM_HAS_SIN}
  958. function fpc_Sin_real(d:ValReal):ValReal;compilerproc;
  959. {*****************************************************************}
  960. { Circular Sine }
  961. {*****************************************************************}
  962. { }
  963. { SYNOPSIS: }
  964. { }
  965. { double x, y, sin(); }
  966. { }
  967. { y = sin( x ); }
  968. { }
  969. { DESCRIPTION: }
  970. { }
  971. { Range reduction is into intervals of pi/4. The reduction }
  972. { error is nearly eliminated by contriving an extended }
  973. { precision modular arithmetic. }
  974. { }
  975. { Two polynomial approximating functions are employed. }
  976. { Between 0 and pi/4 the sine is approximated by }
  977. { x + x**3 P(x**2). }
  978. { Between pi/4 and pi/2 the cosine is represented as }
  979. { 1 - x**2 Q(x**2). }
  980. {*****************************************************************}
  981. var y, z, zz : Real;
  982. j, sign : Integer;
  983. begin
  984. { make argument positive but save the sign }
  985. sign := 1;
  986. if( d < 0 ) then
  987. begin
  988. d := -d;
  989. sign := -1;
  990. end;
  991. { above this value, approximate towards 0 }
  992. if( d > lossth ) then
  993. begin
  994. result := 0.0;
  995. exit;
  996. end;
  997. y := Trunc( d/PIO4 ); { integer part of x/PIO4 }
  998. { strip high bits of integer part to prevent integer overflow }
  999. z := ldexp( y, -4 );
  1000. z := Trunc(z); { integer part of y/8 }
  1001. z := y - ldexp( z, 4 ); { y - 16 * (y/16) }
  1002. j := Trunc(z); { convert to integer for tests on the phase angle }
  1003. { map zeros to origin }
  1004. { typecast is to avoid "can't determine which overloaded function }
  1005. { to call" }
  1006. if odd( longint(j) ) then
  1007. begin
  1008. inc(j);
  1009. y := y + 1.0;
  1010. end;
  1011. j := j and 7; { octant modulo 360 degrees }
  1012. { reflect in x axis }
  1013. if( j > 3) then
  1014. begin
  1015. sign := -sign;
  1016. dec(j, 4);
  1017. end;
  1018. { Extended precision modular arithmetic }
  1019. z := ((d - y * DP1) - y * DP2) - y * DP3;
  1020. zz := z * z;
  1021. if( (j=1) or (j=2) ) then
  1022. y := 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 )
  1023. else
  1024. { y = z + z * (zz * polevl( zz, sincof, 5 )); }
  1025. y := z + z * z * z * polevl( zz, sincof, 5 );
  1026. if(sign < 0) then
  1027. y := -y;
  1028. result := y;
  1029. end;
  1030. {$endif}
  1031. {$ifndef FPC_SYSTEM_HAS_COS}
  1032. function fpc_Cos_real(d:ValReal):ValReal;compilerproc;
  1033. {*****************************************************************}
  1034. { Circular cosine }
  1035. {*****************************************************************}
  1036. { }
  1037. { Circular cosine }
  1038. { }
  1039. { SYNOPSIS: }
  1040. { }
  1041. { double x, y, cos(); }
  1042. { }
  1043. { y = cos( x ); }
  1044. { }
  1045. { DESCRIPTION: }
  1046. { }
  1047. { Range reduction is into intervals of pi/4. The reduction }
  1048. { error is nearly eliminated by contriving an extended }
  1049. { precision modular arithmetic. }
  1050. { }
  1051. { Two polynomial approximating functions are employed. }
  1052. { Between 0 and pi/4 the cosine is approximated by }
  1053. { 1 - x**2 Q(x**2). }
  1054. { Between pi/4 and pi/2 the sine is represented as }
  1055. { x + x**3 P(x**2). }
  1056. {*****************************************************************}
  1057. var y, z, zz : Real;
  1058. j, sign : Integer;
  1059. i : LongInt;
  1060. begin
  1061. { make argument positive }
  1062. sign := 1;
  1063. if( d < 0 ) then
  1064. d := -d;
  1065. { above this value, round towards zero }
  1066. if( d > lossth ) then
  1067. begin
  1068. result := 0.0;
  1069. exit;
  1070. end;
  1071. y := Trunc( d/PIO4 );
  1072. z := ldexp( y, -4 );
  1073. z := Trunc(z); { integer part of y/8 }
  1074. z := y - ldexp( z, 4 ); { y - 16 * (y/16) }
  1075. { integer and fractional part modulo one octant }
  1076. i := Trunc(z);
  1077. if odd( i ) then { map zeros to origin }
  1078. begin
  1079. inc(i);
  1080. y := y + 1.0;
  1081. end;
  1082. j := i and 07;
  1083. if( j > 3) then
  1084. begin
  1085. dec(j,4);
  1086. sign := -sign;
  1087. end;
  1088. if( j > 1 ) then
  1089. sign := -sign;
  1090. { Extended precision modular arithmetic }
  1091. z := ((d - y * DP1) - y * DP2) - y * DP3;
  1092. zz := z * z;
  1093. if( (j=1) or (j=2) ) then
  1094. { y = z + z * (zz * polevl( zz, sincof, 5 )); }
  1095. y := z + z * z * z * polevl( zz, sincof, 5 )
  1096. else
  1097. y := 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 );
  1098. if(sign < 0) then
  1099. y := -y;
  1100. result := y ;
  1101. end;
  1102. {$endif}
  1103. {$ifndef FPC_SYSTEM_HAS_ARCTAN}
  1104. function fpc_ArcTan_real(d:ValReal):ValReal;compilerproc;
  1105. {*****************************************************************}
  1106. { Inverse circular tangent (arctangent) }
  1107. {*****************************************************************}
  1108. { }
  1109. { SYNOPSIS: }
  1110. { }
  1111. { double x, y, atan(); }
  1112. { }
  1113. { y = atan( x ); }
  1114. { }
  1115. { DESCRIPTION: }
  1116. { }
  1117. { Returns radian angle between -pi/2 and +pi/2 whose tangent }
  1118. { is x. }
  1119. { }
  1120. { Range reduction is from four intervals into the interval }
  1121. { from zero to tan( pi/8 ). The approximant uses a rational }
  1122. { function of degree 3/4 of the form x + x**3 P(x)/Q(x). }
  1123. {*****************************************************************}
  1124. const P : TabCoef = (
  1125. -8.40980878064499716001E-1,
  1126. -8.83860837023772394279E0,
  1127. -2.18476213081316705724E1,
  1128. -1.48307050340438946993E1, 0, 0, 0);
  1129. Q : TabCoef = (
  1130. 1.54974124675307267552E1,
  1131. 6.27906555762653017263E1,
  1132. 9.22381329856214406485E1,
  1133. 4.44921151021319438465E1, 0, 0, 0);
  1134. { tan( 3*pi/8 ) }
  1135. T3P8 = 2.41421356237309504880;
  1136. { tan( pi/8 ) }
  1137. TP8 = 0.41421356237309504880;
  1138. var y,z : Real;
  1139. Sign : Integer;
  1140. begin
  1141. { make argument positive and save the sign }
  1142. sign := 1;
  1143. if( d < 0.0 ) then
  1144. begin
  1145. sign := -1;
  1146. d := -d;
  1147. end;
  1148. { range reduction }
  1149. if( d > T3P8 ) then
  1150. begin
  1151. y := PIO2;
  1152. d := -( 1.0/d );
  1153. end
  1154. else if( d > TP8 ) then
  1155. begin
  1156. y := PIO4;
  1157. d := (d-1.0)/(d+1.0);
  1158. end
  1159. else
  1160. y := 0.0;
  1161. { rational form in x**2 }
  1162. z := d * d;
  1163. y := y + ( polevl( z, P, 3 ) / p1evl( z, Q, 4 ) ) * z * d + d;
  1164. if( sign < 0 ) then
  1165. y := -y;
  1166. result := y;
  1167. end;
  1168. {$endif}
  1169. {$ifndef FPC_SYSTEM_HAS_FRAC}
  1170. function fpc_frac_real(d : ValReal) : ValReal;compilerproc;
  1171. begin
  1172. result := d - Int(d);
  1173. end;
  1174. {$endif}
  1175. {$ifdef FPC_INCLUDE_SOFTWARE_INT64_TO_DOUBLE}
  1176. {$ifndef FPC_SYSTEM_HAS_QWORD_TO_DOUBLE}
  1177. function fpc_qword_to_double(q : qword): double; compilerproc;
  1178. begin
  1179. result:=dword(q and $ffffffff)+dword(q shr 32)*4294967296.0;
  1180. end;
  1181. {$endif FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  1182. {$ifndef FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  1183. function fpc_int64_to_double(i : int64): double; compilerproc;
  1184. begin
  1185. if i<0 then
  1186. result:=-double(qword(-i))
  1187. else
  1188. result:=qword(i);
  1189. end;
  1190. {$endif FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  1191. {$endif FPC_INCLUDE_SOFTWARE_INT64_TO_DOUBLE}
  1192. {$ifdef SUPPORT_DOUBLE}
  1193. {****************************************************************************
  1194. Helper routines to support old TP styled reals
  1195. ****************************************************************************}
  1196. {$ifndef FPC_SYSTEM_HAS_REAL2DOUBLE}
  1197. function real2double(r : real48) : double;
  1198. var
  1199. res : array[0..7] of byte;
  1200. exponent : word;
  1201. begin
  1202. { copy mantissa }
  1203. res[0]:=0;
  1204. res[1]:=r[1] shl 5;
  1205. res[2]:=(r[1] shr 3) or (r[2] shl 5);
  1206. res[3]:=(r[2] shr 3) or (r[3] shl 5);
  1207. res[4]:=(r[3] shr 3) or (r[4] shl 5);
  1208. res[5]:=(r[4] shr 3) or (r[5] and $7f) shl 5;
  1209. res[6]:=(r[5] and $7f) shr 3;
  1210. { copy exponent }
  1211. { correct exponent: }
  1212. exponent:=(word(r[0])+(1023-129));
  1213. res[6]:=res[6] or ((exponent and $f) shl 4);
  1214. res[7]:=exponent shr 4;
  1215. { set sign }
  1216. res[7]:=res[7] or (r[5] and $80);
  1217. real2double:=double(res);
  1218. end;
  1219. {$endif FPC_SYSTEM_HAS_REAL2DOUBLE}
  1220. {$endif SUPPORT_DOUBLE}
  1221. {$ifdef SUPPORT_EXTENDED}
  1222. { fast 10^n routine }
  1223. function FPower10(val: Extended; Power: Longint): Extended;
  1224. const
  1225. pow32 : array[0..31] of extended =
  1226. (
  1227. 1e0,1e1,1e2,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,
  1228. 1e11,1e12,1e13,1e14,1e15,1e16,1e17,1e18,1e19,1e20,
  1229. 1e21,1e22,1e23,1e24,1e25,1e26,1e27,1e28,1e29,1e30,
  1230. 1e31
  1231. );
  1232. pow512 : array[0..15] of extended =
  1233. (
  1234. 1,1e32,1e64,1e96,1e128,1e160,1e192,1e224,
  1235. 1e256,1e288,1e320,1e352,1e384,1e416,1e448,
  1236. 1e480
  1237. );
  1238. pow4096 : array[0..9] of extended =
  1239. (1,1e512,1e1024,1e1536,
  1240. 1e2048,1e2560,1e3072,1e3584,
  1241. 1e4096,1e4608
  1242. );
  1243. negpow32 : array[0..31] of extended =
  1244. (
  1245. 1e-0,1e-1,1e-2,1e-3,1e-4,1e-5,1e-6,1e-7,1e-8,1e-9,1e-10,
  1246. 1e-11,1e-12,1e-13,1e-14,1e-15,1e-16,1e-17,1e-18,1e-19,1e-20,
  1247. 1e-21,1e-22,1e-23,1e-24,1e-25,1e-26,1e-27,1e-28,1e-29,1e-30,
  1248. 1e-31
  1249. );
  1250. negpow512 : array[0..15] of extended =
  1251. (
  1252. 0,1e-32,1e-64,1e-96,1e-128,1e-160,1e-192,1e-224,
  1253. 1e-256,1e-288,1e-320,1e-352,1e-384,1e-416,1e-448,
  1254. 1e-480
  1255. );
  1256. negpow4096 : array[0..9] of extended =
  1257. (
  1258. 0,1e-512,1e-1024,1e-1536,
  1259. 1e-2048,1e-2560,1e-3072,1e-3584,
  1260. 1e-4096,1e-4608
  1261. );
  1262. begin
  1263. if Power<0 then
  1264. begin
  1265. Power:=-Power;
  1266. result:=val*negpow32[Power and $1f];
  1267. power:=power shr 5;
  1268. if power<>0 then
  1269. begin
  1270. result:=result*negpow512[Power and $f];
  1271. power:=power shr 4;
  1272. if power<>0 then
  1273. begin
  1274. if power<=9 then
  1275. result:=result*negpow4096[Power]
  1276. else
  1277. result:=1.0/0.0;
  1278. end;
  1279. end;
  1280. end
  1281. else
  1282. begin
  1283. result:=val*pow32[Power and $1f];
  1284. power:=power shr 5;
  1285. if power<>0 then
  1286. begin
  1287. result:=result*pow512[Power and $f];
  1288. power:=power shr 4;
  1289. if power<>0 then
  1290. begin
  1291. if power<=9 then
  1292. result:=result*pow4096[Power]
  1293. else
  1294. result:=1.0/0.0;
  1295. end;
  1296. end;
  1297. end;
  1298. end;
  1299. {$endif SUPPORT_EXTENDED}