genmath.inc 70 KB

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