genmath.inc 49 KB

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