mathu.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. softfloat_rounding_mode := float_round_nearest_even;
  52. end;
  53. rmTruncate :
  54. begin
  55. mode := 1;
  56. softfloat_rounding_mode := float_round_to_zero;
  57. end;
  58. rmUp :
  59. begin
  60. mode := 2;
  61. softfloat_rounding_mode := float_round_up;
  62. end;
  63. rmDown :
  64. begin
  65. mode := 3;
  66. softfloat_rounding_mode := float_round_down;
  67. end;
  68. end;
  69. setFPSCR((getFPSCR and (not RoundModeMask)) or mode);
  70. result := RoundMode;
  71. end;
  72. function GetPrecisionMode: TFPUPrecisionMode;
  73. begin
  74. result := pmDouble;
  75. end;
  76. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  77. begin
  78. { nothing to do, not supported }
  79. result := pmDouble;
  80. end;
  81. function GetExceptionMask: TFPUExceptionMask;
  82. begin
  83. result := [];
  84. if ((getFPSCR and InvalidOperationMask) = 0) then
  85. result := result + [exInvalidOp];
  86. if ((getFPSCR and OverflowMask) = 0) then
  87. result := result + [exOverflow];
  88. if ((getFPSCR and UnderflowMask) = 0) then
  89. result := result + [exUnderflow];
  90. if ((getFPSCR and ZeroDivideMask) = 0) then
  91. result := result + [exZeroDivide];
  92. if ((getFPSCR and InexactMask) = 0) then
  93. result := result + [exPrecision];
  94. end;
  95. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  96. var
  97. mode : DWord;
  98. begin
  99. mode := 0;
  100. if (exInvalidOp in Mask) then
  101. mode := mode or InvalidOperationMask;
  102. if (exOverflow in Mask) then
  103. mode := mode or OverflowMask;
  104. if (exUnderflow in Mask) then
  105. mode := mode or UnderflowMask;
  106. if (exZeroDivide in Mask) then
  107. mode := mode or ZeroDivideMask;
  108. if (exPrecision in Mask) then
  109. mode := mode or InexactMask;
  110. setFPSCR((getFPSCR or ExceptionMask) and not mode and not ExceptionsPendingMask);
  111. result := Mask - [exDenormalized];
  112. end;
  113. procedure ClearExceptions(RaisePending: Boolean = true);
  114. begin
  115. { RaisePending has no effect on PPC, always raises them at the correct location }
  116. setFPSCR(getFPSCR and (not ExceptionsPendingMask));
  117. end;