mathu.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2012 by Sven Barth
  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. {$if defined(FPU68881) or defined(FPUCOLDFIRE)}
  12. { 68881/2 FPCR Encodings
  13. Rounding Mode Rounding Precision
  14. (RND Field) Encoding (PREC Field)
  15. To Nearest (RN) 0 0 Extend (X)
  16. To Zero (RZ) 0 1 Single (S)
  17. To Minus Infinity (RM) 1 0 Double (D)
  18. To Plus Infinity (RP) 1 1 Undefined
  19. }
  20. { 68881/2 FPCR layout }
  21. { Exception Enable Byte: }
  22. { 15 - BSUN - Branch/Set on Unordered }
  23. { 14 - SNAN - Signal Not A Number }
  24. { 13 - OPERR - Operand Error }
  25. { 12 - OVFL - Overflow }
  26. { 11 - UNFL - Underflow }
  27. { 10 - DZ - Divide by Zero }
  28. { 09 - INEX2 - Inexact Operation }
  29. { 08 - INEX1 - Inexact Decimal Input }
  30. { Mode Control Byte: }
  31. { 07 - PREC - Rounding Precision }
  32. { 06 - PREC - Rounding Precision }
  33. { 05 - RND - Rounding Mode }
  34. { 04 - RND - Rounding Mode }
  35. { 03 - 0 - Reserved, Set to zero }
  36. { 02 - 0 - Reserved, Set to zero }
  37. { 01 - 0 - Reserved, Set to zero }
  38. { 00 - 0 - Reserved, Set to zero }
  39. {
  40. Please note that the rounding mode setting via FPCR in most emulators is broken.
  41. The list includes most versions and incarnations of UAE, MorphOS' Trance emulator,
  42. and others. The following code was verified to work on real hardware. (KB)
  43. }
  44. const
  45. FPU68K_ROUND_MASK_SHIFT = 4;
  46. FPU68K_ROUND_MASK = 3 shl FPU68K_ROUND_MASK_SHIFT;
  47. FPU68K_ROUND_NEAREST = 0 shl FPU68K_ROUND_MASK_SHIFT;
  48. FPU68K_ROUND_ZERO = 1 shl FPU68K_ROUND_MASK_SHIFT;
  49. FPU68K_ROUND_MINUSINF = 2 shl FPU68K_ROUND_MASK_SHIFT;
  50. FPU68K_ROUND_PLUSINF = 3 shl FPU68K_ROUND_MASK_SHIFT;
  51. const
  52. FPU68K_PREC_MASK_SHIFT = 6;
  53. FPU68K_PREC_MASK = 3 shl FPU68K_PREC_MASK_SHIFT;
  54. FPU68K_PREC_EXTENDED = 0 shl FPU68K_PREC_MASK_SHIFT;
  55. FPU68K_PREC_SINGLE = 1 shl FPU68K_PREC_MASK_SHIFT;
  56. FPU68K_PREC_DOUBLE = 2 shl FPU68K_PREC_MASK_SHIFT;
  57. const
  58. FPU68K_EXCEPT_MASK_SHIFT = 8;
  59. FPU68K_EXCEPT_MASK = 255 shl FPU68K_EXCEPT_MASK_SHIFT;
  60. FPU68K_EXCEPT_INEX1 = 1 shl (FPU68K_EXCEPT_MASK_SHIFT + 0);
  61. FPU68K_EXCEPT_INEX2 = 1 shl (FPU68K_EXCEPT_MASK_SHIFT + 1);
  62. FPU68K_EXCEPT_DZ = 1 shl (FPU68K_EXCEPT_MASK_SHIFT + 2);
  63. FPU68K_EXCEPT_UNFL = 1 shl (FPU68K_EXCEPT_MASK_SHIFT + 3);
  64. FPU68K_EXCEPT_OVFL = 1 shl (FPU68K_EXCEPT_MASK_SHIFT + 4);
  65. FPU68K_EXCEPT_OPERR = 1 shl (FPU68K_EXCEPT_MASK_SHIFT + 5);
  66. FPU68K_EXCEPT_SNAN = 1 shl (FPU68K_EXCEPT_MASK_SHIFT + 6);
  67. FPU68K_EXCEPT_BSUN = 1 shl (FPU68K_EXCEPT_MASK_SHIFt + 7);
  68. FPU68K_AE_MASK = $F8;
  69. function GetExceptionMask: TFPUExceptionMask;
  70. begin
  71. Result := softfloat_exception_mask;
  72. end;
  73. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  74. const
  75. FPCToFPUExceptionFlags: array[TFPUException] of DWord =
  76. ( {exInvalidOp,} FPU68K_EXCEPT_OPERR or FPU68K_EXCEPT_SNAN or FPU68K_EXCEPT_BSUN,
  77. {exDenormalized,} 0,
  78. {exZeroDivide,} FPU68K_EXCEPT_DZ,
  79. {exOverflow,} FPU68K_EXCEPT_OVFL,
  80. {exUnderflow,} FPU68K_EXCEPT_UNFL,
  81. {exPrecision} FPU68K_EXCEPT_INEX1 or FPU68K_EXCEPT_INEX2 );
  82. FPUToFPCExceptionFlags: array[0..7] of TFPUExceptionMask =
  83. ( [exPrecision], [exPrecision], [exZeroDivide], [exUnderflow], [exOverflow], [exInvalidOp], [exInvalidOp], [exInvalidOp] );
  84. var
  85. oldMode, Mode: DWord;
  86. e: TFPUException;
  87. i: longint;
  88. begin
  89. result:=[];
  90. oldMode:=(GetFPCR and FPU68K_EXCEPT_MASK) shr FPU68K_EXCEPT_MASK_SHIFT;
  91. for i:=low(FPUToFPCExceptionFlags) to high(FPUToFPCExceptionFlags) do
  92. if ((1 shl i) and oldMode) > 0 then
  93. result:=result+FPUToFPCExceptionFlags[i];
  94. mode:=0;
  95. { The bits set inside FPCR register are the enabled exceptions,
  96. not the masked exceptions, thus we need to invert list }
  97. for e:=low(TFPUException) to high(TFPUException) do
  98. if not (e in Mask) then
  99. mode:=mode or FPCToFPUExceptionFlags[e];
  100. SetFPCR((GetFPCR and not FPU68K_EXCEPT_MASK) or (mode and FPU68K_EXCEPT_MASK));
  101. { Wipe out any previous exception }
  102. SetFPSR(GetFPSR and (not (FPU68K_AE_MASK or FPU68K_EXCEPT_MASK)));
  103. softfloat_exception_mask:=mask;
  104. end;
  105. function GetRoundMode: TFPURoundingMode;
  106. const
  107. FPUToFPCRoundingMode: array[0..3] of TFPURoundingMode = ( rmNearest, rmTruncate, rmUp, rmDown );
  108. begin
  109. Result:=FPUToFPCRoundingMode[(GetFPCR and FPU68K_ROUND_MASK) shr FPU68K_ROUND_MASK_SHIFT];
  110. end;
  111. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  112. const
  113. FPCToFPURoundingMode: array[TFPURoundingMode] of DWord =
  114. ( FPU68K_ROUND_NEAREST, FPU68K_ROUND_MINUSINF, FPU68K_ROUND_PLUSINF, FPU68K_ROUND_ZERO );
  115. var
  116. FPCR: DWord;
  117. begin
  118. Result:=GetRoundMode;
  119. FPCR:=GetFPCR and not FPU68K_ROUND_MASK;
  120. SetFPCR(FPCR or FPCToFPURoundingMode[RoundMode]);
  121. softfloat_rounding_mode:=RoundMode;
  122. end;
  123. function GetPrecisionMode: TFPUPrecisionMode;
  124. begin
  125. result:=pmDouble;
  126. end;
  127. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  128. begin
  129. result:=pmDouble;
  130. end;
  131. procedure ClearExceptions(RaisePending: Boolean);
  132. begin
  133. SetFPCR(GetFPCR and not FPU68K_EXCEPT_MASK);
  134. SetFPSR(0);
  135. softfloat_exception_flags:=[];
  136. end;
  137. {$else}
  138. function GetExceptionMask: TFPUExceptionMask;
  139. begin
  140. Result := softfloat_exception_mask;
  141. end;
  142. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  143. begin
  144. result:=softfloat_exception_mask;
  145. softfloat_exception_mask:=mask;
  146. end;
  147. function GetRoundMode: TFPURoundingMode;
  148. begin
  149. Result:=softfloat_rounding_mode;
  150. end;
  151. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  152. begin
  153. Result:=softfloat_rounding_mode;
  154. softfloat_rounding_mode:=RoundMode;
  155. end;
  156. function GetPrecisionMode: TFPUPrecisionMode;
  157. begin
  158. result:=pmDouble;
  159. end;
  160. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  161. begin
  162. result:=pmDouble;
  163. end;
  164. procedure ClearExceptions(RaisePending: Boolean);
  165. begin
  166. softfloat_exception_flags:=[];
  167. end;
  168. {$endif}