mathu.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. lwz r3, -12(r1)
  26. end;
  27. procedure setFPSCR(newFPSCR : DWord); assembler; nostackframe;
  28. asm
  29. stw r3, -12(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. end;
  63. function GetExceptionMask: TFPUExceptionMask;
  64. begin
  65. result := [];
  66. if ((getFPSCR and InvalidOperationMask) <> 0) then
  67. result := result + [exInvalidOp];
  68. if ((getFPSCR and OverflowMask) <> 0) then
  69. result := result + [exOverflow];
  70. if ((getFPSCR and UnderflowMask) <> 0) then
  71. result := result + [exUnderflow];
  72. if ((getFPSCR and ZeroDivideMask) <> 0) then
  73. result := result + [exZeroDivide];
  74. if ((getFPSCR and InexactMask) <> 0) then
  75. result := result + [exPrecision];
  76. end;
  77. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  78. var
  79. mode : DWord;
  80. begin
  81. mode := 0;
  82. if (exInvalidOp in Mask) then
  83. mode := mode or InvalidOperationMask;
  84. if (exOverflow in Mask) then
  85. mode := mode or OverflowMask;
  86. if (exUnderflow in Mask) then
  87. mode := mode or UnderflowMask;
  88. if (exZeroDivide in Mask) then
  89. mode := mode or ZeroDivideMask;
  90. if (exPrecision in Mask) then
  91. mode := mode or InexactMask;
  92. setFPSCR((getFPSCR and (not ExceptionMask)) or mode);
  93. result := Mask - [exDenormalized];
  94. end;
  95. procedure ClearExceptions(RaisePending: Boolean = true);
  96. begin
  97. { RaisePending has no effect on PPC, always raises them at the correct location }
  98. setFPSCR(getFPSCR and (not AllConfigBits));
  99. end;