mathu.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2004 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. {$ASMMODE GAS}
  12. {$ifdef FPC_HAS_TYPE_EXTENDED}
  13. {$define FPC_MATH_HAS_ARCTAN2}
  14. function arctan2(y,x : float) : float;assembler;
  15. asm
  16. fldt y
  17. fldt x
  18. fpatan
  19. fwait
  20. end;
  21. {$define FPC_MATH_HAS_TAN}
  22. function tan(x : float) : float;assembler;
  23. asm
  24. fldt X
  25. fptan
  26. fstp %st
  27. fwait
  28. end;
  29. {$define FPC_MATH_HAS_COTAN}
  30. function cotan(x : float) : float;assembler;
  31. asm
  32. fldt X
  33. fptan
  34. fdivp %st,%st(1)
  35. fwait
  36. end;
  37. {$define FPC_MATH_HAS_LOG2}
  38. function log2(x : float) : float;assembler;
  39. asm
  40. fld1
  41. fldt x
  42. fyl2x
  43. fwait
  44. end;
  45. {$endif FPC_HAS_TYPE_EXTENDED}
  46. {$define FPC_MATH_HAS_SINCOS}
  47. {$ifdef FPC_HAS_TYPE_EXTENDED}
  48. procedure sincos(theta : extended;out sinus,cosinus : extended);assembler;
  49. asm
  50. fldt theta
  51. fsincos
  52. {$ifdef WIN64}
  53. fstpl (%r8)
  54. fstpl (%rdx)
  55. {$else WIN64}
  56. fstpt (%rsi)
  57. fstpt (%rdi)
  58. {$endif WIN64}
  59. fwait
  60. end;
  61. {$endif FPC_HAS_TYPE_EXTENDED}
  62. {$asmmode intel}
  63. procedure sincos(theta : double;out sinus,cosinus : double);assembler;
  64. var
  65. t : double;
  66. asm
  67. movsd qword ptr t,xmm0
  68. fld qword ptr t
  69. fsincos
  70. fstp qword ptr [cosinus]
  71. fstp qword ptr [sinus]
  72. fwait
  73. end;
  74. procedure sincos(theta : single;out sinus,cosinus : single);assembler;
  75. var
  76. t : single;
  77. asm
  78. movss dword ptr t,xmm0
  79. fld dword ptr t
  80. fsincos
  81. fstp dword ptr [cosinus]
  82. fstp dword ptr [sinus]
  83. fwait
  84. end;
  85. {$asmmode gas}
  86. function GetRoundMode: TFPURoundingMode;
  87. begin
  88. {$ifndef FPC_HAS_TYPE_EXTENDED}
  89. Result:=TFPURoundingMode((GetSSECSR shr 13) and $3);
  90. {$else win64}
  91. Result:=TFPURoundingMode((Get8087CW shr 10) and $3);
  92. {$endif win64}
  93. end;
  94. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  95. var
  96. CtlWord: Word;
  97. begin
  98. CtlWord:=Get8087CW;
  99. Set8087CW((CtlWord and $F3FF) or (Ord(RoundMode) shl 10));
  100. SetSSECSR((GetSSECSR and $ffff9fff) or (dword(RoundMode) shl 13));
  101. Result:=GetRoundMode;
  102. end;
  103. function GetPrecisionMode: TFPUPrecisionMode;
  104. begin
  105. Result:=TFPUPrecisionMode((Get8087CW shr 8) and 3);
  106. end;
  107. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  108. var
  109. CtlWord: Word;
  110. begin
  111. CtlWord:=Get8087CW;
  112. Set8087CW((CtlWord and $FCFF) or (Ord(Precision) shl 8));
  113. Result:=TFPUPrecisionMode((CtlWord shr 8) and 3);
  114. end;
  115. function GetExceptionMask: TFPUExceptionMask;
  116. begin
  117. {$ifndef FPC_HAS_TYPE_EXTENDED}
  118. Result:=TFPUExceptionMask(dword((GetSSECSR shr 7) and $3f));
  119. {$else win64}
  120. Result:=TFPUExceptionMask(dword(Get8087CW and $3F));
  121. {$endif win64}
  122. end;
  123. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  124. var
  125. CtlWord: Word;
  126. begin
  127. CtlWord:=Get8087CW;
  128. Set8087CW((CtlWord and $FFC0) or Byte(Longint(Mask)));
  129. SetSSECSR((GetSSECSR and $ffffe07f) or (dword(Mask) shl 7));
  130. softfloat_exception_mask:=dword(Mask);
  131. Result:=GetExceptionMask;
  132. end;
  133. procedure ClearExceptions(RaisePending: Boolean);assembler;
  134. asm
  135. cmpb $0,RaisePending
  136. je .Lclear
  137. fwait
  138. .Lclear:
  139. fnclex
  140. end;