genmath.inc 47 KB

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