genmath.inc 50 KB

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