2
0

mathh.inc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Florian Klaempfl,
  4. member of the Free Pascal development team
  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. { i386 FPU Controlword }
  12. {$if defined(cpui8086) or defined(cpui386) or defined(cpux86_64)}
  13. const
  14. Default8087CW : word = $1332;
  15. procedure Set8087CW(cw:word);
  16. function Get8087CW:word;
  17. {$endif}
  18. {$if defined (cpui386) or defined(cpux86_64)}
  19. const
  20. DefaultMXCSR: dword = $1900;
  21. procedure SetMXCSR(w: dword);
  22. function GetMXCSR: dword;
  23. procedure SetSSECSR(w : dword); deprecated 'Renamed to SetMXCSR';
  24. function GetSSECSR : dword; deprecated 'Renamed to GetMXCSR';
  25. {$endif}
  26. {$if defined(cpum68k)}
  27. {$if defined(fpu68881) or defined(fpucoldfire)}
  28. const
  29. {$ifdef FPC_68K_SYSTEM_HAS_FPU_EXCEPTIONS}
  30. Default68KFPCR: DWord = $3400; { Enable OVFL, OPERR and DZ, round to nearest, default precision }
  31. {$else}
  32. Default68KFPCR: DWord = 0;
  33. {$endif}
  34. procedure SetFPCR(x: DWord);
  35. procedure SetFPSR(x: DWord);
  36. function GetFPCR: DWord;
  37. function GetFPSR: DWord;
  38. {$endif}
  39. {$endif}
  40. type
  41. TFPURoundingMode = (rmNearest, rmDown, rmUp, rmTruncate);
  42. TFPUPrecisionMode = (pmSingle, pmReserved, pmDouble, pmExtended);
  43. TFPUException = (exInvalidOp, exDenormalized, exZeroDivide,
  44. exOverflow, exUnderflow, exPrecision);
  45. TFPUExceptionMask = set of TFPUException;
  46. const
  47. {*
  48. -------------------------------------------------------------------------------
  49. Software IEC/IEEE floating-point exception flags.
  50. -------------------------------------------------------------------------------
  51. *}
  52. float_flag_invalid = exInvalidOp;
  53. float_flag_denormal = exDenormalized;
  54. float_flag_divbyzero = exZeroDivide;
  55. float_flag_overflow = exOverflow;
  56. float_flag_underflow = exUnderflow;
  57. float_flag_inexact = exPrecision;
  58. {*
  59. -------------------------------------------------------------------------------
  60. Software IEC/IEEE floating-point rounding mode.
  61. -------------------------------------------------------------------------------
  62. *}
  63. float_round_nearest_even = rmNearest;
  64. float_round_down = rmDown;
  65. float_round_up = rmUp;
  66. float_round_to_zero = rmTruncate;
  67. {$ifdef FPC_HAS_FEATURE_THREADING}
  68. ThreadVar
  69. {$else FPC_HAS_FEATURE_THREADING}
  70. Var
  71. {$endif FPC_HAS_FEATURE_THREADING}
  72. softfloat_exception_mask : TFPUExceptionMask;
  73. softfloat_exception_flags : TFPUExceptionMask;
  74. softfloat_rounding_mode : TFPURoundingMode;
  75. procedure float_raise(i: TFPUException);
  76. procedure float_raise(i: TFPUExceptionMask);
  77. {$ifdef cpui386}
  78. {$define INTERNMATH}
  79. {$endif}
  80. {$ifndef INTERNMATH}
  81. {$ifdef FPC_USE_LIBC}
  82. {$ifdef SYSTEMINLINE}
  83. {$define MATHINLINE}
  84. {$endif}
  85. {$endif}
  86. {$endif}
  87. function Pi : ValReal;[internproc:fpc_in_pi_real];
  88. function Abs(d : ValReal) : ValReal;[internproc:fpc_in_abs_real];
  89. function Sqr(d : ValReal) : ValReal;[internproc:fpc_in_sqr_real];
  90. function Sqrt(d : ValReal) : ValReal;[internproc:fpc_in_sqrt_real];
  91. function ArcTan(d : ValReal) : ValReal;[internproc:fpc_in_arctan_real];
  92. function Ln(d : ValReal) : ValReal;[internproc:fpc_in_ln_real];
  93. function Sin(d : ValReal) : ValReal;[internproc:fpc_in_sin_real];
  94. function Cos(d : ValReal) : ValReal;[internproc:fpc_in_cos_real];
  95. function Exp(d : ValReal) : ValReal;[internproc:fpc_in_exp_real];
  96. function Round(d : ValReal) : int64;[internproc:fpc_in_round_real];
  97. function Frac(d : ValReal) : ValReal;[internproc:fpc_in_frac_real];
  98. function Int(d : ValReal) : ValReal;[internproc:fpc_in_int_real];
  99. function Trunc(d : ValReal) : int64;[internproc:fpc_in_trunc_real];
  100. {$ifdef SUPPORT_EXTENDED}
  101. function FPower10(val: Extended; Power: Longint): Extended;
  102. {$endif SUPPORT_EXTENDED}
  103. type
  104. Real48 = array[0..5] of byte;
  105. {$ifdef SUPPORT_DOUBLE}
  106. function Real2Double(r : real48) : double;
  107. operator := (b:real48) d:double;
  108. {$endif}
  109. {$ifdef SUPPORT_EXTENDED}
  110. operator := (b:real48) e:extended;
  111. {$endif SUPPORT_EXTENDED}
  112. type
  113. TFloatSpecial = (fsZero,fsNZero,fsDenormal,fsNDenormal,fsPositive,fsNegative,
  114. fsInf,fsNInf,fsNaN,fsInvalidOp);
  115. {$if defined(SUPPORT_EXTENDED) or defined(FPC_SOFT_FPUX80)}
  116. TExtended80Rec = packed record
  117. private
  118. const
  119. Bias = $3FFF;
  120. function GetExp : QWord;
  121. procedure SetExp(e : QWord);
  122. function GetSign : Boolean;
  123. procedure SetSign(s : Boolean);
  124. public
  125. function Mantissa(IncludeHiddenBit: Boolean = False) : QWord; // unused parameter inserted to have consistent function signature
  126. function Fraction : Extended;
  127. function Exponent : Longint;
  128. property Sign : Boolean read GetSign write SetSign;
  129. property Exp : QWord read GetExp write SetExp;
  130. function SpecialType : TFloatSpecial;
  131. procedure BuildUp(const _Sign : Boolean; const _Mantissa : QWord; const _Exponent : Longint);
  132. case byte of
  133. 0: (Bytes : array[0..9] of Byte);
  134. 1: (Words : array[0..4] of Word);
  135. {$ifdef ENDIAN_LITTLE}
  136. 2: (Frac : QWord; _Exp: Word);
  137. {$else ENDIAN_LITTLE}
  138. 2: (_Exp: Word; Frac : QWord);
  139. {$endif ENDIAN_LITTLE}
  140. {$ifdef SUPPORT_EXTENDED}
  141. 3: (Value: Extended);
  142. {$else}
  143. 3: (Value: array[0..9] of Byte);
  144. {$endif}
  145. end;
  146. {$endif SUPPORT_EXTENDED or FPC_SOFT_FPUX80}
  147. {$ifdef SUPPORT_DOUBLE}
  148. TDoubleRec = packed record
  149. private
  150. const
  151. Bias = $3FF;
  152. function GetExp : QWord;
  153. procedure SetExp(e : QWord);
  154. function GetSign : Boolean;
  155. procedure SetSign(s : Boolean);
  156. function GetFrac : QWord;
  157. procedure SetFrac(e : QWord);
  158. public
  159. function Mantissa(IncludeHiddenBit: Boolean = False) : QWord;
  160. function Fraction : ValReal;
  161. function Exponent : Longint;
  162. property Sign : Boolean read GetSign write SetSign;
  163. property Exp : QWord read GetExp write SetExp;
  164. property Frac : QWord read Getfrac write SetFrac;
  165. function SpecialType : TFloatSpecial;
  166. procedure BuildUp(const _Sign : Boolean; const _Mantissa : QWord; const _Exponent : Longint);
  167. case byte of
  168. 0: (Bytes : array[0..7] of Byte);
  169. 1: (Words : array[0..3] of Word);
  170. 2: (Data : QWord);
  171. 3: (Value: Double);
  172. end;
  173. {$endif SUPPORT_DOUBLE}
  174. {$ifdef SUPPORT_SINGLE}
  175. TSingleRec = packed record
  176. private
  177. const
  178. Bias = $7F;
  179. function GetExp : QWord;
  180. procedure SetExp(e : QWord);
  181. function GetSign : Boolean;
  182. procedure SetSign(s : Boolean);
  183. function GetFrac : QWord;
  184. procedure SetFrac(e : QWord);
  185. public
  186. function Mantissa(IncludeHiddenBit: Boolean = False) : QWord;
  187. function Fraction : ValReal;
  188. function Exponent : Longint;
  189. property Sign : Boolean read GetSign write SetSign;
  190. property Exp : QWord read GetExp write SetExp;
  191. property Frac : QWord read Getfrac write SetFrac;
  192. function SpecialType : TFloatSpecial;
  193. procedure BuildUp(const _Sign : Boolean; const _Mantissa : QWord; const _Exponent : Longint);
  194. case byte of
  195. 0: (Bytes : array[0..3] of Byte);
  196. 1: (Words : array[0..1] of Word);
  197. 2: (Data : DWord);
  198. 3: (Value: Single);
  199. end;
  200. {$endif SUPPORT_SINGLE}
  201. function FMASingle(s1,s2,s3 : single) : single;[internproc:fpc_in_fma_single];
  202. {$ifdef SUPPORT_DOUBLE}
  203. function FMADouble(d1,d2,d3 : double) : double;[internproc:fpc_in_fma_double];
  204. {$endif SUPPORT_DOUBLE}
  205. {$ifdef SUPPORT_EXTENDED}
  206. function FMAExtended(e1,e2,e3 : extended) : extended;[internproc:fpc_in_fma_extended];
  207. {$endif SUPPORT_EXTENDED}
  208. {$ifdef SUPPORT_FLOAT128}
  209. function FMAFloat128(f1,f2,f3 : float128) : float128;[internproc:fpc_in_fma_float128];
  210. {$endif SUPPORT_FLOAT128}