mathu.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2005 by Thomas Schatzl
  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. const
  12. RoundModeMask = %00000011;
  13. NonIEEEModeMask = %00000100;
  14. InvalidOperationMask = %10000000;
  15. OverflowMask = %01000000;
  16. UnderflowMask = %00100000;
  17. ZeroDivideMask = %00010000;
  18. InexactMask = %00001000;
  19. AllExceptionsMask = %11111000;
  20. ExceptionsPendingMask = %11111111111111100000011100000000;
  21. ExceptionMask = InvalidOperationMask or OverflowMask or UnderflowMask or ZeroDivideMask or InexactMask;
  22. AllConfigBits = ExceptionMask or NonIEEEModeMask or RoundModeMask;
  23. function getFPSCR : DWord; assembler; nostackframe;
  24. asm
  25. mffs f0
  26. stfd f0, -16(r1)
  27. {$ifdef FPC_BIG_ENDIAN}
  28. lwz r3, -12(r1)
  29. {$else}
  30. lwz r3, -16(r1)
  31. {$endif}
  32. end;
  33. procedure setFPSCR(newFPSCR : DWord); assembler; nostackframe;
  34. asm
  35. {$ifdef FPC_BIG_ENDIAN}
  36. stw r3, -12(r1)
  37. {$else}
  38. stw r3, -16(r1)
  39. {$endif}
  40. lfd f0, -16(r1)
  41. mtfsf 255, f0
  42. end;
  43. {$ifdef aix}
  44. const
  45. FP_RND_RZ = 0;
  46. FP_RND_RN = 1;
  47. FP_RND_RP = 2;
  48. FP_RND_RM = 3;
  49. function fp_is_enabled(Mask: DWord): boolean;cdecl;external;
  50. procedure fp_enable(Mask: DWord);cdecl;external;
  51. function feclearexcept(Mask: DWord):DWord;cdecl;external;
  52. procedure fp_disable(Mask: DWord);cdecl;external;
  53. function fp_read_rnd: word;cdecl;external;
  54. function fp_swap_rnd(RoundMode: word): word;cdecl;external;
  55. {$else aix}
  56. const
  57. FP_RND_RZ = 1;
  58. FP_RND_RN = 0;
  59. FP_RND_RP = 2;
  60. FP_RND_RM = 3;
  61. {$endif aix}
  62. function GetRoundMode: TFPURoundingMode;
  63. begin
  64. {$ifndef aix}
  65. case (getFPSCR and RoundModeMask) of
  66. {$else not aix}
  67. case fp_read_rnd of
  68. {$endif not aix}
  69. FP_RND_RN : result := rmNearest;
  70. FP_RND_RZ : result := rmTruncate;
  71. FP_RND_RP : result := rmUp;
  72. FP_RND_RM : result := rmDown;
  73. end;
  74. end;
  75. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  76. var
  77. mode : DWord;
  78. begin
  79. softfloat_rounding_mode:=RoundMode;
  80. case (RoundMode) of
  81. rmNearest :
  82. begin
  83. mode := FP_RND_RN;
  84. end;
  85. rmTruncate :
  86. begin
  87. mode := FP_RND_RZ;
  88. end;
  89. rmUp :
  90. begin
  91. mode := FP_RND_RP;
  92. end;
  93. rmDown :
  94. begin
  95. mode := FP_RND_RM;
  96. end;
  97. end;
  98. {$ifndef aix}
  99. setFPSCR((getFPSCR and (not RoundModeMask)) or mode);
  100. {$else not aix}
  101. fp_swap_rnd(mode);
  102. {$endif not aix}
  103. result := RoundMode;
  104. end;
  105. function GetPrecisionMode: TFPUPrecisionMode;
  106. begin
  107. result := pmDouble;
  108. end;
  109. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  110. begin
  111. { nothing to do, not supported }
  112. result := pmDouble;
  113. end;
  114. function GetExceptionMask: TFPUExceptionMask;
  115. begin
  116. result := [];
  117. {$ifndef aix}
  118. if ((getFPSCR and InvalidOperationMask) = 0) then
  119. result := result + [exInvalidOp];
  120. if ((getFPSCR and OverflowMask) = 0) then
  121. result := result + [exOverflow];
  122. if ((getFPSCR and UnderflowMask) = 0) then
  123. result := result + [exUnderflow];
  124. if ((getFPSCR and ZeroDivideMask) = 0) then
  125. result := result + [exZeroDivide];
  126. if ((getFPSCR and InexactMask) = 0) then
  127. result := result + [exPrecision];
  128. {$else not aix}
  129. if not fp_is_enabled(InvalidOperationMask) then
  130. result := result + [exInvalidOp];
  131. if not fp_is_enabled(OverflowMask) then
  132. result := result + [exOverflow];
  133. if not fp_is_enabled(UnderflowMask) then
  134. result := result + [exUnderflow];
  135. if not fp_is_enabled(ZeroDivideMask) then
  136. result := result + [exZeroDivide];
  137. if not fp_is_enabled(InexactMask) then
  138. result := result + [exPrecision];
  139. {$endif not aix}
  140. end;
  141. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  142. var
  143. mode : DWord;
  144. begin
  145. mode := 0;
  146. softfloat_exception_mask := mask;
  147. if (exInvalidOp in Mask) then
  148. begin
  149. mode := mode or InvalidOperationMask;
  150. end;
  151. if (exOverflow in Mask) then
  152. begin
  153. mode := mode or OverflowMask;
  154. end;
  155. if (exUnderflow in Mask) then
  156. begin
  157. mode := mode or UnderflowMask;
  158. end;
  159. if (exZeroDivide in Mask) then
  160. begin
  161. mode := mode or ZeroDivideMask;
  162. end;
  163. if (exPrecision in Mask) then
  164. begin
  165. mode := mode or InexactMask;
  166. end;
  167. setFPSCR((getFPSCR or ExceptionMask) and not mode and not ExceptionsPendingMask);
  168. softfloat_exception_flags := [];;
  169. { also clear out pending exceptions on AIX }
  170. {$ifdef aix}
  171. { clear pending exceptions }
  172. feclearexcept(AllExceptionsMask);
  173. { enable the exceptions that are not disabled }
  174. fp_enable(mode xor AllExceptionsMask);
  175. { and disable the rest }
  176. fp_disable(mode);
  177. {$endif}
  178. result := Mask - [exDenormalized];
  179. end;
  180. procedure ClearExceptions(RaisePending: Boolean = true);
  181. begin
  182. {$ifdef aix}
  183. { clear pending exceptions }
  184. feclearexcept(AllExceptionsMask);
  185. {$endif}
  186. softfloat_exception_flags := [];
  187. { RaisePending has no effect on PPC, always raises them at the correct location }
  188. setFPSCR(getFPSCR and (not ExceptionsPendingMask));
  189. end;