mathu.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. ExceptionsPendingMask = %11111111111111100000011100000000;
  20. ExceptionMask = InvalidOperationMask or OverflowMask or UnderflowMask or ZeroDivideMask or InexactMask;
  21. AllConfigBits = ExceptionMask or NonIEEEModeMask or RoundModeMask;
  22. function getFPSCR : DWord; assembler; nostackframe;
  23. asm
  24. mffs f0
  25. stfd f0, -12(r1)
  26. lwz r3, -8(r1)
  27. end;
  28. procedure setFPSCR(newFPSCR : DWord); assembler; nostackframe;
  29. asm
  30. stw r3, -8(r1)
  31. lfd f0, -12(r1)
  32. mtfsf 255, f0
  33. end;
  34. function GetRoundMode: TFPURoundingMode;
  35. begin
  36. case (getFPSCR and RoundModeMask) of
  37. 0 : result := rmNearest;
  38. 1 : result := rmTruncate;
  39. 2 : result := rmUp;
  40. 3 : result := rmDown;
  41. end;
  42. end;
  43. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  44. var
  45. mode : DWord;
  46. begin
  47. case (RoundMode) of
  48. rmNearest :
  49. begin
  50. mode := 0;
  51. { 2.3.x has internal rounding support, which does the right thing }
  52. { automatically }
  53. {$ifdef VER2_2}
  54. softfloat_rounding_mode := float_round_nearest_even;
  55. {$endif}
  56. end;
  57. rmTruncate :
  58. begin
  59. mode := 1;
  60. {$ifdef VER2_2}
  61. softfloat_rounding_mode := float_round_to_zero;
  62. {$endif}
  63. end;
  64. rmUp :
  65. begin
  66. mode := 2;
  67. {$ifdef VER2_2}
  68. softfloat_rounding_mode := float_round_up;
  69. {$endif}
  70. end;
  71. rmDown :
  72. begin
  73. mode := 3;
  74. {$ifdef VER2_2}
  75. softfloat_rounding_mode := float_round_down;
  76. {$endif}
  77. end;
  78. end;
  79. setFPSCR((getFPSCR and (not RoundModeMask)) or mode);
  80. result := RoundMode;
  81. end;
  82. function GetPrecisionMode: TFPUPrecisionMode;
  83. begin
  84. result := pmDouble;
  85. end;
  86. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  87. begin
  88. { nothing to do, not supported }
  89. result := pmDouble;
  90. end;
  91. function GetExceptionMask: TFPUExceptionMask;
  92. begin
  93. result := [];
  94. if ((getFPSCR and InvalidOperationMask) = 0) then
  95. result := result + [exInvalidOp];
  96. if ((getFPSCR and OverflowMask) = 0) then
  97. result := result + [exOverflow];
  98. if ((getFPSCR and UnderflowMask) = 0) then
  99. result := result + [exUnderflow];
  100. if ((getFPSCR and ZeroDivideMask) = 0) then
  101. result := result + [exZeroDivide];
  102. if ((getFPSCR and InexactMask) = 0) then
  103. result := result + [exPrecision];
  104. end;
  105. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  106. var
  107. mode : DWord;
  108. begin
  109. mode := 0;
  110. if (exInvalidOp in Mask) then
  111. mode := mode or InvalidOperationMask;
  112. if (exOverflow in Mask) then
  113. mode := mode or OverflowMask;
  114. if (exUnderflow in Mask) then
  115. mode := mode or UnderflowMask;
  116. if (exZeroDivide in Mask) then
  117. mode := mode or ZeroDivideMask;
  118. if (exPrecision in Mask) then
  119. mode := mode or InexactMask;
  120. setFPSCR((getFPSCR or ExceptionMask) and not mode and not ExceptionsPendingMask);
  121. result := Mask - [exDenormalized];
  122. end;
  123. procedure ClearExceptions(RaisePending: Boolean = true);
  124. begin
  125. { RaisePending has no effect on PPC, always raises them at the correct location }
  126. setFPSCR(getFPSCR and (not ExceptionsPendingMask));
  127. end;