cgenmath.inc 5.9 KB

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