mathu.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 : extended;out sinus,cosinus : extended);assembler;
  22. asm
  23. fldt theta
  24. fsincos
  25. fstpt (%edx)
  26. fstpt (%eax)
  27. fwait
  28. end;
  29. procedure sincos(theta : double;out sinus,cosinus : double);assembler;
  30. asm
  31. fldl theta
  32. fsincos
  33. fstpl (%edx)
  34. fstpl (%eax)
  35. fwait
  36. end;
  37. procedure sincos(theta : single;out sinus,cosinus : single);assembler;
  38. asm
  39. flds theta
  40. fsincos
  41. fstps (%edx)
  42. fstps (%eax)
  43. fwait
  44. end;
  45. {$define FPC_MATH_HAS_TAN}
  46. function tan(x : float) : float;assembler;
  47. asm
  48. fldt X
  49. fptan
  50. fstp %st
  51. fwait
  52. end;
  53. {$define FPC_MATH_HAS_COTAN}
  54. function cotan(x : float) : float;assembler;
  55. asm
  56. fldt X
  57. fptan
  58. fdivp %st,%st(1)
  59. fwait
  60. end;
  61. {$define FPC_MATH_HAS_LOG2}
  62. function log2(x : float) : float;assembler;
  63. asm
  64. fld1
  65. fldt x
  66. fyl2x
  67. fwait
  68. end;
  69. {$define FPC_MATH_HAS_DIVMOD}
  70. procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: Word);assembler;
  71. asm
  72. pushl %edi
  73. movzwl %dx,%edi
  74. cltd
  75. idiv %edi
  76. movw %ax,(%ecx)
  77. movl Remainder,%ecx
  78. movw %dx,(%ecx)
  79. popl %edi
  80. end;
  81. procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: SmallInt);assembler;
  82. asm
  83. pushl %edi
  84. movzwl %dx,%edi
  85. cltd
  86. idiv %edi
  87. movw %ax,(%ecx)
  88. movl Remainder,%ecx
  89. movw %dx,(%ecx)
  90. popl %edi
  91. end;
  92. procedure DivMod(Dividend: DWord; Divisor: DWord; var Result, Remainder: DWord);assembler;
  93. asm
  94. pushl %edi
  95. movl %edx,%edi
  96. xorl %edx,%edx
  97. div %edi
  98. movl %eax,(%ecx)
  99. movl Remainder,%ecx
  100. movl %edx,(%ecx)
  101. popl %edi
  102. end;
  103. procedure DivMod(Dividend: Integer; Divisor: Integer; var Result, Remainder: Integer);assembler;
  104. asm
  105. pushl %edi
  106. movl %edx,%edi
  107. cltd
  108. idiv %edi
  109. movl %eax,(%ecx)
  110. movl Remainder,%ecx
  111. movl %edx,(%ecx)
  112. popl %edi
  113. end;
  114. function GetRoundMode: TFPURoundingMode;
  115. begin
  116. Result := TFPURoundingMode((Get8087CW shr 10) and 3);
  117. end;
  118. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  119. var
  120. CtlWord: Word;
  121. begin
  122. CtlWord := Get8087CW;
  123. Set8087CW((CtlWord and $F3FF) or (Ord(RoundMode) shl 10));
  124. if has_sse_support then
  125. SetMXCSR((GetMXCSR and $ffff9fff) or (dword(RoundMode) shl 13));
  126. Result := TFPURoundingMode((CtlWord shr 10) and 3);
  127. end;
  128. function GetPrecisionMode: TFPUPrecisionMode;
  129. begin
  130. Result := TFPUPrecisionMode((Get8087CW shr 8) and 3);
  131. end;
  132. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  133. var
  134. CtlWord: Word;
  135. begin
  136. CtlWord := Get8087CW;
  137. Set8087CW((CtlWord and $FCFF) or (Ord(Precision) shl 8));
  138. Result := TFPUPrecisionMode((CtlWord shr 8) and 3);
  139. end;
  140. function GetExceptionMask: TFPUExceptionMask;
  141. begin
  142. Result := TFPUExceptionMask(Longint(Get8087CW and $3F));
  143. end;
  144. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  145. var
  146. CtlWord: Word;
  147. begin
  148. CtlWord := Get8087CW;
  149. Set8087CW( (CtlWord and $FFC0) or Byte(Longint(Mask)) );
  150. if has_sse_support then
  151. SetMXCSR((GetMXCSR and $ffffe07f) or (dword(Mask) shl 7));
  152. Result := TFPUExceptionMask(Longint(CtlWord and $3F));
  153. end;
  154. procedure ClearExceptions(RaisePending: Boolean);assembler;
  155. asm
  156. cmpb $0,RaisePending
  157. je .Lclear
  158. fwait
  159. .Lclear:
  160. fnclex
  161. end;