math.inc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. {
  2. Implementation of mathematical routines for x86_64
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2005 by 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. label
  12. FPC_ABSMASK_DOUBLE,
  13. FPC_ABSMASK_SINGLE;
  14. procedure dummyproc;assembler;nostackframe;
  15. asm
  16. .data
  17. .balign 16
  18. .globl FPC_ABSMASK_SINGLE
  19. FPC_ABSMASK_SINGLE:
  20. .quad 0x7FFFFFFF7FFFFFFF
  21. .quad 0x7FFFFFFF7FFFFFFF
  22. .globl FPC_ABSMASK_DOUBLE
  23. FPC_ABSMASK_DOUBLE:
  24. .quad 0x7FFFFFFFFFFFFFFF
  25. .quad 0x7FFFFFFFFFFFFFFF
  26. .text
  27. end;
  28. {****************************************************************************
  29. FPU Control word
  30. ****************************************************************************}
  31. procedure Set8087CW(cw:word);
  32. begin
  33. default8087cw:=cw;
  34. asm
  35. fnclex
  36. fldcw cw
  37. end;
  38. end;
  39. function Get8087CW:word;assembler;
  40. var
  41. tmp: word;
  42. asm
  43. fnstcw tmp
  44. movw tmp,%ax
  45. andl $0xffff,%eax { clears bits 32-63 }
  46. end;
  47. procedure SetSSECSR(w : dword);
  48. begin
  49. mxcsr:=w;
  50. asm
  51. ldmxcsr w
  52. end;
  53. end;
  54. function GetSSECSR : dword;assembler;
  55. var
  56. _w : dword;
  57. asm
  58. stmxcsr _w
  59. movl _w,%eax
  60. end;
  61. {****************************************************************************
  62. EXTENDED data type routines
  63. ****************************************************************************}
  64. {$ifndef FPC_SYSTEM_HAS_PI}
  65. {$define FPC_SYSTEM_HAS_PI}
  66. function fpc_pi_real : ValReal;compilerproc;
  67. begin
  68. { Function is handled internal in the compiler }
  69. runerror(207);
  70. result:=0;
  71. end;
  72. {$endif FPC_SYSTEM_HAS_PI}
  73. {$ifndef FPC_SYSTEM_HAS_ABS}
  74. {$define FPC_SYSTEM_HAS_ABS}
  75. function fpc_abs_real(d : ValReal) : ValReal;compilerproc;
  76. begin
  77. { Function is handled internal in the compiler }
  78. runerror(207);
  79. result:=0;
  80. end;
  81. {$endif FPC_SYSTEM_HAS_ABS}
  82. {$ifndef FPC_SYSTEM_HAS_SQR}
  83. {$define FPC_SYSTEM_HAS_SQR}
  84. function fpc_sqr_real(d : ValReal) : ValReal;compilerproc;
  85. begin
  86. { Function is handled internal in the compiler }
  87. runerror(207);
  88. result:=0;
  89. end;
  90. {$endif FPC_SYSTEM_HAS_SQR}
  91. {$ifndef FPC_SYSTEM_HAS_SQRT}
  92. {$define FPC_SYSTEM_HAS_SQRT}
  93. function fpc_sqrt_real(d : ValReal) : ValReal;compilerproc;
  94. begin
  95. { Function is handled internal in the compiler }
  96. runerror(207);
  97. result:=0;
  98. end;
  99. {$endif FPC_SYSTEM_HAS_SQRT}
  100. {$ifndef FPC_SYSTEM_HAS_ARCTAN}
  101. {$define FPC_SYSTEM_HAS_ARCTAN}
  102. function fpc_arctan_real(d : ValReal) : ValReal;compilerproc;
  103. begin
  104. { Function is handled internal in the compiler }
  105. runerror(207);
  106. result:=0;
  107. end;
  108. {$endif FPC_SYSTEM_HAS_ARCTAN}
  109. {$ifndef FPC_SYSTEM_HAS_LN}
  110. {$define FPC_SYSTEM_HAS_LN}
  111. function fpc_ln_real(d : ValReal) : ValReal;compilerproc;
  112. begin
  113. { Function is handled internal in the compiler }
  114. runerror(207);
  115. result:=0;
  116. end;
  117. {$endif FPC_SYSTEM_HAS_LN}
  118. {$ifndef FPC_SYSTEM_HAS_SIN}
  119. {$define FPC_SYSTEM_HAS_SIN}
  120. function fpc_sin_real(d : ValReal) : ValReal;compilerproc;
  121. begin
  122. { Function is handled internal in the compiler }
  123. runerror(207);
  124. result:=0;
  125. end;
  126. {$endif FPC_SYSTEM_HAS_SIN}
  127. {$ifndef FPC_SYSTEM_HAS_COS}
  128. {$define FPC_SYSTEM_HAS_COS}
  129. function fpc_cos_real(d : ValReal) : ValReal;compilerproc;
  130. begin
  131. { Function is handled internal in the compiler }
  132. runerror(207);
  133. result:=0;
  134. end;
  135. {$endif FPC_SYSTEM_HAS_COS}
  136. {$ifdef FPC_HAS_TYPE_EXTENDED}
  137. {$ifndef FPC_SYSTEM_HAS_EXP}
  138. {$define FPC_SYSTEM_HAS_EXP}
  139. function fpc_exp_real(d : ValReal) : ValReal;assembler;compilerproc;
  140. var
  141. oldcw,newcw: word;
  142. asm
  143. // comes from DJ GPP
  144. fldt d
  145. fldl2e
  146. fmulp %st,%st(1)
  147. fstcw oldcw
  148. fstcw newcw
  149. andw $0xf3ff,newcw
  150. orw $0x0400,newcw
  151. fldcw newcw
  152. fld %st(0)
  153. frndint
  154. fldcw oldcw
  155. fxch %st(1)
  156. fsub %st(1),%st
  157. f2xm1
  158. fld1
  159. faddp %st,%st(1)
  160. fscale
  161. fstp %st(1)
  162. end;
  163. {$endif FPC_SYSTEM_HAS_EXP}
  164. {$ifndef FPC_SYSTEM_HAS_FRAC}
  165. {$define FPC_SYSTEM_HAS_FRAC}
  166. function fpc_frac_real(d : ValReal) : ValReal;assembler;compilerproc;
  167. var
  168. oldcw,newcw: word;
  169. asm
  170. fnstcw oldcw
  171. fwait
  172. movw oldcw,%cx
  173. orw $0x0c3f,%cx
  174. movw %cx,newcw
  175. fldcw newcw
  176. fwait
  177. fldt d
  178. frndint
  179. fldt d
  180. fsub %st(1),%st
  181. fstp %st(1)
  182. fnclex
  183. fldcw oldcw
  184. end;
  185. {$endif FPC_SYSTEM_HAS_FRAC}
  186. {$ifndef FPC_SYSTEM_HAS_INT}
  187. {$define FPC_SYSTEM_HAS_INT}
  188. function fpc_int_real(d : ValReal) : ValReal;assembler;compilerproc;
  189. var
  190. oldcw,newcw: word;
  191. asm
  192. fnstcw oldcw
  193. fwait
  194. movw oldcw,%cx
  195. orw $0x0c3f,%cx
  196. movw %cx,newcw
  197. fldcw newcw
  198. fwait
  199. fldt d
  200. frndint
  201. fwait
  202. fldcw oldcw
  203. end;
  204. {$endif FPC_SYSTEM_HAS_INT}
  205. {$ifndef FPC_SYSTEM_HAS_TRUNC}
  206. {$define FPC_SYSTEM_HAS_TRUNC}
  207. function fpc_trunc_real(d : ValReal) : int64;assembler;compilerproc;
  208. var
  209. oldcw,
  210. newcw : word;
  211. res : int64;
  212. asm
  213. fnstcw oldcw
  214. fwait
  215. movw oldcw,%cx
  216. orw $0x0c3f,%cx
  217. movw %cx,newcw
  218. fldcw newcw
  219. fldt d
  220. fistpq res
  221. fwait
  222. movq res,%rax
  223. fldcw oldcw
  224. end;
  225. {$endif FPC_SYSTEM_HAS_TRUNC}
  226. {$ifndef FPC_SYSTEM_HAS_ROUND}
  227. {$define FPC_SYSTEM_HAS_ROUND}
  228. function fpc_round_real(d : ValReal) : int64;assembler;compilerproc;
  229. var
  230. res : int64;
  231. asm
  232. fldt d
  233. fistpq res
  234. fwait
  235. movq res,%rax
  236. end;
  237. {$endif FPC_SYSTEM_HAS_ROUND}
  238. {$ifndef FPC_SYSTEM_HAS_POWER}
  239. {$define FPC_SYSTEM_HAS_POWER}
  240. function power(bas,expo : extended) : extended;
  241. begin
  242. if bas=0 then
  243. begin
  244. if expo<>0 then
  245. power:=0.0
  246. else
  247. HandleError(207);
  248. end
  249. else if expo=0 then
  250. power:=1
  251. else
  252. { bas < 0 is not allowed }
  253. if bas<0 then
  254. handleerror(207)
  255. else
  256. power:=exp(ln(bas)*expo);
  257. end;
  258. {$endif FPC_SYSTEM_HAS_POWER}
  259. {$endif FPC_HAS_TYPE_EXTENDED}