cgenmath.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. {$ifdef aix}
  16. { aix math library routines don't raise exceptions, you have to manually
  17. check for them }
  18. function feclearexcept(flags: longint): longint; cdecl; external 'c';
  19. function fetestexcept(flags: longint): longint; cdecl; external 'c';
  20. const
  21. FE_DIVBYZERO = $04000000;
  22. FE_INEXACT = $02000000;
  23. FE_INVALID = $20000000;
  24. FE_OVERFLOW = $10000000;
  25. FE_UNDERFLOW = $08000000;
  26. FE_ALL_EXCEPT = $3E000000;
  27. procedure resetexcepts;
  28. begin
  29. seterrno(0);
  30. feclearexcept(FE_ALL_EXCEPT);
  31. end;
  32. procedure checkexcepts;
  33. var
  34. feres: longint;
  35. sfexcepts: TFPUExceptionMask;
  36. begin
  37. feres:=fetestexcept(FE_ALL_EXCEPT);
  38. sfexcepts:=[];
  39. if feres<>0 then
  40. begin
  41. if (feres and FE_DIVBYZERO) <> 0 then
  42. include(sfexcepts,float_flag_divbyzero);
  43. if (feres and FE_INEXACT) <> 0 then
  44. include(sfexcepts,float_flag_inexact);
  45. if (feres and FE_INVALID) <> 0 then
  46. include(sfexcepts,float_flag_invalid);
  47. if (feres and FE_OVERFLOW) <> 0 then
  48. include(sfexcepts,float_flag_overflow);
  49. if (feres and FE_UNDERFLOW) <> 0 then
  50. include(sfexcepts,float_flag_underflow);
  51. end
  52. { unknown error }
  53. else if (geterrno<>0) then
  54. include(sfexcepts,float_flag_invalid);
  55. if sfexcepts<>[] then
  56. float_raise(sfexcepts);
  57. end;
  58. {$else aix}
  59. procedure resetexcepts; inline;
  60. begin
  61. end;
  62. procedure checkexcepts; inline;
  63. begin
  64. end;
  65. {$endif aix}
  66. {$ifndef SOLARIS}
  67. {$ifndef FPC_SYSTEM_HAS_INT}
  68. {$define FPC_SYSTEM_HAS_INT}
  69. {$ifdef SUPPORT_DOUBLE}
  70. function c_trunc(d: double): double; cdecl; external 'c' name 'trunc';
  71. function fpc_int_real(d: ValReal): ValReal;compilerproc; {$ifdef MATHINLINE}inline;{$endif}
  72. begin
  73. resetexcepts;
  74. result := c_trunc(d);
  75. checkexcepts;
  76. end;
  77. {$else SUPPORT_DOUBLE}
  78. function c_truncf(d: single): double; cdecl; external 'c' name 'truncf';
  79. function fpc_int_real(d: ValReal): ValReal;compilerproc; {$ifdef MATHINLINE}inline;{$endif}
  80. begin
  81. { this will be correct since real = single in the case of }
  82. { the motorola version of the compiler... }
  83. resetexcepts;
  84. int:=c_truncf(d);
  85. checkexcepts;
  86. end;
  87. {$endif SUPPORT_DOUBLE}
  88. {$endif FPC_SYSTEM_HAS_INT}
  89. {$endif SOLARIS}
  90. {$ifndef SYSTEM_HAS_FREXP}
  91. {$define SYSTEM_HAS_FREXP}
  92. function c_frexp(x: double; out e: longint): double; cdecl; external 'c' name 'frexp';
  93. procedure frexp(x:ValReal; out Mantissa: ValReal; out Exponent: longint); {$ifdef MATHINLINE}inline;{$endif}
  94. begin
  95. resetexcepts;
  96. Mantissa := c_frexp(x,Exponent);
  97. checkexcepts;
  98. end;
  99. {$endif not SYSTEM_HAS_FREXP}
  100. {$ifndef SYSTEM_HAS_LDEXP}
  101. {$define SYSTEM_HAS_LDEXP}
  102. function c_ldexp(x: double; n: longint): double; cdecl; external 'c' name 'ldexp';
  103. function ldexp( x: ValReal; N: Integer):ValReal;{$ifdef MATHINLINE}inline;{$endif}
  104. begin
  105. resetexcepts;
  106. ldexp := c_ldexp(x,n);
  107. checkexcepts;
  108. end;
  109. {$endif not SYSTEM_HAS_LDEXP}
  110. {$ifndef FPC_SYSTEM_HAS_SQRT}
  111. {$define FPC_SYSTEM_HAS_SQRT}
  112. function c_sqrt(d: double): double; cdecl; external 'c' name 'sqrt';
  113. function fpc_sqrt_real(d:ValReal):ValReal;compilerproc; {$ifdef MATHINLINE}inline;{$endif}
  114. begin
  115. resetexcepts;
  116. result := c_sqrt(d);
  117. checkexcepts;
  118. end;
  119. {$endif}
  120. {$ifndef FPC_SYSTEM_HAS_EXP}
  121. {$define FPC_SYSTEM_HAS_EXP}
  122. function c_exp(d: double): double; cdecl; external 'c' name 'exp';
  123. function fpc_Exp_real(d:ValReal):ValReal;compilerproc; {$ifdef MATHINLINE}inline;{$endif}
  124. begin
  125. resetexcepts;
  126. result := c_exp(d);
  127. checkexcepts;
  128. end;
  129. {$endif}
  130. { buggy on aix, sets DIV_BY_ZERO flag for some valid inputs }
  131. {$ifndef aix}
  132. {$ifndef FPC_SYSTEM_HAS_LN}
  133. {$define FPC_SYSTEM_HAS_LN}
  134. function c_log(d: double): double; cdecl; external 'c' name 'log';
  135. function fpc_Ln_real(d:ValReal):ValReal;compilerproc;{$ifdef MATHINLINE}inline;{$endif}
  136. begin
  137. resetexcepts;
  138. result := c_log(d);
  139. checkexcepts;
  140. end;
  141. {$endif}
  142. {$endif}
  143. {$ifndef FPC_SYSTEM_HAS_SIN}
  144. {$define FPC_SYSTEM_HAS_SIN}
  145. function c_sin(d: double): double; cdecl; external 'c' name 'sin';
  146. function fpc_Sin_real(d:ValReal):ValReal;compilerproc; {$ifdef MATHINLINE}inline;{$endif}
  147. begin
  148. resetexcepts;
  149. result := c_sin(d);
  150. checkexcepts;
  151. end;
  152. {$endif}
  153. {$ifndef FPC_SYSTEM_HAS_COS}
  154. {$define FPC_SYSTEM_HAS_COS}
  155. function c_cos(d: double): double; cdecl; external 'c' name 'cos';
  156. function fpc_Cos_real(d:ValReal):ValReal;compilerproc; {$ifdef MATHINLINE}inline;{$endif}
  157. begin
  158. resetexcepts;
  159. result := c_cos(d);
  160. checkexcepts;
  161. end;
  162. {$endif}
  163. {$ifndef FPC_SYSTEM_HAS_ARCTAN}
  164. {$define FPC_SYSTEM_HAS_ARCTAN}
  165. function c_atan(d: double): double; cdecl; external 'c' name 'atan';
  166. function fpc_ArcTan_real(d:ValReal):ValReal;compilerproc; {$ifdef MATHINLINE}inline;{$endif}
  167. begin
  168. resetexcepts;
  169. result := c_atan(d);
  170. checkexcepts;
  171. end;
  172. {$endif}
  173. {$endif not FPC_HAS_TYPE_EXTENDED}