mathu.inc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Florian Klaempfl
  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. { exported by the system unit }
  12. function get_fsr : dword;external name 'FPC_GETFSR';
  13. procedure set_fsr(fsr : dword);external name 'FPC_SETFSR';
  14. function GetRoundMode: TFPURoundingMode;
  15. const
  16. bits2rm: array[0..3] of TFPURoundingMode = (rmNearest,rmTruncate,rmUp,rmDown);
  17. begin
  18. result:=TFPURoundingMode(bits2rm[(get_fsr shr 30) and 3])
  19. end;
  20. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  21. const
  22. rm2bits: array[TFPURoundingMode] of byte = (0,3,2,1);
  23. var
  24. cw: dword;
  25. begin
  26. cw:=get_fsr;
  27. result:=TFPURoundingMode(cw shr 30);
  28. set_fsr((cw and $3fffffff) or (rm2bits[RoundMode] shl 30));
  29. end;
  30. function GetPrecisionMode: TFPUPrecisionMode;
  31. begin
  32. result:=pmDouble;
  33. end;
  34. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  35. begin
  36. result:=pmDouble;
  37. end;
  38. function FSR2ExceptionMask(fsr: dword): TFPUExceptionMask;
  39. begin
  40. result:=[];
  41. { invalid operation: bit 27 }
  42. if (fsr and (1 shl 27))=0 then
  43. include(result,exInvalidOp);
  44. { zero divide: bit 24 }
  45. if (fsr and (1 shl 24))=0 then
  46. include(result,exZeroDivide);
  47. { overflow: bit 26 }
  48. if (fsr and (1 shl 26))=0 then
  49. include(result,exOverflow);
  50. { underflow: bit 25 }
  51. if (fsr and (1 shl 25))=0 then
  52. include(result,exUnderflow);
  53. { Precision (inexact result): bit 23 }
  54. if (fsr and (1 shl 23))=0 then
  55. include(result,exPrecision);
  56. end;
  57. function GetExceptionMask: TFPUExceptionMask;
  58. begin
  59. result:=FSR2ExceptionMask(get_fsr);
  60. end;
  61. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  62. var
  63. fsr : dword;
  64. begin
  65. fsr:=get_fsr;
  66. result:=FSR2ExceptionMask(fsr);
  67. { invalid operation: bit 27 }
  68. if (exInvalidOp in mask) then
  69. fsr:=fsr and not(1 shl 27)
  70. else
  71. fsr:=fsr or (1 shl 27);
  72. { zero divide: bit 24 }
  73. if (exZeroDivide in mask) then
  74. fsr:=fsr and not(1 shl 24)
  75. else
  76. fsr:=fsr or (1 shl 24);
  77. { overflow: bit 26 }
  78. if (exOverflow in mask) then
  79. fsr:=fsr and not(1 shl 26)
  80. else
  81. fsr:=fsr or (1 shl 26);
  82. { underflow: bit 25 }
  83. if (exUnderflow in mask) then
  84. fsr:=fsr and not(1 shl 25)
  85. else
  86. fsr:=fsr or (1 shl 25);
  87. { Precision (inexact result): bit 23 }
  88. if (exPrecision in mask) then
  89. fsr:=fsr and not(1 shl 23)
  90. else
  91. fsr:=fsr or (1 shl 23);
  92. { update control register contents }
  93. set_fsr(fsr);
  94. end;
  95. procedure ClearExceptions(RaisePending: Boolean =true);
  96. begin
  97. set_fsr(get_fsr and $fffffc1f);
  98. end;