mathu.inc 3.1 KB

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