cgenmath.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2001 by Several contributors
  4. Generic mathemtical routines in libc
  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. { for 80x86, we can easily write the optimal inline code }
  12. { Furthermore, the routines below only go up to double }
  13. { precision and we need extended precision if supported }
  14. {$ifndef FPC_HAS_TYPE_EXTENDED}
  15. {$ifndef SOLARIS}
  16. {$ifndef FPC_SYSTEM_HAS_INT}
  17. {$define FPC_SYSTEM_HAS_INT}
  18. {$ifdef SUPPORT_DOUBLE}
  19. function c_trunc(d: double): double; cdecl; external 'c' name 'trunc';
  20. function fpc_int_real(d: ValReal): ValReal;compilerproc; {$ifdef MATHINLINE}inline;{$endif}
  21. begin
  22. result := c_trunc(d);
  23. end;
  24. {$else SUPPORT_DOUBLE}
  25. function c_truncf(d: double): double; cdecl; external 'c' name 'truncf';
  26. function fpc_int_real(d: ValReal): ValReal;compilerproc; {$ifdef MATHINLINE}inline;{$endif}
  27. begin
  28. { this will be correct since real = single in the case of }
  29. { the motorola version of the compiler... }
  30. int:=c_truncf(d);
  31. end;
  32. {$endif SUPPORT_DOUBLE}
  33. {$endif FPC_SYSTEM_HAS_INT}
  34. {$endif SOLARIS}
  35. {$ifndef SYSTEM_HAS_FREXP}
  36. {$define SYSTEM_HAS_FREXP}
  37. function c_frexp(x: double; out e: longint): double; cdecl; external 'c' name 'frexp';
  38. function frexp(x:ValReal; out e:Integer ):ValReal; {$ifdef MATHINLINE}inline;{$endif}
  39. var
  40. l: longint;
  41. begin
  42. frexp := c_frexp(x,l);
  43. e := l;
  44. end;
  45. {$endif not SYSTEM_HAS_FREXP}
  46. {$ifndef SYSTEM_HAS_LDEXP}
  47. {$define SYSTEM_HAS_LDEXP}
  48. function c_ldexp(x: double; n: longint): double; cdecl; external 'c' name 'ldexp';
  49. function ldexp( x: ValReal; N: Integer):ValReal;{$ifdef MATHINLINE}inline;{$endif}
  50. begin
  51. ldexp := c_ldexp(x,n);
  52. end;
  53. {$endif not SYSTEM_HAS_LDEXP}
  54. {$ifndef FPC_SYSTEM_HAS_SQRT}
  55. {$define FPC_SYSTEM_HAS_SQRT}
  56. function c_sqrt(d: double): double; cdecl; external 'c' name 'sqrt';
  57. function fpc_sqrt_real(d:ValReal):ValReal;compilerproc; {$ifdef MATHINLINE}inline;{$endif}
  58. begin
  59. result := c_sqrt(d);
  60. end;
  61. {$endif}
  62. {$ifndef FPC_SYSTEM_HAS_EXP}
  63. {$define FPC_SYSTEM_HAS_EXP}
  64. function c_exp(d: double): double; cdecl; external 'c' name 'exp';
  65. function fpc_Exp_real(d:ValReal):ValReal;compilerproc; {$ifdef MATHINLINE}inline;{$endif}
  66. begin
  67. result := c_exp(d);
  68. end;
  69. {$endif}
  70. { Not supported everywhere (also not on Mac OS X 10.1, but that's deprecated. }
  71. { It is supported on linux, but at least for linux/i386 we should call }
  72. { llroundl() instead (for extended support). }
  73. {$if defined(darwin) }
  74. {$ifndef FPC_SYSTEM_HAS_ROUND}
  75. {$define FPC_SYSTEM_HAS_ROUND}
  76. function c_llround(d: double): int64; cdecl; external 'c' name 'llround';
  77. function fpc_round_real(d : ValReal) : int64;[public, alias:'FPC_ROUND'];compilerproc;
  78. begin
  79. case softfloat_rounding_mode of
  80. float_round_nearest_even:
  81. begin
  82. fpc_round_real:=c_llround(d);
  83. { llround always rounds half-way cases away from zero, }
  84. { regardless of the current rounding mode }
  85. if (abs(frac(d))=0.5) then
  86. fpc_round_real:=2*trunc(fpc_round_real*extended(0.5));
  87. end;
  88. float_round_down:
  89. if (d>=0) or
  90. (frac(d)=0.0) then
  91. result:=trunc(d)
  92. else
  93. result:=trunc(d-1.0);
  94. float_round_up:
  95. if (d>=0) and
  96. (frac(d)<>0.0) then
  97. result:=trunc(d+1.0)
  98. else
  99. result:=trunc(d);
  100. float_round_to_zero:
  101. result:=trunc(d);
  102. end;
  103. end;
  104. {$endif not FPC_SYSTEM_HAS_ROUND}
  105. {$endif darwin}
  106. {$ifndef FPC_SYSTEM_HAS_LN}
  107. {$define FPC_SYSTEM_HAS_LN}
  108. function c_log(d: double): double; cdecl; external 'c' name 'log';
  109. function fpc_Ln_real(d:ValReal):ValReal;compilerproc;{$ifdef MATHINLINE}inline;{$endif}
  110. begin
  111. result := c_log(d);
  112. end;
  113. {$endif}
  114. {$ifndef FPC_SYSTEM_HAS_SIN}
  115. {$define FPC_SYSTEM_HAS_SIN}
  116. function c_sin(d: double): double; cdecl; external 'c' name 'sin';
  117. function fpc_Sin_real(d:ValReal):ValReal;compilerproc; {$ifdef MATHINLINE}inline;{$endif}
  118. begin
  119. result := c_sin(d);
  120. end;
  121. {$endif}
  122. {$ifndef FPC_SYSTEM_HAS_COS}
  123. {$define FPC_SYSTEM_HAS_COS}
  124. function c_cos(d: double): double; cdecl; external 'c' name 'cos';
  125. function fpc_Cos_real(d:ValReal):ValReal;compilerproc; {$ifdef MATHINLINE}inline;{$endif}
  126. begin
  127. result := c_cos(d);
  128. end;
  129. {$endif}
  130. {$ifndef FPC_SYSTEM_HAS_ARCTAN}
  131. {$define FPC_SYSTEM_HAS_ARCTAN}
  132. function c_atan(d: double): double; cdecl; external 'c' name 'atan';
  133. function fpc_ArcTan_real(d:ValReal):ValReal;compilerproc; {$ifdef MATHINLINE}inline;{$endif}
  134. begin
  135. result := c_atan(d);
  136. end;
  137. {$endif}
  138. {$endif not FPC_HAS_TYPE_EXTENDED}