mathu.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2003 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 ATT}
  12. {$define FPC_MATH_HAS_ARCTAN2}
  13. function arctan2(y,x : float) : float;assembler;
  14. asm
  15. fldt y
  16. fldt x
  17. fpatan
  18. fwait
  19. end;
  20. {$define FPC_MATH_HAS_SINCOS}
  21. procedure sincos(theta : float;out sinus,cosinus : float);assembler;
  22. asm
  23. fldt theta
  24. fsincos
  25. fstpt (%edx)
  26. fstpt (%eax)
  27. fwait
  28. end;
  29. {$define FPC_MATH_HAS_TAN}
  30. function tan(x : float) : float;assembler;
  31. asm
  32. fldt X
  33. fptan
  34. fstp %st
  35. fwait
  36. end;
  37. {$define FPC_MATH_HAS_COTAN}
  38. function cotan(x : float) : float;assembler;
  39. asm
  40. fldt X
  41. fptan
  42. fdivp %st,%st(1)
  43. fwait
  44. end;
  45. {$define FPC_MATH_HAS_DIVMOD}
  46. procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: Word);assembler;
  47. asm
  48. pushl %edi
  49. movzwl %dx,%edi
  50. cltd
  51. idiv %edi
  52. movw %ax,(%ecx)
  53. movl Remainder,%ecx
  54. movw %dx,(%ecx)
  55. popl %edi
  56. end;
  57. procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: SmallInt);assembler;
  58. asm
  59. pushl %edi
  60. movzwl %dx,%edi
  61. cltd
  62. idiv %edi
  63. movw %ax,(%ecx)
  64. movl Remainder,%ecx
  65. movw %dx,(%ecx)
  66. popl %edi
  67. end;
  68. procedure DivMod(Dividend: DWord; Divisor: DWord; var Result, Remainder: DWord);assembler;
  69. asm
  70. pushl %edi
  71. movl %edx,%edi
  72. xorl %edx,%edx
  73. div %edi
  74. movl %eax,(%ecx)
  75. movl Remainder,%ecx
  76. movl %edx,(%ecx)
  77. popl %edi
  78. end;
  79. procedure DivMod(Dividend: Integer; Divisor: Integer; var Result, Remainder: Integer);assembler;
  80. asm
  81. pushl %edi
  82. movl %edx,%edi
  83. cltd
  84. idiv %edi
  85. movl %eax,(%ecx)
  86. movl Remainder,%ecx
  87. movl %edx,(%ecx)
  88. popl %edi
  89. end;
  90. function GetRoundMode: TFPURoundingMode;
  91. begin
  92. Result := TFPURoundingMode((Get8087CW shr 10) and 3);
  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. if has_sse_support then
  101. SetSSECSR((GetSSECSR and $ffff9fff) or (dword(RoundMode) shl 13));
  102. Result := TFPURoundingMode((CtlWord shr 10) and 3);
  103. end;
  104. function GetPrecisionMode: TFPUPrecisionMode;
  105. begin
  106. Result := TFPUPrecisionMode((Get8087CW shr 8) and 3);
  107. end;
  108. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  109. var
  110. CtlWord: Word;
  111. begin
  112. CtlWord := Get8087CW;
  113. Set8087CW((CtlWord and $FCFF) or (Ord(Precision) shl 8));
  114. Result := TFPUPrecisionMode((CtlWord shr 8) and 3);
  115. end;
  116. function GetExceptionMask: TFPUExceptionMask;
  117. begin
  118. Result := TFPUExceptionMask(Longint(Get8087CW and $3F));
  119. end;
  120. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  121. var
  122. CtlWord: Word;
  123. begin
  124. CtlWord := Get8087CW;
  125. Set8087CW( (CtlWord and $FFC0) or Byte(Longint(Mask)) );
  126. if has_sse_support then
  127. SetSSECSR((GetSSECSR and $ffffe07f) or (dword(Mask) shl 7));
  128. softfloat_exception_mask:=dword(Mask);
  129. Result := TFPUExceptionMask(Longint(CtlWord and $3F));
  130. end;
  131. procedure ClearExceptions(RaisePending: Boolean);assembler;
  132. asm
  133. cmpb $0,RaisePending
  134. je .Lclear
  135. fwait
  136. .Lclear:
  137. fnclex
  138. end;