genmath.inc 50 KB

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