mathu.inc 3.2 KB

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