mathu.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. ExceptionMask = InvalidOperationMask or OverflowMask or UnderflowMask or ZeroDivideMask or InexactMask;
  20. AllConfigBits = ExceptionMask or NonIEEEModeMask or RoundModeMask;
  21. function getFPSCR : DWord; assembler; nostackframe;
  22. asm
  23. mffs f0
  24. stfd f0, -8(r1)
  25. ld r3, -8(r1)
  26. end;
  27. procedure setFPSCR(newFPSCR : DWord); assembler; nostackframe;
  28. asm
  29. std r3, -8(r1)
  30. lfd f0, -8(r1)
  31. mtfsf 255, f0
  32. end;
  33. function GetRoundMode: TFPURoundingMode;
  34. begin
  35. case (getFPSCR and RoundModeMask) of
  36. 0 : result := rmNearest;
  37. 1 : result := rmTruncate;
  38. 2 : result := rmUp;
  39. 3 : result := rmDown;
  40. end;
  41. end;
  42. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  43. var
  44. mode : DWord;
  45. begin
  46. case (RoundMode) of
  47. rmNearest : mode := 0;
  48. rmTruncate : mode := 1;
  49. rmUp : mode := 2;
  50. rmDown : mode := 3;
  51. end;
  52. setFPSCR((getFPSCR and (not RoundModeMask)) or mode);
  53. result := RoundMode;
  54. end;
  55. function GetPrecisionMode: TFPUPrecisionMode;
  56. begin
  57. result := pmDouble;
  58. end;
  59. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  60. begin
  61. { nothing to do, not supported }
  62. result := pmDouble;
  63. end;
  64. function GetExceptionMask: TFPUExceptionMask;
  65. begin
  66. result := [];
  67. if ((getFPSCR and InvalidOperationMask) <> 0) then
  68. result := result + [exInvalidOp];
  69. if ((getFPSCR and OverflowMask) <> 0) then
  70. result := result + [exOverflow];
  71. if ((getFPSCR and UnderflowMask) <> 0) then
  72. result := result + [exUnderflow];
  73. if ((getFPSCR and ZeroDivideMask) <> 0) then
  74. result := result + [exZeroDivide];
  75. if ((getFPSCR and InexactMask) <> 0) then
  76. result := result + [exPrecision];
  77. end;
  78. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  79. var
  80. mode : DWord;
  81. begin
  82. mode := 0;
  83. if (exInvalidOp in Mask) then
  84. mode := mode or InvalidOperationMask;
  85. if (exOverflow in Mask) then
  86. mode := mode or OverflowMask;
  87. if (exUnderflow in Mask) then
  88. mode := mode or UnderflowMask;
  89. if (exZeroDivide in Mask) then
  90. mode := mode or ZeroDivideMask;
  91. if (exPrecision in Mask) then
  92. mode := mode or InexactMask;
  93. setFPSCR((getFPSCR and (not ExceptionMask)) or mode);
  94. result := Mask - [exDenormalized];
  95. end;
  96. procedure ClearExceptions(RaisePending: Boolean = true);
  97. begin
  98. { RaisePending has no effect on PPC, always raises them at the correct location }
  99. setFPSCR(getFPSCR and (not AllConfigBits));
  100. end;