math.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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);
  15. begin
  16. { pic-safe ; cw will not be a regvar because it's accessed from }
  17. { assembler }
  18. default8087cw:=cw;
  19. asm
  20. fnclex
  21. fldcw cw
  22. end;
  23. end;
  24. function Get8087CW:word;assembler;
  25. asm
  26. pushl $0
  27. fnstcw (%esp)
  28. popl %eax
  29. end;
  30. procedure SetSSECSR(w : dword);
  31. var
  32. _w : dword;
  33. begin
  34. _w:=w;
  35. asm
  36. ldmxcsr _w
  37. end;
  38. end;
  39. function GetSSECSR : dword;
  40. var
  41. _w : dword;
  42. begin
  43. asm
  44. stmxcsr _w
  45. end;
  46. result:=_w;
  47. end;
  48. {****************************************************************************
  49. EXTENDED data type routines
  50. ****************************************************************************}
  51. {$define FPC_SYSTEM_HAS_PI}
  52. function fpc_pi_real : 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_ABS}
  59. function fpc_abs_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_SQR}
  66. function fpc_sqr_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_SQRT}
  73. function fpc_sqrt_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_ARCTAN}
  80. function fpc_arctan_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_LN}
  87. function fpc_ln_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_SIN}
  94. function fpc_sin_real(d : ValReal) : ValReal;compilerproc;
  95. begin
  96. { Function is handled internal in the compiler }
  97. runerror(207);
  98. result:=0;
  99. end;
  100. {$define FPC_SYSTEM_HAS_COS}
  101. function fpc_cos_real(d : ValReal) : ValReal;compilerproc;
  102. begin
  103. { Function is handled internal in the compiler }
  104. runerror(207);
  105. result:=0;
  106. end;
  107. {$define FPC_SYSTEM_HAS_EXP}
  108. function fpc_exp_real(d : ValReal) : ValReal;assembler;compilerproc;
  109. var
  110. cw1,cw2: word;
  111. asm
  112. // comes from DJ GPP
  113. fldt d
  114. fldl2e
  115. fmulp %st,%st(1)
  116. fstcw CW1
  117. fstcw CW2
  118. fwait
  119. andw $0xf3ff,CW2
  120. orw $0x0400,CW2
  121. fldcw CW2
  122. fld %st(0)
  123. frndint
  124. fldcw CW1
  125. fxch %st(1)
  126. fsub %st(1),%st
  127. f2xm1
  128. fld1
  129. faddp %st,%st(1)
  130. fscale
  131. fstp %st(1)
  132. end;
  133. {$define FPC_SYSTEM_HAS_FRAC}
  134. function fpc_frac_real(d : ValReal) : ValReal;assembler;compilerproc;
  135. asm
  136. subl $4,%esp
  137. fnstcw (%esp)
  138. fwait
  139. movw (%esp),%cx
  140. orw $0x0f00,(%esp)
  141. fldcw (%esp)
  142. fldt d
  143. frndint
  144. fldt d
  145. fsub %st(1),%st
  146. fstp %st(1)
  147. movw %cx,(%esp)
  148. fldcw (%esp)
  149. end;
  150. {$define FPC_SYSTEM_HAS_INT}
  151. function fpc_int_real(d : ValReal) : ValReal;assembler;compilerproc;
  152. asm
  153. subl $4,%esp
  154. fnstcw (%esp)
  155. fwait
  156. movw (%esp),%cx
  157. orw $0x0f00,(%esp)
  158. fldcw (%esp)
  159. fwait
  160. fldt d
  161. frndint
  162. fwait
  163. movw %cx,(%esp)
  164. fldcw (%esp)
  165. end;
  166. {$define FPC_SYSTEM_HAS_TRUNC}
  167. function fpc_trunc_real(d : ValReal) : int64;assembler;compilerproc;
  168. asm
  169. subl $12,%esp
  170. fldt d
  171. fnstcw (%esp)
  172. movw (%esp),%cx
  173. orw $0x0f00,(%esp)
  174. fldcw (%esp)
  175. movw %cx,(%esp)
  176. fistpq 4(%esp)
  177. fldcw (%esp)
  178. fwait
  179. movl 4(%esp),%eax
  180. movl 8(%esp),%edx
  181. end;
  182. {$define FPC_SYSTEM_HAS_ROUND}
  183. { keep for bootstrapping with 2.0.x }
  184. function fpc_round_real(d : ValReal) : int64;compilerproc;assembler;
  185. var
  186. res : int64;
  187. asm
  188. fldt d
  189. fistpq res
  190. fwait
  191. movl res,%eax
  192. movl res+4,%edx
  193. end;
  194. {$define FPC_SYSTEM_HAS_POWER}
  195. function power(bas,expo : ValReal) : ValReal;
  196. begin
  197. if bas=0 then
  198. begin
  199. if expo<>0 then
  200. power:=0.0
  201. else
  202. HandleError(207);
  203. end
  204. else if expo=0 then
  205. power:=1
  206. else
  207. { bas < 0 is not allowed when doing roots }
  208. if (bas<0) and (frac(expo) <> 0) then
  209. handleerror(207)
  210. else
  211. begin
  212. power:=exp(ln(abs(bas))*expo);
  213. if (bas < 0) and
  214. odd(trunc(expo)) then
  215. begin
  216. power := -power;
  217. end;
  218. end;
  219. end;