math.inc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2000 by Jonas Maebe and other members of the
  5. Free Pascal development team
  6. Implementation of mathamatical Routines (only for real)
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {****************************************************************************
  14. Int to real helpers
  15. ****************************************************************************}
  16. const
  17. longint_to_real_helper: int64 = $4330000080000000;
  18. cardinal_to_real_helper: int64 = $430000000000000;
  19. int_to_real_factor: double = double(high(cardinal))+1.0;
  20. {****************************************************************************
  21. EXTENDED data type routines
  22. ****************************************************************************}
  23. {$define FPC_SYSTEM_HAS_PI}
  24. function pi : double;[internproc:in_pi];
  25. {$define FPC_SYSTEM_HAS_ABS}
  26. function abs(d : extended) : extended;[internproc:in_abs_extended];
  27. {$define FPC_SYSTEM_HAS_SQR}
  28. function sqr(d : extended) : extended;[internproc:in_sqr_extended];
  29. {$define FPC_SYSTEM_HAS_SQRT}
  30. function sqrt(d : extended) : extended;[internproc:in_sqrt_extended];
  31. {
  32. function arctan(d : extended) : extended;[internconst:in_arctan_extended];
  33. begin
  34. runerror(207);
  35. end;
  36. function ln(d : extended) : extended;[internconst:in_ln_extended];
  37. begin
  38. runerror(207);
  39. end;
  40. function sin(d : extended) : extended;[internconst: in_sin_extended];
  41. begin
  42. runerror(207);
  43. end;
  44. function cos(d : extended) : extended;[internconst:in_cos_extended];
  45. begin
  46. runerror(207);
  47. end;
  48. function exp(d : extended) : extended;[internconst:in_const_exp];
  49. begin
  50. runerror(207);
  51. end;
  52. function frac(d : extended) : extended;[internconst:in_const_frac];
  53. begin
  54. runerror(207);
  55. end;
  56. }
  57. {$define FPC_SYSTEM_HAS_INT}
  58. {$warning FIX ME}
  59. function int(d : extended) : extended;[internconst:in_const_int];
  60. begin
  61. runerror(207);
  62. end;
  63. {$define FPC_SYSTEM_HAS_TRUNC}
  64. {$warning FIX ME}
  65. function trunc(d : extended) : int64;{assembler;}[internconst:in_const_trunc];
  66. { input: d in fr1 }
  67. { output: result in r3 }
  68. {assembler;}
  69. var
  70. temp: packed record
  71. case byte of
  72. 0: (l1,l2: longint);
  73. 1: (d: double);
  74. end;
  75. begin{asm}
  76. { fctiwz f1,f1
  77. stfd f1,temp
  78. lwz r3,temp
  79. lwz r4,4+temp}
  80. end{ ['R3','F1']};
  81. {$define FPC_SYSTEM_HAS_ROUND}
  82. {$ifdef hascompilerproc}
  83. function round(d : extended) : int64;[internconst:in_const_round, external name 'FPC_ROUND'];
  84. function fpc_round(d : extended) : int64;[public, alias:'FPC_ROUND'];{$ifdef hascompilerproc}compilerproc;{$endif hascompilerproc}
  85. {$else}
  86. function round(d : extended) : int64;[internconst:in_const_round];
  87. {$endif hascompilerproc}
  88. { input: d in fr1 }
  89. { output: result in r3 }
  90. {assembler;}
  91. var
  92. temp : packed record
  93. case byte of
  94. 0: (l1,l2: longint);
  95. 1: (d: double);
  96. end;
  97. begin{asm}
  98. { fctiw f1,f1
  99. stfd f1,temp
  100. lwz r3,temp
  101. lwz r4,4+temp}
  102. end{ ['R3','F1']};
  103. {$define FPC_SYSTEM_HAS_POWER}
  104. function power(bas,expo : extended) : extended;
  105. begin
  106. if bas=0 then
  107. begin
  108. if expo<>0 then
  109. power:=0.0
  110. else
  111. HandleError(207);
  112. end
  113. else if expo=0 then
  114. power:=1
  115. else
  116. { bas < 0 is not allowed }
  117. if bas<0 then
  118. handleerror(207)
  119. else
  120. power:=exp(ln(bas)*expo);
  121. end;
  122. {****************************************************************************
  123. Longint data type routines
  124. ****************************************************************************}
  125. {$define FPC_SYSTEM_HAS_POWER_INT64}
  126. function power(bas,expo : Int64) : Int64;
  127. begin
  128. if bas=0 then
  129. begin
  130. if expo<>0 then
  131. power:=0
  132. else
  133. HandleError(207);
  134. end
  135. else if expo=0 then
  136. power:=1
  137. else
  138. begin
  139. if bas<0 then
  140. begin
  141. if odd(expo) then
  142. power:=-round(exp(ln(-bas)*expo))
  143. else
  144. power:=round(exp(ln(-bas)*expo));
  145. end
  146. else
  147. power:=round(exp(ln(bas)*expo));
  148. end;
  149. end;
  150. {****************************************************************************
  151. Helper routines to support old TP styled reals
  152. ****************************************************************************}
  153. { warning: the following converts a little-endian TP-style real }
  154. { to a big-endian double. So don't byte-swap the TP real! }
  155. {$define FPC_SYSTEM_HAS_REAL2DOUBLE}
  156. function real2double(r : real48) : double;
  157. var
  158. res : array[0..7] of byte;
  159. exponent : word;
  160. begin
  161. { copy mantissa }
  162. res[6]:=0;
  163. res[5]:=r[1] shl 5;
  164. res[4]:=(r[1] shr 3) or (r[2] shl 5);
  165. res[3]:=(r[2] shr 3) or (r[3] shl 5);
  166. res[2]:=(r[3] shr 3) or (r[4] shl 5);
  167. res[1]:=(r[4] shr 3) or (r[5] and $7f) shl 5;
  168. res[0]:=(r[5] and $7f) shr 3;
  169. { copy exponent }
  170. { correct exponent: }
  171. exponent:=(word(r[0])+(1023-129));
  172. res[1]:=res[1] or ((exponent and $f) shl 4);
  173. res[0]:=exponent shr 4;
  174. { set sign }
  175. res[0]:=res[0] or (r[5] and $80);
  176. real2double:=double(res);
  177. end;
  178. {
  179. $Log$
  180. Revision 1.7 2003-09-14 15:02:24 peter
  181. * remove int64 to double helpers
  182. Revision 1.6 2003/09/02 17:41:49 peter
  183. * updated for int64 to double
  184. Revision 1.5 2003/09/01 20:46:32 peter
  185. * new dummies
  186. Revision 1.4 2003/04/23 21:28:21 peter
  187. * fpc_round added, needed for int64 currency
  188. Revision 1.3 2003/01/22 20:45:15 mazen
  189. * making math code in RTL compiling.
  190. *NB : This does NOT mean necessary that it will generate correct code!
  191. Revision 1.2 2003/01/20 22:21:36 mazen
  192. * many stuff related to RTL fixed
  193. Revision 1.1 2002/12/24 21:30:20 mazen
  194. - some writeln(s) removed in compiler
  195. + many files added to RTL
  196. * some errors fixed in RTL
  197. Revision 1.14 2002/11/28 11:04:16 olle
  198. * macos: refs to globals in begin{asm} adapted to macos
  199. Revision 1.13 2002/10/21 18:08:28 jonas
  200. * round has int64 instead of longint result
  201. Revision 1.12 2002/09/08 13:00:21 jonas
  202. * made pi an internproc instead of internconst
  203. Revision 1.11 2002/09/07 16:01:26 peter
  204. * old logs removed and tabs fixed
  205. Revision 1.10 2002/08/18 22:11:10 florian
  206. * fixed remaining assembler errors
  207. Revision 1.9 2002/08/18 21:37:48 florian
  208. * several errors in inline assembler fixed
  209. Revision 1.8 2002/08/10 17:14:36 jonas
  210. * various fixes, mostly changing the names of the modifies registers to
  211. upper case since that seems to be required by the compiler
  212. Revision 1.7 2002/07/31 16:58:12 jonas
  213. * fixed conversion from int64/qword to double errors
  214. Revision 1.6 2002/07/29 21:28:17 florian
  215. * several fixes to get further with linux/ppc system unit compilation
  216. Revision 1.5 2002/07/28 21:39:29 florian
  217. * made abs a compiler proc if it is generic
  218. Revision 1.4 2002/07/28 20:43:49 florian
  219. * several fixes for linux/powerpc
  220. * several fixes to MT
  221. }