mathh.inc 8.9 KB

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