math.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. begin
  32. mxcsr:=w;
  33. asm
  34. ldmxcsr w
  35. end;
  36. end;
  37. function GetSSECSR : dword;
  38. var
  39. _w : dword;
  40. begin
  41. asm
  42. stmxcsr _w
  43. end;
  44. result:=_w;
  45. end;
  46. {****************************************************************************
  47. EXTENDED data type routines
  48. ****************************************************************************}
  49. {$define FPC_SYSTEM_HAS_PI}
  50. function fpc_pi_real : ValReal;compilerproc;
  51. begin
  52. { Function is handled internal in the compiler }
  53. runerror(207);
  54. result:=0;
  55. end;
  56. {$define FPC_SYSTEM_HAS_ABS}
  57. function fpc_abs_real(d : ValReal) : ValReal;compilerproc;
  58. begin
  59. { Function is handled internal in the compiler }
  60. runerror(207);
  61. result:=0;
  62. end;
  63. {$define FPC_SYSTEM_HAS_SQR}
  64. function fpc_sqr_real(d : ValReal) : ValReal;compilerproc;
  65. begin
  66. { Function is handled internal in the compiler }
  67. runerror(207);
  68. result:=0;
  69. end;
  70. {$define FPC_SYSTEM_HAS_SQRT}
  71. function fpc_sqrt_real(d : ValReal) : ValReal;compilerproc;
  72. begin
  73. { Function is handled internal in the compiler }
  74. runerror(207);
  75. result:=0;
  76. end;
  77. {$define FPC_SYSTEM_HAS_ARCTAN}
  78. function fpc_arctan_real(d : ValReal) : ValReal;compilerproc;
  79. begin
  80. { Function is handled internal in the compiler }
  81. runerror(207);
  82. result:=0;
  83. end;
  84. {$define FPC_SYSTEM_HAS_LN}
  85. function fpc_ln_real(d : ValReal) : ValReal;compilerproc;
  86. begin
  87. { Function is handled internal in the compiler }
  88. runerror(207);
  89. result:=0;
  90. end;
  91. {$define FPC_SYSTEM_HAS_SIN}
  92. function fpc_sin_real(d : ValReal) : ValReal;compilerproc;
  93. begin
  94. { Function is handled internal in the compiler }
  95. runerror(207);
  96. result:=0;
  97. end;
  98. {$define FPC_SYSTEM_HAS_COS}
  99. function fpc_cos_real(d : ValReal) : ValReal;compilerproc;
  100. begin
  101. { Function is handled internal in the compiler }
  102. runerror(207);
  103. result:=0;
  104. end;
  105. {$define FPC_SYSTEM_HAS_EXP}
  106. function fpc_exp_real(d : ValReal) : ValReal;assembler;compilerproc;
  107. var
  108. cw1,cw2: word;
  109. asm
  110. // comes from DJ GPP
  111. fldt d
  112. fldl2e
  113. fmulp %st,%st(1)
  114. fstcw CW1
  115. fstcw CW2
  116. fwait
  117. andw $0xf3ff,CW2
  118. orw $0x0400,CW2
  119. fldcw CW2
  120. fld %st(0)
  121. frndint
  122. fldcw CW1
  123. fxch %st(1)
  124. fsub %st(1),%st
  125. f2xm1
  126. fld1
  127. faddp %st,%st(1)
  128. fscale
  129. fstp %st(1)
  130. end;
  131. {$define FPC_SYSTEM_HAS_FRAC}
  132. function fpc_frac_real(d : ValReal) : ValReal;assembler;compilerproc;
  133. asm
  134. subl $4,%esp
  135. fnstcw (%esp)
  136. fwait
  137. movw (%esp),%cx
  138. orw $0x0f00,(%esp)
  139. fldcw (%esp)
  140. fldt d
  141. frndint
  142. fldt d
  143. fsub %st(1),%st
  144. fstp %st(1)
  145. movw %cx,(%esp)
  146. fldcw (%esp)
  147. end;
  148. {$define FPC_SYSTEM_HAS_INT}
  149. function fpc_int_real(d : ValReal) : ValReal;assembler;compilerproc;
  150. asm
  151. subl $4,%esp
  152. fnstcw (%esp)
  153. fwait
  154. movw (%esp),%cx
  155. orw $0x0f00,(%esp)
  156. fldcw (%esp)
  157. fwait
  158. fldt d
  159. frndint
  160. fwait
  161. movw %cx,(%esp)
  162. fldcw (%esp)
  163. end;
  164. {$define FPC_SYSTEM_HAS_TRUNC}
  165. function fpc_trunc_real(d : ValReal) : int64;assembler;compilerproc;
  166. asm
  167. subl $12,%esp
  168. fldt d
  169. fnstcw (%esp)
  170. movw (%esp),%cx
  171. orw $0x0f00,(%esp)
  172. fldcw (%esp)
  173. movw %cx,(%esp)
  174. fistpq 4(%esp)
  175. fldcw (%esp)
  176. fwait
  177. movl 4(%esp),%eax
  178. movl 8(%esp),%edx
  179. end;
  180. {$define FPC_SYSTEM_HAS_ROUND}
  181. { keep for bootstrapping with 2.0.x }
  182. function fpc_round_real(d : ValReal) : int64;compilerproc;assembler;
  183. var
  184. res : int64;
  185. asm
  186. fldt d
  187. fistpq res
  188. fwait
  189. movl res,%eax
  190. movl res+4,%edx
  191. end;
  192. {$define FPC_SYSTEM_HAS_POWER}
  193. function power(bas,expo : ValReal) : ValReal;
  194. begin
  195. if bas=0 then
  196. begin
  197. if expo<>0 then
  198. power:=0.0
  199. else
  200. HandleError(207);
  201. end
  202. else if expo=0 then
  203. power:=1
  204. else
  205. { bas < 0 is not allowed when doing roots }
  206. if (bas<0) and (frac(expo) <> 0) then
  207. handleerror(207)
  208. else
  209. begin
  210. power:=exp(ln(abs(bas))*expo);
  211. if (bas < 0) and
  212. odd(trunc(expo)) then
  213. begin
  214. power := -power;
  215. end;
  216. end;
  217. end;