mathu.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. softfloat_rounding_mode:=RoundMode;
  28. result:=TFPURoundingMode(cw shr 30);
  29. set_fsr((cw and $3fffffff) or (rm2bits[RoundMode] shl 30));
  30. end;
  31. function GetPrecisionMode: TFPUPrecisionMode;
  32. begin
  33. result:=pmDouble;
  34. end;
  35. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  36. begin
  37. result:=pmDouble;
  38. end;
  39. function FSR2ExceptionMask(fsr: dword): TFPUExceptionMask;
  40. begin
  41. result:=[];
  42. { invalid operation: bit 27 }
  43. if (fsr and (1 shl 27))=0 then
  44. include(result,exInvalidOp);
  45. { zero divide: bit 24 }
  46. if (fsr and (1 shl 24))=0 then
  47. include(result,exZeroDivide);
  48. { overflow: bit 26 }
  49. if (fsr and (1 shl 26))=0 then
  50. include(result,exOverflow);
  51. { underflow: bit 25 }
  52. if (fsr and (1 shl 25))=0 then
  53. include(result,exUnderflow);
  54. { Precision (inexact result): bit 23 }
  55. if (fsr and (1 shl 23))=0 then
  56. include(result,exPrecision);
  57. end;
  58. function GetExceptionMask: TFPUExceptionMask;
  59. begin
  60. result:=FSR2ExceptionMask(get_fsr);
  61. end;
  62. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  63. var
  64. fsr : dword;
  65. begin
  66. fsr:=get_fsr;
  67. result:=FSR2ExceptionMask(fsr);
  68. { invalid operation: bit 27 }
  69. if (exInvalidOp in mask) then
  70. fsr:=fsr and not(1 shl 27)
  71. else
  72. fsr:=fsr or (1 shl 27);
  73. { zero divide: bit 24 }
  74. if (exZeroDivide in mask) then
  75. fsr:=fsr and not(1 shl 24)
  76. else
  77. fsr:=fsr or (1 shl 24);
  78. { overflow: bit 26 }
  79. if (exOverflow in mask) then
  80. fsr:=fsr and not(1 shl 26)
  81. else
  82. fsr:=fsr or (1 shl 26);
  83. { underflow: bit 25 }
  84. if (exUnderflow in mask) then
  85. fsr:=fsr and not(1 shl 25)
  86. else
  87. fsr:=fsr or (1 shl 25);
  88. { Precision (inexact result): bit 23 }
  89. if (exPrecision in mask) then
  90. fsr:=fsr and not(1 shl 23)
  91. else
  92. fsr:=fsr or (1 shl 23);
  93. { update control register contents }
  94. set_fsr(fsr);
  95. end;
  96. procedure ClearExceptions(RaisePending: Boolean =true);
  97. begin
  98. set_fsr(get_fsr and $fffffc1f);
  99. end;