math.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2001 by the Free Pascal development team
  4. Implementation of mathematical routines (for extended type)
  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. {****************************************************************************
  12. FPU Control word
  13. ****************************************************************************}
  14. procedure Set8087CW(cw:word);assembler;
  15. asm
  16. movw cw,%ax
  17. {$ifdef FPC_PIC}
  18. movq default8087cw@GOTPCREL(%rip),%rax
  19. movw %ax,(%rax)
  20. fnclex
  21. fldcw (%rax)
  22. {$else FPC_PIC}
  23. movw %ax,default8087cw
  24. fnclex
  25. fldcw default8087cw
  26. {$endif FPC_PIC}
  27. end;
  28. function Get8087CW:word;assembler;
  29. asm
  30. pushq $0
  31. fnstcw (%rsp)
  32. popq %rax
  33. end;
  34. {****************************************************************************
  35. EXTENDED data type routines
  36. ****************************************************************************}
  37. {$define FPC_SYSTEM_HAS_PI}
  38. function fpc_pi_real : ValReal;compilerproc;
  39. begin
  40. { Function is handled internal in the compiler }
  41. runerror(207);
  42. result:=0;
  43. end;
  44. {$define FPC_SYSTEM_HAS_ABS}
  45. function fpc_abs_real(d : ValReal) : ValReal;compilerproc;
  46. begin
  47. { Function is handled internal in the compiler }
  48. runerror(207);
  49. result:=0;
  50. end;
  51. {$define FPC_SYSTEM_HAS_SQR}
  52. function fpc_sqr_real(d : ValReal) : ValReal;compilerproc;
  53. begin
  54. { Function is handled internal in the compiler }
  55. runerror(207);
  56. result:=0;
  57. end;
  58. {$define FPC_SYSTEM_HAS_SQRT}
  59. function fpc_sqrt_real(d : ValReal) : ValReal;compilerproc;
  60. begin
  61. { Function is handled internal in the compiler }
  62. runerror(207);
  63. result:=0;
  64. end;
  65. {$define FPC_SYSTEM_HAS_ARCTAN}
  66. function fpc_arctan_real(d : ValReal) : ValReal;compilerproc;
  67. begin
  68. { Function is handled internal in the compiler }
  69. runerror(207);
  70. result:=0;
  71. end;
  72. {$define FPC_SYSTEM_HAS_LN}
  73. function fpc_ln_real(d : ValReal) : ValReal;compilerproc;
  74. begin
  75. { Function is handled internal in the compiler }
  76. runerror(207);
  77. result:=0;
  78. end;
  79. {$define FPC_SYSTEM_HAS_SIN}
  80. function fpc_sin_real(d : ValReal) : ValReal;compilerproc;
  81. begin
  82. { Function is handled internal in the compiler }
  83. runerror(207);
  84. result:=0;
  85. end;
  86. {$define FPC_SYSTEM_HAS_COS}
  87. function fpc_cos_real(d : ValReal) : ValReal;compilerproc;
  88. begin
  89. { Function is handled internal in the compiler }
  90. runerror(207);
  91. result:=0;
  92. end;
  93. {$define FPC_SYSTEM_HAS_EXP}
  94. function fpc_exp_real(d : ValReal) : ValReal;assembler;compilerproc;
  95. asm
  96. subq $16,%rsp
  97. // comes from DJ GPP
  98. fldt d
  99. fldl2e
  100. fmulp %st,%st(1)
  101. fstcw -2(%rbp)
  102. fstcw -4(%rbp)
  103. andw $0xf3ff,-4(%rbp)
  104. orw $0x0400,-4(%rbp)
  105. fldcw -4(%rbp)
  106. fld %st(0)
  107. frndint
  108. fldcw -2(%rbp)
  109. fxch %st(1)
  110. fsub %st(1),%st
  111. f2xm1
  112. fld1
  113. faddp %st,%st(1)
  114. fscale
  115. fstp %st(1)
  116. end;
  117. {$define FPC_SYSTEM_HAS_FRAC}
  118. function fpc_frac_real(d : ValReal) : ValReal;assembler;compilerproc;
  119. asm
  120. subq $16,%rsp
  121. fnstcw -4(%rbp)
  122. fwait
  123. movw -4(%rbp),%cx
  124. orw $0x0c3f,%cx
  125. movw %cx,-8(%rbp)
  126. fldcw -8(%rbp)
  127. fwait
  128. fldt d
  129. frndint
  130. fldt d
  131. fsub %st(1),%st
  132. fstp %st(1)
  133. fnclex
  134. fldcw -4(%rbp)
  135. end;
  136. {$define FPC_SYSTEM_HAS_INT}
  137. function fpc_int_real(d : ValReal) : ValReal;assembler;compilerproc;
  138. asm
  139. subq $16,%rsp
  140. fnstcw -4(%rbp)
  141. fwait
  142. movw -4(%rbp),%cx
  143. orw $0x0c3f,%cx
  144. movw %cx,-8(%rbp)
  145. fldcw -8(%rbp)
  146. fwait
  147. fldt d
  148. frndint
  149. fwait
  150. fldcw -4(%rbp)
  151. end;
  152. {$define FPC_SYSTEM_HAS_TRUNC}
  153. function fpc_trunc_real(d : ValReal) : int64;assembler;compilerproc;
  154. var
  155. oldcw,
  156. newcw : word;
  157. res : int64;
  158. asm
  159. fnstcw oldcw
  160. fwait
  161. movw oldcw,%cx
  162. orw $0x0c3f,%cx
  163. movw %cx,newcw
  164. fldcw newcw
  165. fldt d
  166. fistpq res
  167. fwait
  168. movq res,%rax
  169. fldcw oldcw
  170. end;
  171. {$define FPC_SYSTEM_HAS_ROUND}
  172. function fpc_round_real(d : ValReal) : int64;assembler;compilerproc;
  173. var
  174. res : int64;
  175. asm
  176. fldt d
  177. fistpq res
  178. fwait
  179. movq res,%rax
  180. end;
  181. {$define FPC_SYSTEM_HAS_POWER}
  182. function power(bas,expo : extended) : extended;
  183. begin
  184. if bas=0 then
  185. begin
  186. if expo<>0 then
  187. power:=0.0
  188. else
  189. HandleError(207);
  190. end
  191. else if expo=0 then
  192. power:=1
  193. else
  194. { bas < 0 is not allowed }
  195. if bas<0 then
  196. handleerror(207)
  197. else
  198. power:=exp(ln(bas)*expo);
  199. end;