math.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2001 by the Free Pascal development team
  5. Implementation of mathematical routines (for extended type)
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {****************************************************************************
  13. FPU Control word
  14. ****************************************************************************}
  15. procedure Set8087CW(cw:word);assembler;
  16. asm
  17. movw cw,%ax
  18. movw %ax,default8087cw
  19. fnclex
  20. fldcw default8087cw
  21. end;
  22. function Get8087CW:word;assembler;
  23. asm
  24. pushl $0
  25. fnstcw (%rsp)
  26. popl %eax
  27. end;
  28. {****************************************************************************
  29. EXTENDED data type routines
  30. ****************************************************************************}
  31. {$define FPC_SYSTEM_HAS_PI}
  32. function pi : extended;[internproc:in_pi];
  33. {$define FPC_SYSTEM_HAS_ABS}
  34. function abs(d : extended) : extended;[internproc:in_abs_extended];
  35. {$define FPC_SYSTEM_HAS_SQR}
  36. function sqr(d : extended) : extended;[internproc:in_sqr_extended];
  37. {$define FPC_SYSTEM_HAS_SQRT}
  38. function sqrt(d : extended) : extended;[internproc:in_sqrt_extended];
  39. {$define FPC_SYSTEM_HAS_ARCTAN}
  40. function arctan(d : extended) : extended;[internproc:in_arctan_extended];
  41. {$define FPC_SYSTEM_HAS_LN}
  42. function ln(d : extended) : extended;[internproc:in_ln_extended];
  43. {$define FPC_SYSTEM_HAS_SIN}
  44. function sin(d : extended) : extended;[internproc:in_sin_extended];
  45. {$define FPC_SYSTEM_HAS_COS}
  46. function cos(d : extended) : extended;[internproc:in_cos_extended];
  47. {$define FPC_SYSTEM_HAS_EXP}
  48. function exp(d : extended) : extended;assembler;[internconst:in_const_exp];
  49. asm
  50. // comes from DJ GPP
  51. fldt d
  52. fldl2e
  53. fmulp %st,%st(1)
  54. fstcw .LCW1
  55. fstcw .LCW2
  56. andw $0xf3ff,.LCW2
  57. orw $0x0400,.LCW2
  58. fldcw .LCW2
  59. fld %st(0)
  60. frndint
  61. fldcw .LCW1
  62. fxch %st(1)
  63. fsub %st(1),%st
  64. f2xm1
  65. fld1
  66. faddp %st,%st(1)
  67. fscale
  68. fstp %st(1)
  69. jmp .LCW3
  70. // store some help data in the data segment
  71. .data
  72. .LCW1:
  73. .word 0
  74. .LCW2:
  75. .word 0
  76. .text
  77. .LCW3:
  78. end;
  79. {$define FPC_SYSTEM_HAS_FRAC}
  80. function frac(d : extended) : extended;assembler;[internconst:in_const_frac];
  81. asm
  82. subl $16,%esp
  83. fnstcw -4(%ebp)
  84. fwait
  85. movw -4(%ebp),%cx
  86. orw $0x0c3f,%cx
  87. movw %cx,-8(%ebp)
  88. fldcw -8(%ebp)
  89. fwait
  90. fldt d
  91. frndint
  92. fldt d
  93. fsub %st(1),%st
  94. fstp %st(1)
  95. fclex
  96. fldcw -4(%ebp)
  97. end ['ECX'];
  98. {$define FPC_SYSTEM_HAS_INT}
  99. function int(d : extended) : extended;assembler;[internconst:in_const_int];
  100. asm
  101. subl $16,%rsp
  102. fnstcw -4(%rbp)
  103. fwait
  104. movw -4(%rbp),%cx
  105. orw $0x0c3f,%cx
  106. movw %cx,-8(rbp)
  107. fldcw -8(%rbp)
  108. fwait
  109. fldt d
  110. frndint
  111. fclex
  112. fldcw -4(%rbp)
  113. end ['ECX'];
  114. {$define FPC_SYSTEM_HAS_TRUNC}
  115. function trunc(d : extended) : int64;assembler;[internconst:in_const_trunc];
  116. var
  117. oldcw,
  118. newcw : word;
  119. res : int64;
  120. asm
  121. fnstcw oldcw
  122. fwait
  123. movw oldcw,%cx
  124. orw $0x0c3f,%cx
  125. movw %cx,newcw
  126. fldcw newcw
  127. fwait
  128. fldt d
  129. fistpq res
  130. movl res,%eax
  131. movl res+4,%edx
  132. fldcw oldcw
  133. end ['EAX','ECX','EDX'];
  134. {$define FPC_SYSTEM_HAS_ROUND}
  135. {$ifdef hascompilerproc}
  136. function round(d : extended) : int64;[internconst:in_const_round, external name 'FPC_ROUND'];
  137. function fpc_round(d : extended) : int64;assembler;[public, alias:'FPC_ROUND'];{$ifdef hascompilerproc}compilerproc;{$endif hascompilerproc}
  138. {$else}
  139. function round(d : extended) : int64;assembler;[internconst:in_const_round];
  140. {$endif hascompilerproc}
  141. var
  142. oldcw,
  143. newcw : word;
  144. res : int64;
  145. asm
  146. fnstcw oldcw
  147. fwait
  148. movw $0x1372,newcw
  149. fldcw newcw
  150. fwait
  151. fldt d
  152. fistpq res
  153. movl res,%eax
  154. movl res+4,%edx
  155. fldcw oldcw
  156. end ['EAX','EDX'];
  157. {$define FPC_SYSTEM_HAS_POWER}
  158. function power(bas,expo : extended) : extended;
  159. begin
  160. if bas=0 then
  161. begin
  162. if expo<>0 then
  163. power:=0.0
  164. else
  165. HandleError(207);
  166. end
  167. else if expo=0 then
  168. power:=1
  169. else
  170. { bas < 0 is not allowed }
  171. if bas<0 then
  172. handleerror(207)
  173. else
  174. power:=exp(ln(bas)*expo);
  175. end;
  176. {
  177. $Log$
  178. Revision 1.1 2003-04-30 22:11:06 florian
  179. + for a lot of x86-64 dependend files mostly dummies added
  180. }