ppcmathu.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. {$ifdef aix}
  24. const
  25. FP_RND_RZ = 0;
  26. FP_RND_RN = 1;
  27. FP_RND_RP = 2;
  28. FP_RND_RM = 3;
  29. function feclearexcept(Mask: DWord):DWord;cdecl;external;
  30. {$else aix}
  31. const
  32. FP_RND_RZ = 1;
  33. FP_RND_RN = 0;
  34. FP_RND_RP = 2;
  35. FP_RND_RM = 3;
  36. {$endif aix}
  37. function GetRoundMode: TFPURoundingMode;
  38. begin
  39. {$ifndef aix}
  40. case GetNativeFPUControlWord and RoundModeMask of
  41. {$else not aix}
  42. case GetNativeFPUControlWord.rndmode of
  43. {$endif not aix}
  44. FP_RND_RN : result := rmNearest;
  45. FP_RND_RZ : result := rmTruncate;
  46. FP_RND_RP : result := rmUp;
  47. FP_RND_RM : result := rmDown;
  48. end;
  49. end;
  50. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  51. var
  52. mode : DWord;
  53. currentcw: TNativeFPUControlWord;
  54. begin
  55. softfloat_rounding_mode:=RoundMode;
  56. case (RoundMode) of
  57. rmNearest :
  58. begin
  59. mode := FP_RND_RN;
  60. end;
  61. rmTruncate :
  62. begin
  63. mode := FP_RND_RZ;
  64. end;
  65. rmUp :
  66. begin
  67. mode := FP_RND_RP;
  68. end;
  69. rmDown :
  70. begin
  71. mode := FP_RND_RM;
  72. end;
  73. end;
  74. result := GetRoundMode;
  75. currentcw:=GetNativeFPUControlWord;
  76. {$ifndef aix}
  77. SetNativeFPUControlWord((currentcw and (not RoundModeMask)) or mode);
  78. {$else not aix}
  79. currentcw.rndmode:=mode;
  80. SetNativeFPUControlWord(currentcw);
  81. {$endif not aix}
  82. end;
  83. function GetPrecisionMode: TFPUPrecisionMode;
  84. begin
  85. result := pmDouble;
  86. end;
  87. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  88. begin
  89. { nothing to do, not supported }
  90. result := pmDouble;
  91. end;
  92. function GetExceptionMask: TFPUExceptionMask;
  93. var
  94. currentExceptionMask: cardinal;
  95. begin
  96. result := [];
  97. {$ifndef aix}
  98. currentExceptionMask:=GetNativeFPUControlWord;
  99. {$else}
  100. currentExceptionMask:=GetNativeFPUControlWord.exceptionmask;
  101. {$endif}
  102. if ((currentExceptionMask and InvalidOperationMask) = 0) then
  103. result := result + [exInvalidOp];
  104. if ((currentExceptionMask and OverflowMask) = 0) then
  105. result := result + [exOverflow];
  106. if ((currentExceptionMask and UnderflowMask) = 0) then
  107. result := result + [exUnderflow];
  108. if ((currentExceptionMask and ZeroDivideMask) = 0) then
  109. result := result + [exZeroDivide];
  110. if ((currentExceptionMask and InexactMask) = 0) then
  111. result := result + [exPrecision];
  112. end;
  113. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  114. var
  115. mode : DWord;
  116. currentcw: TNativeFPUControlWord;
  117. begin
  118. mode := 0;
  119. softfloat_exception_mask := mask;
  120. if (exInvalidOp in Mask) then
  121. begin
  122. mode := mode or InvalidOperationMask;
  123. end;
  124. if (exOverflow in Mask) then
  125. begin
  126. mode := mode or OverflowMask;
  127. end;
  128. if (exUnderflow in Mask) then
  129. begin
  130. mode := mode or UnderflowMask;
  131. end;
  132. if (exZeroDivide in Mask) then
  133. begin
  134. mode := mode or ZeroDivideMask;
  135. end;
  136. if (exPrecision in Mask) then
  137. begin
  138. mode := mode or InexactMask;
  139. end;
  140. softfloat_exception_flags := [];
  141. currentcw:=GetNativeFPUControlWord;
  142. {$ifdef aix}
  143. currentcw.exceptionmask:=ExceptionMask and not mode;
  144. {$else}
  145. currentcw:=(currentcw or ExceptionMask) and not mode and not ExceptionsPendingMask;
  146. {$endif}
  147. SetNativeFPUControlWord(currentcw);
  148. { also clear out pending exceptions on AIX }
  149. {$ifdef aix}
  150. { clear pending exceptions }
  151. feclearexcept(AllExceptionsMask);
  152. {$endif}
  153. result := Mask - [exDenormalized];
  154. end;
  155. procedure ClearExceptions(RaisePending: Boolean = true);
  156. begin
  157. {$ifdef aix}
  158. { clear pending exceptions }
  159. feclearexcept(AllExceptionsMask);
  160. {$else}
  161. { RaisePending has no effect on PPC, always raises them at the correct location }
  162. SetNativeFPUControlWord(GetNativeFPUControlWord and (not ExceptionsPendingMask));
  163. {$endif}
  164. softfloat_exception_flags := [];
  165. end;