genmath.inc 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2001 by Several contributors
  5. Generic mathemtical routines (on type real)
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {*************************************************************************}
  13. { Credits }
  14. {*************************************************************************}
  15. { Copyright Abandoned, 1987, Fred Fish }
  16. { }
  17. { This previously copyrighted work has been placed into the }
  18. { public domain by the author (Fred Fish) and may be freely used }
  19. { for any purpose, private or commercial. I would appreciate }
  20. { it, as a courtesy, if this notice is left in all copies and }
  21. { derivative works. Thank you, and enjoy... }
  22. { }
  23. { The author makes no warranty of any kind with respect to this }
  24. { product and explicitly disclaims any implied warranties of }
  25. { merchantability or fitness for any particular purpose. }
  26. {-------------------------------------------------------------------------}
  27. { Copyright (c) 1992 Odent Jean Philippe }
  28. { }
  29. { The source can be modified as long as my name appears and some }
  30. { notes explaining the modifications done are included in the file. }
  31. {-------------------------------------------------------------------------}
  32. { Copyright (c) 1997 Carl Eric Codere }
  33. {-------------------------------------------------------------------------}
  34. {$goto on}
  35. type
  36. TabCoef = array[0..6] of Real;
  37. const
  38. PIO2 = 1.57079632679489661923; { pi/2 }
  39. PIO4 = 7.85398163397448309616E-1; { pi/4 }
  40. SQRT2 = 1.41421356237309504880; { sqrt(2) }
  41. SQRTH = 7.07106781186547524401E-1; { sqrt(2)/2 }
  42. LOG2E = 1.4426950408889634073599; { 1/log(2) }
  43. SQ2OPI = 7.9788456080286535587989E-1; { sqrt( 2/pi )}
  44. LOGE2 = 6.93147180559945309417E-1; { log(2) }
  45. LOGSQ2 = 3.46573590279972654709E-1; { log(2)/2 }
  46. THPIO4 = 2.35619449019234492885; { 3*pi/4 }
  47. TWOOPI = 6.36619772367581343075535E-1; { 2/pi }
  48. lossth = 1.073741824e9;
  49. MAXLOG = 8.8029691931113054295988E1; { log(2**127) }
  50. MINLOG = -8.872283911167299960540E1; { log(2**-128) }
  51. DP1 = 7.85398125648498535156E-1;
  52. DP2 = 3.77489470793079817668E-8;
  53. DP3 = 2.69515142907905952645E-15;
  54. const sincof : TabCoef = (
  55. 1.58962301576546568060E-10,
  56. -2.50507477628578072866E-8,
  57. 2.75573136213857245213E-6,
  58. -1.98412698295895385996E-4,
  59. 8.33333333332211858878E-3,
  60. -1.66666666666666307295E-1, 0);
  61. coscof : TabCoef = (
  62. -1.13585365213876817300E-11,
  63. 2.08757008419747316778E-9,
  64. -2.75573141792967388112E-7,
  65. 2.48015872888517045348E-5,
  66. -1.38888888888730564116E-3,
  67. 4.16666666666665929218E-2, 0);
  68. { also necessary for Int() on systems with 64bit floats (JM) }
  69. type
  70. {$ifdef ENDIAN_LITTLE}
  71. float64 = packed record
  72. low: longint;
  73. high: longint;
  74. end;
  75. {$else}
  76. float64 = packed record
  77. high: longint;
  78. low: longint;
  79. end;
  80. {$endif}
  81. {$ifndef FPC_SYSTEM_HAS_TRUNC}
  82. type
  83. float32 = longint;
  84. flag = byte;
  85. Function extractFloat64Frac0(const a: float64): longint;
  86. Begin
  87. extractFloat64Frac0 := a.high and $000FFFFF;
  88. End;
  89. Function extractFloat64Frac1(const a: float64): longint;
  90. Begin
  91. extractFloat64Frac1 := a.low;
  92. End;
  93. Function extractFloat64Exp(const a: float64): smallint;
  94. Begin
  95. extractFloat64Exp:= ( a.high shr 20 ) AND $7FF;
  96. End;
  97. Function extractFloat64Sign(const a: float64) : flag;
  98. Begin
  99. extractFloat64Sign := a.high shr 31;
  100. End;
  101. Procedure
  102. shortShift64Left(
  103. a0:longint; a1:longint; count:smallint; VAR z0Ptr:longint; VAR z1Ptr:longint );
  104. Begin
  105. z1Ptr := a1 shl count;
  106. if count = 0 then
  107. z0Ptr := a0
  108. else
  109. z0Ptr := ( a0 shl count ) OR ( a1 shr ( ( - count ) AND 31 ) );
  110. End;
  111. function float64_to_int32_round_to_zero(a: float64 ): longint;
  112. Var
  113. aSign: flag;
  114. aExp, shiftCount: smallint;
  115. aSig0, aSig1, absZ, aSigExtra: longint;
  116. z: longint;
  117. Begin
  118. aSig1 := extractFloat64Frac1( a );
  119. aSig0 := extractFloat64Frac0( a );
  120. aExp := extractFloat64Exp( a );
  121. aSign := extractFloat64Sign( a );
  122. shiftCount := aExp - $413;
  123. if ( 0 <= shiftCount ) then
  124. Begin
  125. if (aExp=$7FF) and ((aSig0 or aSig1)<>0) then
  126. HandleError(207);
  127. shortShift64Left(
  128. aSig0 OR $00100000, aSig1, shiftCount, absZ, aSigExtra );
  129. End
  130. else
  131. Begin
  132. if ( aExp < $3FF ) then
  133. begin
  134. float64_to_int32_round_to_zero := 0;
  135. exit;
  136. end;
  137. aSig0 := aSig0 or $00100000;
  138. aSigExtra := ( aSig0 shl ( shiftCount and 31 ) ) OR aSig1;
  139. absZ := aSig0 shr ( - shiftCount );
  140. End;
  141. if aSign<>0 then
  142. z:=-absZ
  143. else
  144. z:=absZ;
  145. if ((aSign<>0) xor (z<0)) AND (z<>0) then
  146. HandleError(207);
  147. float64_to_int32_round_to_zero := z;
  148. End;
  149. Function ExtractFloat32Frac(a : Float32) : longint;
  150. Begin
  151. ExtractFloat32Frac := A AND $007FFFFF;
  152. End;
  153. Function extractFloat32Exp( a: float32 ): smallint;
  154. Begin
  155. extractFloat32Exp := (a shr 23) AND $FF;
  156. End;
  157. Function extractFloat32Sign( a: float32 ): Flag;
  158. Begin
  159. extractFloat32Sign := a shr 31;
  160. End;
  161. Function float32_to_int32_round_to_zero( a: Float32 ): longint;
  162. Var
  163. aSign : flag;
  164. aExp, shiftCount : smallint;
  165. aSig : longint;
  166. z : longint;
  167. Begin
  168. aSig := extractFloat32Frac( a );
  169. aExp := extractFloat32Exp( a );
  170. aSign := extractFloat32Sign( a );
  171. shiftCount := aExp - $9E;
  172. if ( 0 <= shiftCount ) then
  173. Begin
  174. if ( a <> Float32($CF000000) ) then
  175. Begin
  176. if ( (aSign=0) or ( ( aExp = $FF ) and (aSig<>0) ) ) then
  177. Begin
  178. HandleError(207);
  179. exit;
  180. end;
  181. End;
  182. HandleError(207);
  183. exit;
  184. End
  185. else
  186. if ( aExp <= $7E ) then
  187. Begin
  188. float32_to_int32_round_to_zero := 0;
  189. exit;
  190. End;
  191. aSig := ( aSig or $00800000 ) shl 8;
  192. z := aSig shr ( - shiftCount );
  193. if ( aSign<>0 ) then z := - z;
  194. float32_to_int32_round_to_zero := z;
  195. End;
  196. function trunc(d : real) : int64;[internconst:in_const_trunc];
  197. var
  198. {$ifdef cpuarm}
  199. l: longint;
  200. {$endif cpuarm}
  201. f32 : float32;
  202. f64 : float64;
  203. Begin
  204. { in emulation mode the real is equal to a single }
  205. { otherwise in fpu mode, it is equal to a double }
  206. { extended is not supported yet. }
  207. if sizeof(D) > 8 then
  208. HandleError(255);
  209. if sizeof(D)=8 then
  210. begin
  211. move(d,f64,sizeof(f64));
  212. {$ifdef cpuarm}
  213. { the arm fpu has a strange opinion how a double has to be stored }
  214. l:=f64.low;
  215. f64.low:=f64.high;
  216. f64.high:=l;
  217. {$endif cpuarm}
  218. trunc:=float64_to_int32_round_to_zero(f64);
  219. end
  220. else
  221. begin
  222. move(d,f32,sizeof(f32));
  223. trunc:=float32_to_int32_round_to_zero(f32);
  224. end;
  225. end;
  226. {$endif}
  227. {$ifndef FPC_SYSTEM_HAS_INT}
  228. {$ifdef SUPPORT_DOUBLE}
  229. { straight Pascal translation of the code for __trunc() in }
  230. { the file sysdeps/libm-ieee754/s_trunc.c of glibc (JM) }
  231. function int(d: double): double;[internconst:in_const_int];
  232. var
  233. i0, j0: longint;
  234. i1: cardinal;
  235. sx: longint;
  236. f64 : float64;
  237. begin
  238. f64:=float64(d);
  239. {$ifdef cpuarm}
  240. { the arm fpu has a strange opinion how a double has to be stored }
  241. i0:=f64.low;
  242. f64.low:=f64.high;
  243. f64.high:=i0;
  244. {$endif cpuarm}
  245. i0 := f64.high;
  246. i1 := cardinal(f64.low);
  247. sx := i0 and $80000000;
  248. j0 := ((i0 shr 20) and $7ff) - $3ff;
  249. if (j0 < 20) then
  250. begin
  251. if (j0 < 0) then
  252. begin
  253. { the magnitude of the number is < 1 so the result is +-0. }
  254. f64.high := sx;
  255. f64.low := 0;
  256. end
  257. else
  258. begin
  259. f64.high := sx or (i0 and not($fffff shr j0));
  260. f64.low := 0;
  261. end
  262. end
  263. else if (j0 > 51) then
  264. begin
  265. if (j0 = $400) then
  266. { d is inf or NaN }
  267. exit(d + d); { don't know why they do this (JM) }
  268. end
  269. else
  270. begin
  271. f64.high := i0;
  272. f64.low := longint(i1 and not(cardinal($ffffffff) shr (j0 - 20)));
  273. end;
  274. {$ifdef cpuarm}
  275. { the arm fpu has a strange opinion how a double has to be stored }
  276. i0:=f64.low;
  277. f64.low:=f64.high;
  278. f64.high:=i0;
  279. {$endif cpuarm}
  280. result:=double(f64);
  281. end;
  282. {$else SUPPORT_DOUBLE}
  283. function int(d : real) : real;[internconst:in_const_int];
  284. begin
  285. { this will be correct since real = single in the case of }
  286. { the motorola version of the compiler... }
  287. int:=real(trunc(d));
  288. end;
  289. {$endif SUPPORT_DOUBLE}
  290. {$endif}
  291. {$ifndef FPC_SYSTEM_HAS_ABS}
  292. {$ifdef SUPPORT_DOUBLE}
  293. function abs(d : Double) : Double;[public,alias:'FPC_ABS_REAL'];
  294. begin
  295. if (d<0.0) then
  296. abs := -d
  297. else
  298. abs := d ;
  299. end;
  300. {$else}
  301. function abs(d : Real) : Real;[public,alias:'FPC_ABS_REAL'];
  302. begin
  303. if (d<0.0) then
  304. abs := -d
  305. else
  306. abs := d ;
  307. end;
  308. {$endif}
  309. {$ifdef hascompilerproc}
  310. function fpc_abs_real(d:Real):Real;compilerproc; external name 'FPC_ABS_REAL';
  311. {$endif hascompilerproc}
  312. {$endif not FPC_SYSTEM_HAS_ABS}
  313. function frexp(x:Real; var e:Integer ):Real;
  314. {* frexp() extracts the exponent from x. It returns an integer *}
  315. {* power of two to expnt and the significand between 0.5 and 1 *}
  316. {* to y. Thus x = y * 2**expn. *}
  317. begin
  318. e :=0;
  319. if (abs(x)<0.5) then
  320. While (abs(x)<0.5) do
  321. begin
  322. x := x*2;
  323. Dec(e);
  324. end
  325. else
  326. While (abs(x)>1) do
  327. begin
  328. x := x/2;
  329. Inc(e);
  330. end;
  331. frexp := x;
  332. end;
  333. function ldexp( x: Real; N: Integer):Real;
  334. {* ldexp() multiplies x by 2**n. *}
  335. var r : Real;
  336. begin
  337. R := 1;
  338. if N>0 then
  339. while N>0 do
  340. begin
  341. R:=R*2;
  342. Dec(N);
  343. end
  344. else
  345. while N<0 do
  346. begin
  347. R:=R/2;
  348. Inc(N);
  349. end;
  350. ldexp := x * R;
  351. end;
  352. function polevl(var x:Real; var Coef:TabCoef; N:Integer):Real;
  353. {*****************************************************************}
  354. { Evaluate polynomial }
  355. {*****************************************************************}
  356. { }
  357. { SYNOPSIS: }
  358. { }
  359. { int N; }
  360. { double x, y, coef[N+1], polevl[]; }
  361. { }
  362. { y = polevl( x, coef, N ); }
  363. { }
  364. { DESCRIPTION: }
  365. { }
  366. { Evaluates polynomial of degree N: }
  367. { }
  368. { 2 N }
  369. { y = C + C x + C x +...+ C x }
  370. { 0 1 2 N }
  371. { }
  372. { Coefficients are stored in reverse order: }
  373. { }
  374. { coef[0] = C , ..., coef[N] = C . }
  375. { N 0 }
  376. { }
  377. { The function p1evl() assumes that coef[N] = 1.0 and is }
  378. { omitted from the array. Its calling arguments are }
  379. { otherwise the same as polevl(). }
  380. { }
  381. { SPEED: }
  382. { }
  383. { In the interest of speed, there are no checks for out }
  384. { of bounds arithmetic. This routine is used by most of }
  385. { the functions in the library. Depending on available }
  386. { equipment features, the user may wish to rewrite the }
  387. { program in microcode or assembly language. }
  388. {*****************************************************************}
  389. var ans : Real;
  390. i : Integer;
  391. begin
  392. ans := Coef[0];
  393. for i:=1 to N do
  394. ans := ans * x + Coef[i];
  395. polevl:=ans;
  396. end;
  397. function p1evl(var x:Real; var Coef:TabCoef; N:Integer):Real;
  398. { }
  399. { Evaluate polynomial when coefficient of x is 1.0. }
  400. { Otherwise same as polevl. }
  401. { }
  402. var
  403. ans : Real;
  404. i : Integer;
  405. begin
  406. ans := x + Coef[0];
  407. for i:=1 to N-1 do
  408. ans := ans * x + Coef[i];
  409. p1evl := ans;
  410. end;
  411. {$ifndef FPC_SYSTEM_HAS_SQR}
  412. function sqr(d : Real) : Real;[internconst:in_const_sqr];
  413. begin
  414. sqr := d*d;
  415. end;
  416. {$endif}
  417. {$ifndef FPC_SYSTEM_HAS_PI}
  418. function pi : Real;[internconst:in_const_pi];
  419. begin
  420. pi := 3.1415926535897932385;
  421. end;
  422. {$endif}
  423. {$ifndef FPC_SYSTEM_HAS_SQRT}
  424. function sqrt(d:Real):Real;[internconst:in_const_sqrt]; [public, alias: 'FPC_SQRT_REAL'];
  425. {*****************************************************************}
  426. { Square root }
  427. {*****************************************************************}
  428. { }
  429. { SYNOPSIS: }
  430. { }
  431. { double x, y, sqrt(); }
  432. { }
  433. { y = sqrt( x ); }
  434. { }
  435. { DESCRIPTION: }
  436. { }
  437. { Returns the square root of x. }
  438. { }
  439. { Range reduction involves isolating the power of two of the }
  440. { argument and using a polynomial approximation to obtain }
  441. { a rough value for the square root. Then Heron's iteration }
  442. { is used three times to converge to an accurate value. }
  443. {*****************************************************************}
  444. var e : Integer;
  445. w,z : Real;
  446. begin
  447. if( d <= 0.0 ) then
  448. begin
  449. if( d < 0.0 ) then
  450. HandleError(207);
  451. sqrt := 0.0;
  452. end
  453. else
  454. begin
  455. w := d;
  456. { separate exponent and significand }
  457. z := frexp( d, e );
  458. { approximate square root of number between 0.5 and 1 }
  459. { relative error of approximation = 7.47e-3 }
  460. d := 4.173075996388649989089E-1 + 5.9016206709064458299663E-1 * z;
  461. { adjust for odd powers of 2 }
  462. if odd(e) then
  463. d := d*SQRT2;
  464. { re-insert exponent }
  465. d := ldexp( d, (e div 2) );
  466. { Newton iterations: }
  467. d := 0.5*(d + w/d);
  468. d := 0.5*(d + w/d);
  469. d := 0.5*(d + w/d);
  470. d := 0.5*(d + w/d);
  471. d := 0.5*(d + w/d);
  472. d := 0.5*(d + w/d);
  473. sqrt := d;
  474. end;
  475. end;
  476. {$ifdef hascompilerproc}
  477. function fpc_sqrt_real(d:Real):Real;compilerproc; external name 'FPC_SQRT_REAL';
  478. {$endif hascompilerproc}
  479. {$endif}
  480. {$ifndef FPC_SYSTEM_HAS_EXP}
  481. function Exp(d:Real):Real;[internconst:in_const_exp];
  482. {*****************************************************************}
  483. { Exponential Function }
  484. {*****************************************************************}
  485. { }
  486. { SYNOPSIS: }
  487. { }
  488. { double x, y, exp(); }
  489. { }
  490. { y = exp( x ); }
  491. { }
  492. { DESCRIPTION: }
  493. { }
  494. { Returns e (2.71828...) raised to the x power. }
  495. { }
  496. { Range reduction is accomplished by separating the argument }
  497. { into an integer k and fraction f such that }
  498. { }
  499. { x k f }
  500. { e = 2 e. }
  501. { }
  502. { A Pade' form of degree 2/3 is used to approximate exp(f)- 1 }
  503. { in the basic range [-0.5 ln 2, 0.5 ln 2]. }
  504. {*****************************************************************}
  505. const P : TabCoef = (
  506. 1.26183092834458542160E-4,
  507. 3.02996887658430129200E-2,
  508. 1.00000000000000000000E0, 0, 0, 0, 0);
  509. Q : TabCoef = (
  510. 3.00227947279887615146E-6,
  511. 2.52453653553222894311E-3,
  512. 2.27266044198352679519E-1,
  513. 2.00000000000000000005E0, 0 ,0 ,0);
  514. C1 = 6.9335937500000000000E-1;
  515. C2 = 2.1219444005469058277E-4;
  516. var n : Integer;
  517. px, qx, xx : Real;
  518. begin
  519. if( d > MAXLOG) then
  520. HandleError(205)
  521. else
  522. if( d < MINLOG ) then
  523. begin
  524. HandleError(205);
  525. end
  526. else
  527. begin
  528. { Express e**x = e**g 2**n }
  529. { = e**g e**( n loge(2) ) }
  530. { = e**( g + n loge(2) ) }
  531. px := d * LOG2E;
  532. qx := Trunc( px + 0.5 ); { Trunc() truncates toward -infinity. }
  533. n := Trunc(qx);
  534. d := d - qx * C1;
  535. d := d + qx * C2;
  536. { rational approximation for exponential }
  537. { of the fractional part: }
  538. { e**x - 1 = 2x P(x**2)/( Q(x**2) - P(x**2) ) }
  539. xx := d * d;
  540. px := d * polevl( xx, P, 2 );
  541. d := px/( polevl( xx, Q, 3 ) - px );
  542. d := ldexp( d, 1 );
  543. d := d + 1.0;
  544. d := ldexp( d, n );
  545. Exp := d;
  546. end;
  547. end;
  548. {$endif}
  549. {$ifndef FPC_SYSTEM_HAS_ROUND}
  550. {$ifdef hascompilerproc}
  551. function round(d : Real) : int64;[internconst:in_const_round, external name 'FPC_ROUND'];
  552. function fpc_round(d : Real) : int64;[public, alias:'FPC_ROUND'];{$ifdef hascompilerproc}compilerproc;{$endif hascompilerproc}
  553. {$else}
  554. function round(d : Real) : int64;[internconst:in_const_round];
  555. {$endif hascompilerproc}
  556. var
  557. fr: Real;
  558. tr: Int64;
  559. Begin
  560. fr := abs(Frac(d));
  561. tr := Trunc(d);
  562. if fr > 0.5 then
  563. if d >= 0 then
  564. result:=tr+1
  565. else
  566. result:=tr-1
  567. else
  568. if fr < 0.5 then
  569. result:=tr
  570. else { fr = 0.5 }
  571. { check sign to decide ... }
  572. { as in Turbo Pascal... }
  573. if d >= 0.0 then
  574. result:=tr+1
  575. else
  576. result:=tr;
  577. end;
  578. {$endif}
  579. {$ifdef FPC_CURRENCY_IS_INT64}
  580. function trunc(c : currency) : int64;
  581. type
  582. tmyrec = record
  583. i: int64;
  584. end;
  585. begin
  586. result := int64(tmyrec(c)) div 10000
  587. end;
  588. function trunc(c : comp) : int64;
  589. begin
  590. result := c
  591. end;
  592. function round(c : currency) : int64;
  593. type
  594. tmyrec = record
  595. i: int64;
  596. end;
  597. var
  598. rem, absrem: longint;
  599. begin
  600. { (int64(tmyrec(c))(+/-)5000) div 10000 can overflow }
  601. result := int64(tmyrec(c)) div 10000;
  602. rem := int64(tmyrec(c)) - result * 10000;
  603. absrem := abs(rem);
  604. if (absrem > 5000) or
  605. ((absrem = 5000) and
  606. (rem > 0)) then
  607. if (rem > 0) then
  608. inc(result)
  609. else
  610. dec(result);
  611. end;
  612. function round(c : comp) : int64;
  613. begin
  614. result := c
  615. end;
  616. {$endif FPC_CURRENCY_IS_INT64}
  617. {$ifndef FPC_SYSTEM_HAS_LN}
  618. function Ln(d:Real):Real;[internconst:in_const_ln];
  619. {*****************************************************************}
  620. { Natural Logarithm }
  621. {*****************************************************************}
  622. { }
  623. { SYNOPSIS: }
  624. { }
  625. { double x, y, log(); }
  626. { }
  627. { y = ln( x ); }
  628. { }
  629. { DESCRIPTION: }
  630. { }
  631. { Returns the base e (2.718...) logarithm of x. }
  632. { }
  633. { The argument is separated into its exponent and fractional }
  634. { parts. If the exponent is between -1 and +1, the logarithm }
  635. { of the fraction is approximated by }
  636. { }
  637. { log(1+x) = x - 0.5 x**2 + x**3 P(x)/Q(x). }
  638. { }
  639. { Otherwise, setting z = 2(x-1)/x+1), }
  640. { }
  641. { log(x) = z + z**3 P(z)/Q(z). }
  642. { }
  643. {*****************************************************************}
  644. const P : TabCoef = (
  645. { Coefficients for log(1+x) = x - x**2/2 + x**3 P(x)/Q(x)
  646. 1/sqrt(2) <= x < sqrt(2) }
  647. 4.58482948458143443514E-5,
  648. 4.98531067254050724270E-1,
  649. 6.56312093769992875930E0,
  650. 2.97877425097986925891E1,
  651. 6.06127134467767258030E1,
  652. 5.67349287391754285487E1,
  653. 1.98892446572874072159E1);
  654. Q : TabCoef = (
  655. 1.50314182634250003249E1,
  656. 8.27410449222435217021E1,
  657. 2.20664384982121929218E2,
  658. 3.07254189979530058263E2,
  659. 2.14955586696422947765E2,
  660. 5.96677339718622216300E1, 0);
  661. { Coefficients for log(x) = z + z**3 P(z)/Q(z),
  662. where z = 2(x-1)/(x+1)
  663. 1/sqrt(2) <= x < sqrt(2) }
  664. R : TabCoef = (
  665. -7.89580278884799154124E-1,
  666. 1.63866645699558079767E1,
  667. -6.41409952958715622951E1, 0, 0, 0, 0);
  668. S : TabCoef = (
  669. -3.56722798256324312549E1,
  670. 3.12093766372244180303E2,
  671. -7.69691943550460008604E2, 0, 0, 0, 0);
  672. var e : Integer;
  673. z, y : Real;
  674. Label Ldone;
  675. begin
  676. if( d <= 0.0 ) then
  677. HandleError(207);
  678. d := frexp( d, e );
  679. { logarithm using log(x) = z + z**3 P(z)/Q(z),
  680. where z = 2(x-1)/x+1) }
  681. if( (e > 2) or (e < -2) ) then
  682. begin
  683. if( d < SQRTH ) then
  684. begin
  685. { 2( 2x-1 )/( 2x+1 ) }
  686. Dec(e, 1);
  687. z := d - 0.5;
  688. y := 0.5 * z + 0.5;
  689. end
  690. else
  691. begin
  692. { 2 (x-1)/(x+1) }
  693. z := d - 0.5;
  694. z := z - 0.5;
  695. y := 0.5 * d + 0.5;
  696. end;
  697. d := z / y;
  698. { /* rational form */ }
  699. z := d*d;
  700. z := d + d * ( z * polevl( z, R, 2 ) / p1evl( z, S, 3 ) );
  701. goto ldone;
  702. end;
  703. { logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) }
  704. if( d < SQRTH ) then
  705. begin
  706. Dec(e, 1);
  707. d := ldexp( d, 1 ) - 1.0; { 2x - 1 }
  708. end
  709. else
  710. d := d - 1.0;
  711. { rational form }
  712. z := d*d;
  713. y := d * ( z * polevl( d, P, 6 ) / p1evl( d, Q, 6 ) );
  714. y := y - ldexp( z, -1 ); { y - 0.5 * z }
  715. z := d + y;
  716. ldone:
  717. { recombine with exponent term }
  718. if( e <> 0 ) then
  719. begin
  720. y := e;
  721. z := z - y * 2.121944400546905827679e-4;
  722. z := z + y * 0.693359375;
  723. end;
  724. Ln:= z;
  725. end;
  726. {$endif}
  727. {$ifndef FPC_SYSTEM_HAS_SIN}
  728. function Sin(d:Real):Real;[internconst:in_const_sin];
  729. {*****************************************************************}
  730. { Circular Sine }
  731. {*****************************************************************}
  732. { }
  733. { SYNOPSIS: }
  734. { }
  735. { double x, y, sin(); }
  736. { }
  737. { y = sin( x ); }
  738. { }
  739. { DESCRIPTION: }
  740. { }
  741. { Range reduction is into intervals of pi/4. The reduction }
  742. { error is nearly eliminated by contriving an extended }
  743. { precision modular arithmetic. }
  744. { }
  745. { Two polynomial approximating functions are employed. }
  746. { Between 0 and pi/4 the sine is approximated by }
  747. { x + x**3 P(x**2). }
  748. { Between pi/4 and pi/2 the cosine is represented as }
  749. { 1 - x**2 Q(x**2). }
  750. {*****************************************************************}
  751. var y, z, zz : Real;
  752. j, sign : Integer;
  753. begin
  754. { make argument positive but save the sign }
  755. sign := 1;
  756. if( d < 0 ) then
  757. begin
  758. d := -d;
  759. sign := -1;
  760. end;
  761. { above this value, approximate towards 0 }
  762. if( d > lossth ) then
  763. begin
  764. sin := 0.0;
  765. exit;
  766. end;
  767. y := Trunc( d/PIO4 ); { integer part of x/PIO4 }
  768. { strip high bits of integer part to prevent integer overflow }
  769. z := ldexp( y, -4 );
  770. z := Trunc(z); { integer part of y/8 }
  771. z := y - ldexp( z, 4 ); { y - 16 * (y/16) }
  772. j := Trunc(z); { convert to integer for tests on the phase angle }
  773. { map zeros to origin }
  774. { typecast is to avoid "can't determine which overloaded function }
  775. { to call" }
  776. if odd( longint(j) ) then
  777. begin
  778. inc(j);
  779. y := y + 1.0;
  780. end;
  781. j := j and 7; { octant modulo 360 degrees }
  782. { reflect in x axis }
  783. if( j > 3) then
  784. begin
  785. sign := -sign;
  786. dec(j, 4);
  787. end;
  788. { Extended precision modular arithmetic }
  789. z := ((d - y * DP1) - y * DP2) - y * DP3;
  790. zz := z * z;
  791. if( (j=1) or (j=2) ) then
  792. y := 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 )
  793. else
  794. { y = z + z * (zz * polevl( zz, sincof, 5 )); }
  795. y := z + z * z * z * polevl( zz, sincof, 5 );
  796. if(sign < 0) then
  797. y := -y;
  798. sin := y;
  799. end;
  800. {$endif}
  801. {$ifndef FPC_SYSTEM_HAS_COS}
  802. function Cos(d:Real):Real;[internconst:in_const_cos];
  803. {*****************************************************************}
  804. { Circular cosine }
  805. {*****************************************************************}
  806. { }
  807. { Circular cosine }
  808. { }
  809. { SYNOPSIS: }
  810. { }
  811. { double x, y, cos(); }
  812. { }
  813. { y = cos( x ); }
  814. { }
  815. { DESCRIPTION: }
  816. { }
  817. { Range reduction is into intervals of pi/4. The reduction }
  818. { error is nearly eliminated by contriving an extended }
  819. { precision modular arithmetic. }
  820. { }
  821. { Two polynomial approximating functions are employed. }
  822. { Between 0 and pi/4 the cosine is approximated by }
  823. { 1 - x**2 Q(x**2). }
  824. { Between pi/4 and pi/2 the sine is represented as }
  825. { x + x**3 P(x**2). }
  826. {*****************************************************************}
  827. var y, z, zz : Real;
  828. j, sign : Integer;
  829. i : LongInt;
  830. begin
  831. { make argument positive }
  832. sign := 1;
  833. if( d < 0 ) then
  834. d := -d;
  835. { above this value, round towards zero }
  836. if( d > lossth ) then
  837. begin
  838. cos := 0.0;
  839. exit;
  840. end;
  841. y := Trunc( d/PIO4 );
  842. z := ldexp( y, -4 );
  843. z := Trunc(z); { integer part of y/8 }
  844. z := y - ldexp( z, 4 ); { y - 16 * (y/16) }
  845. { integer and fractional part modulo one octant }
  846. i := Trunc(z);
  847. if odd( i ) then { map zeros to origin }
  848. begin
  849. inc(i);
  850. y := y + 1.0;
  851. end;
  852. j := i and 07;
  853. if( j > 3) then
  854. begin
  855. dec(j,4);
  856. sign := -sign;
  857. end;
  858. if( j > 1 ) then
  859. sign := -sign;
  860. { Extended precision modular arithmetic }
  861. z := ((d - y * DP1) - y * DP2) - y * DP3;
  862. zz := z * z;
  863. if( (j=1) or (j=2) ) then
  864. { y = z + z * (zz * polevl( zz, sincof, 5 )); }
  865. y := z + z * z * z * polevl( zz, sincof, 5 )
  866. else
  867. y := 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 );
  868. if(sign < 0) then
  869. y := -y;
  870. cos := y ;
  871. end;
  872. {$endif}
  873. {$ifndef FPC_SYSTEM_HAS_ARCTAN}
  874. function ArcTan(d:Real):Real;[internconst:in_const_arctan];
  875. {*****************************************************************}
  876. { Inverse circular tangent (arctangent) }
  877. {*****************************************************************}
  878. { }
  879. { SYNOPSIS: }
  880. { }
  881. { double x, y, atan(); }
  882. { }
  883. { y = atan( x ); }
  884. { }
  885. { DESCRIPTION: }
  886. { }
  887. { Returns radian angle between -pi/2 and +pi/2 whose tangent }
  888. { is x. }
  889. { }
  890. { Range reduction is from four intervals into the interval }
  891. { from zero to tan( pi/8 ). The approximant uses a rational }
  892. { function of degree 3/4 of the form x + x**3 P(x)/Q(x). }
  893. {*****************************************************************}
  894. const P : TabCoef = (
  895. -8.40980878064499716001E-1,
  896. -8.83860837023772394279E0,
  897. -2.18476213081316705724E1,
  898. -1.48307050340438946993E1, 0, 0, 0);
  899. Q : TabCoef = (
  900. 1.54974124675307267552E1,
  901. 6.27906555762653017263E1,
  902. 9.22381329856214406485E1,
  903. 4.44921151021319438465E1, 0, 0, 0);
  904. { tan( 3*pi/8 ) }
  905. T3P8 = 2.41421356237309504880;
  906. { tan( pi/8 ) }
  907. TP8 = 0.41421356237309504880;
  908. var y,z : Real;
  909. Sign : Integer;
  910. begin
  911. { make argument positive and save the sign }
  912. sign := 1;
  913. if( d < 0.0 ) then
  914. begin
  915. sign := -1;
  916. d := -d;
  917. end;
  918. { range reduction }
  919. if( d > T3P8 ) then
  920. begin
  921. y := PIO2;
  922. d := -( 1.0/d );
  923. end
  924. else if( d > TP8 ) then
  925. begin
  926. y := PIO4;
  927. d := (d-1.0)/(d+1.0);
  928. end
  929. else
  930. y := 0.0;
  931. { rational form in x**2 }
  932. z := d * d;
  933. y := y + ( polevl( z, P, 3 ) / p1evl( z, Q, 4 ) ) * z * d + d;
  934. if( sign < 0 ) then
  935. y := -y;
  936. Arctan := y;
  937. end;
  938. {$endif}
  939. {$ifndef FPC_SYSTEM_HAS_FRAC}
  940. function frac(d : Real) : Real;[internconst:in_const_frac];
  941. begin
  942. frac := d - Int(d);
  943. end;
  944. {$endif}
  945. {$ifndef FPC_SYSTEM_HAS_POWER}
  946. function power(bas,expo : real) : real;
  947. begin
  948. if bas=0.0 then
  949. begin
  950. if expo<>0.0 then
  951. power:=0.0
  952. else
  953. HandleError(207);
  954. end
  955. else if expo=0.0 then
  956. power:=1
  957. else
  958. { bas < 0 is not allowed }
  959. if bas<0.0 then
  960. handleerror(207)
  961. else
  962. power:=exp(ln(bas)*expo);
  963. end;
  964. {$endif}
  965. {$ifndef FPC_SYSTEM_HAS_POWER_INT64}
  966. function power(bas,expo : int64) : int64;
  967. begin
  968. if bas=0 then
  969. begin
  970. if expo<>0 then
  971. power:=0
  972. else
  973. HandleError(207);
  974. end
  975. else if expo=0 then
  976. power:=1
  977. else
  978. begin
  979. if bas<0 then
  980. begin
  981. if odd(expo) then
  982. power:=-round(exp(ln(-bas)*expo))
  983. else
  984. power:=round(exp(ln(-bas)*expo));
  985. end
  986. else
  987. power:=round(exp(ln(bas)*expo));
  988. end;
  989. end;
  990. {$endif}
  991. {$ifdef FPC_INCLUDE_SOFTWARE_INT64_TO_DOUBLE}
  992. {$ifndef FPC_SYSTEM_HAS_QWORD_TO_DOUBLE}
  993. function fpc_qword_to_double(q : qword): double; compilerproc;
  994. begin
  995. result:=dword(q and $ffffffff)+dword(q shr 32)*4294967296.0;
  996. end;
  997. {$endif FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  998. {$ifndef FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  999. function fpc_int64_to_double(i : int64): double; compilerproc;
  1000. begin
  1001. if i<0 then
  1002. result:=-double(qword(-i))
  1003. else
  1004. result:=qword(i);
  1005. end;
  1006. {$endif FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  1007. {$endif FPC_INCLUDE_SOFTWARE_INT64_TO_DOUBLE}
  1008. {$ifdef SUPPORT_DOUBLE}
  1009. {****************************************************************************
  1010. Helper routines to support old TP styled reals
  1011. ****************************************************************************}
  1012. {$ifndef FPC_SYSTEM_HAS_REAL2DOUBLE}
  1013. function real2double(r : real48) : double;
  1014. var
  1015. res : array[0..7] of byte;
  1016. exponent : word;
  1017. begin
  1018. { copy mantissa }
  1019. res[0]:=0;
  1020. res[1]:=r[1] shl 5;
  1021. res[2]:=(r[1] shr 3) or (r[2] shl 5);
  1022. res[3]:=(r[2] shr 3) or (r[3] shl 5);
  1023. res[4]:=(r[3] shr 3) or (r[4] shl 5);
  1024. res[5]:=(r[4] shr 3) or (r[5] and $7f) shl 5;
  1025. res[6]:=(r[5] and $7f) shr 3;
  1026. { copy exponent }
  1027. { correct exponent: }
  1028. exponent:=(word(r[0])+(1023-129));
  1029. res[6]:=res[6] or ((exponent and $f) shl 4);
  1030. res[7]:=exponent shr 4;
  1031. { set sign }
  1032. res[7]:=res[7] or (r[5] and $80);
  1033. real2double:=double(res);
  1034. end;
  1035. {$endif FPC_SYSTEM_HAS_REAL2DOUBLE}
  1036. {$endif SUPPORT_DOUBLE}
  1037. {
  1038. $Log$
  1039. Revision 1.24 2004-05-31 20:25:04 peter
  1040. * removed warnings
  1041. Revision 1.23 2004/03/13 18:33:52 florian
  1042. * fixed some arm related real stuff
  1043. Revision 1.22 2004/03/11 22:39:53 florian
  1044. * arm startup code fixed
  1045. * made some generic math code more readable
  1046. Revision 1.21 2004/02/04 14:15:57 florian
  1047. * fixed generic system.int(...)
  1048. Revision 1.20 2004/01/24 18:15:58 florian
  1049. * fixed small bugs
  1050. * fixed some arm issues
  1051. Revision 1.18 2004/01/06 21:34:07 peter
  1052. * abs(double) added
  1053. * abs() alias
  1054. Revision 1.17 2004/01/02 17:19:04 jonas
  1055. * if currency = int64, FPC_CURRENCY_IS_INT64 is defined
  1056. + round and trunc for currency and comp if FPC_CURRENCY_IS_INT64 is
  1057. defined
  1058. * if currency = orddef, prefer currency -> int64/qword conversion over
  1059. currency -> float conversions
  1060. * optimized currency/currency if currency = orddef
  1061. * TODO: write FPC_DIV_CURRENCY and FPC_MUL_CURRENCY routines to prevent
  1062. precision loss if currency=int64 and bestreal = double
  1063. Revision 1.16 2003/12/08 19:44:11 jonas
  1064. * use HandleError instead of RunError so exception catching works
  1065. Revision 1.15 2003/09/03 14:09:37 florian
  1066. * arm fixes to the common rtl code
  1067. * some generic math code fixed
  1068. * ...
  1069. Revision 1.14 2003/05/24 13:39:32 jonas
  1070. * fsqrt is an optional instruction in the ppc architecture and isn't
  1071. implemented by any current ppc afaik, so use the generic sqrt routine
  1072. instead (adapted so it works with compilerproc)
  1073. Revision 1.13 2003/05/23 22:58:31 jonas
  1074. * added longint typecase to odd(smallint_var) call to avoid overload
  1075. problem
  1076. Revision 1.12 2003/05/02 15:12:19 jonas
  1077. - removed empty ppc-specific frac()
  1078. + added correct generic frac() implementation for doubles (translated
  1079. from glibc code)
  1080. Revision 1.11 2003/04/23 21:28:21 peter
  1081. * fpc_round added, needed for int64 currency
  1082. Revision 1.10 2003/01/15 00:45:17 peter
  1083. * use generic int64 power
  1084. Revision 1.9 2002/10/12 20:28:49 carl
  1085. * round returns int64
  1086. Revision 1.8 2002/10/07 15:15:02 florian
  1087. * fixed wrong commit
  1088. Revision 1.7 2002/10/07 15:10:45 florian
  1089. + variant wrappers for cmp operators added
  1090. Revision 1.6 2002/09/07 15:07:45 peter
  1091. * old logs removed and tabs fixed
  1092. Revision 1.5 2002/07/28 21:39:29 florian
  1093. * made abs a compiler proc if it is generic
  1094. Revision 1.4 2002/07/28 20:43:48 florian
  1095. * several fixes for linux/powerpc
  1096. * several fixes to MT
  1097. }