mathu.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2014 by Jonas Maebe
  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. {$asmmode gas}
  12. function getfpcr: dword; nostackframe; assembler;
  13. asm
  14. mrs x0,fpcr
  15. end;
  16. procedure setfpcr(val: dword); nostackframe; assembler;
  17. asm
  18. msr fpcr,x0
  19. end;
  20. function getfpsr: dword; nostackframe; assembler;
  21. asm
  22. mrs x0,fpsr
  23. end;
  24. procedure setfpsr(val: dword); nostackframe; assembler;
  25. asm
  26. msr fpsr, x0
  27. end;
  28. function GetRoundMode: TFPURoundingMode;
  29. const
  30. bits2rm: array[0..3] of TFPURoundingMode = (rmNearest,rmUp,rmDown,rmTruncate);
  31. begin
  32. result:=TFPURoundingMode(bits2rm[(getfpcr shr 22) and 3])
  33. end;
  34. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  35. const
  36. rm2bits: array[TFPURoundingMode] of byte = (0,2,1,3);
  37. begin
  38. softfloat_rounding_mode:=RoundMode;
  39. SetRoundMode:=RoundMode;
  40. setfpcr((getfpcr and $ff3fffff) or (rm2bits[RoundMode] shl 22));
  41. end;
  42. function GetPrecisionMode: TFPUPrecisionMode;
  43. begin
  44. result:=pmDouble;
  45. end;
  46. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  47. begin
  48. result:=pmDouble;
  49. end;
  50. const
  51. fpu_ioe = 1 shl 8;
  52. fpu_dze = 1 shl 9;
  53. fpu_ofe = 1 shl 10;
  54. fpu_ufe = 1 shl 11;
  55. fpu_ixe = 1 shl 12;
  56. fpu_ide = 1 shl 15;
  57. fpu_exception_mask = fpu_ioe or fpu_dze or fpu_ofe or fpu_ufe or fpu_ixe or fpu_ide;
  58. fpu_exception_mask_to_status_mask_shift = 8;
  59. function GetExceptionMask: TFPUExceptionMask;
  60. var
  61. fpcr: dword;
  62. begin
  63. fpcr:=getfpcr;
  64. result:=[];
  65. if ((fpcr and fpu_ioe)=0) then
  66. result := result+[exInvalidOp];
  67. if ((fpcr and fpu_ofe)=0) then
  68. result := result+[exOverflow];
  69. if ((fpcr and fpu_ufe)=0) then
  70. result := result+[exUnderflow];
  71. if ((fpcr and fpu_dze)=0) then
  72. result := result+[exZeroDivide];
  73. if ((fpcr and fpu_ixe)=0) then
  74. result := result+[exPrecision];
  75. if ((fpcr and fpu_ide)=0) then
  76. result := result+[exDenormalized];
  77. end;
  78. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  79. var
  80. newfpcr: dword;
  81. begin
  82. softfloat_exception_mask:=mask;
  83. newfpcr:=fpu_exception_mask;
  84. if exInvalidOp in Mask then
  85. newfpcr:=newfpcr and not(fpu_ioe);
  86. if exOverflow in Mask then
  87. newfpcr:=newfpcr and not(fpu_ofe);
  88. if exUnderflow in Mask then
  89. newfpcr:=newfpcr and not(fpu_ufe);
  90. if exZeroDivide in Mask then
  91. newfpcr:=newfpcr and not(fpu_dze);
  92. if exPrecision in Mask then
  93. newfpcr:=newfpcr and not(fpu_ixe);
  94. if exDenormalized in Mask then
  95. newfpcr:=newfpcr and not(fpu_ide);
  96. { clear "exception happened" flags }
  97. ClearExceptions(false);
  98. { set new exception mask }
  99. setfpcr((getfpcr and not(fpu_exception_mask)) or newfpcr);
  100. { unsupported mask bits will remain 0 -> read exception mask again }
  101. result:=GetExceptionMask;
  102. softfloat_exception_mask:=result;
  103. end;
  104. procedure ClearExceptions(RaisePending: Boolean);
  105. begin
  106. { todo: RaisePending = true }
  107. softfloat_exception_flags:=[];
  108. setfpsr(getfpsr and not(fpu_exception_mask shr fpu_exception_mask_to_status_mask_shift));
  109. end;