Commit | Line | Data |
---|---|---|
0f2d19dd JB |
1 | /* classes: h_files */ |
2 | ||
3 | #ifndef NUMBERSH | |
4 | #define NUMBERSH | |
7a58e59f | 5 | /* Copyright (C) 1995, 1996, 1998, 2000 Free Software Foundation, Inc. |
0f2d19dd JB |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2, or (at your option) | |
10 | * any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this software; see the file COPYING. If not, write to | |
82892bed JB |
19 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
20 | * Boston, MA 02111-1307 USA | |
0f2d19dd JB |
21 | * |
22 | * As a special exception, the Free Software Foundation gives permission | |
23 | * for additional uses of the text contained in its release of GUILE. | |
24 | * | |
25 | * The exception is that, if you link the GUILE library with other files | |
26 | * to produce an executable, this does not by itself cause the | |
27 | * resulting executable to be covered by the GNU General Public License. | |
28 | * Your use of that executable is in no way restricted on account of | |
29 | * linking the GUILE library code into it. | |
30 | * | |
31 | * This exception does not however invalidate any other reasons why | |
32 | * the executable file might be covered by the GNU General Public License. | |
33 | * | |
34 | * This exception applies only to the code released by the | |
35 | * Free Software Foundation under the name GUILE. If you copy | |
36 | * code from other Free Software Foundation releases into a copy of | |
37 | * GUILE, as the General Public License permits, the exception does | |
38 | * not apply to the code that you add in this way. To avoid misleading | |
39 | * anyone as to the status of such modified files, you must delete | |
40 | * this exception notice from them. | |
41 | * | |
42 | * If you write modifications of your own for GUILE, it is your choice | |
43 | * whether to permit this exception to apply to your modifications. | |
82892bed | 44 | * If you do not wish that, delete this exception notice. */ |
d3a6bc94 GB |
45 | |
46 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, | |
47 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ | |
0f2d19dd JB |
48 | \f |
49 | ||
b4309c3c | 50 | #include "libguile/__scm.h" |
0f2d19dd | 51 | |
34f74fe1 MD |
52 | #include "libguile/print.h" |
53 | ||
0f2d19dd JB |
54 | \f |
55 | ||
56 | ||
57 | /* Immediate Numbers | |
58 | * | |
59 | * Inums are exact integer data that fits within an SCM word. | |
60 | * | |
61 | * SCM_INUMP applies only to values known to be Scheme objects. | |
62 | * In particular, SCM_INUMP (SCM_CAR (x)) is valid only if x is known | |
63 | * to be a SCM_CONSP. If x is only known to be a SCM_NIMP, | |
64 | * SCM_INUMP (SCM_CAR (x)) can give wrong answers. | |
65 | */ | |
66 | ||
67 | #define SCM_INUMP(x) (2 & (int)(x)) | |
68 | #define SCM_NINUMP(x) (!SCM_INUMP(x)) | |
69 | ||
70 | #ifdef __TURBOC__ | |
71 | /* shifts of more than one are done by a library call, single shifts are | |
72 | * performed in registers | |
73 | */ | |
f1267706 | 74 | # define SCM_MAKINUM(x) ((SCM) (((SCM_UNPACK(x)<<1)<<1)+2L)) |
0f2d19dd | 75 | #else |
f1267706 | 76 | # define SCM_MAKINUM(x) ((SCM)((SCM_UNPACK(x)<<2)+2L)) |
0f2d19dd JB |
77 | #endif /* def __TURBOC__ */ |
78 | ||
79 | ||
80 | /* SCM_SRS is signed right shift */ | |
c209c88e | 81 | /* SCM_INUM makes a C int from an SCM immediate number. */ |
0f2d19dd JB |
82 | /* Turbo C++ v1.0 has a bug with right shifts of signed longs! |
83 | * It is believed to be fixed in Turbo C++ v1.01 | |
84 | */ | |
85 | #if (-1==(((-1)<<2)+2)>>2) && (__TURBOC__ != 0x295) | |
7a58e59f | 86 | # define SCM_SRS(x, y) ((x) >> y) |
0f2d19dd | 87 | # ifdef __TURBOC__ |
f1267706 | 88 | # define SCM_INUM(x) ((SCM_UNPACK (x) >>1) >>1) |
0f2d19dd | 89 | # else |
f1267706 | 90 | # define SCM_INUM(x) SCM_SRS (SCM_UNPACK (x), 2) |
0f2d19dd JB |
91 | # endif /* def __TURBOC__ */ |
92 | #else | |
7a58e59f | 93 | # define SCM_SRS(x, y)\ |
f1267706 MD |
94 | ((SCM_UNPACK (x) < 0) ? ~( (~SCM_UNPACK (x)) >> y) : (SCM_UNPACK (x) >> y)) |
95 | # define SCM_INUM(x) SCM_SRS (SCM_UNPACK (x), 2) | |
0f2d19dd JB |
96 | #endif /* (-1==(((-1)<<2)+2)>>2) && (__TURBOC__ != 0x295) */ |
97 | ||
98 | ||
99 | /* A name for 0. | |
100 | */ | |
101 | #define SCM_INUM0 ((SCM) 2) | |
102 | ||
103 | ||
104 | ||
105 | /* SCM_FIXABLE is non-0 if its long argument can be encoded in an SCM_INUM. | |
106 | */ | |
08b5b88c MD |
107 | #define SCM_POSFIXABLE(n) ((n) <= SCM_MOST_POSITIVE_FIXNUM) |
108 | #define SCM_NEGFIXABLE(n) ((n) >= SCM_MOST_NEGATIVE_FIXNUM) | |
109 | #define SCM_UNEGFIXABLE(n) ((n) <= -SCM_MOST_NEGATIVE_FIXNUM) | |
110 | #define SCM_FIXABLE(n) (SCM_POSFIXABLE(n) && SCM_NEGFIXABLE(n)) | |
0f2d19dd JB |
111 | |
112 | /* SCM_INTBUFLEN is the maximum number of characters neccessary for the | |
113 | * printed or scm_string representation of an exact immediate. | |
114 | */ | |
115 | ||
116 | #ifndef SCM_CHAR_BIT | |
117 | # define SCM_CHAR_BIT 8 | |
118 | #endif /* ndef SCM_CHAR_BIT */ | |
119 | #ifndef SCM_LONG_BIT | |
120 | # define SCM_LONG_BIT (SCM_CHAR_BIT*sizeof(long)/sizeof(char)) | |
121 | #endif /* ndef SCM_LONG_BIT */ | |
122 | #define SCM_INTBUFLEN (5+SCM_LONG_BIT) | |
123 | ||
124 | /* SCM_FLOBUFLEN is the maximum number of characters neccessary for the | |
125 | * printed or scm_string representation of an inexact number. | |
126 | */ | |
127 | ||
128 | #define SCM_FLOBUFLEN (10+2*(sizeof(double)/sizeof(char)*SCM_CHAR_BIT*3+9)/10) | |
129 | ||
130 | ||
131 | \f | |
132 | ||
133 | /* Numbers | |
134 | */ | |
135 | ||
d3a6bc94 | 136 | #define SCM_INEXP(x) (SCM_NIMP(x) && (SCM_TYP16(x)==scm_tc16_flo)) |
f1267706 | 137 | #define SCM_CPLXP(x) (SCM_NIMP(x) && (SCM_UNPACK_CAR (x)==scm_tc_dblc)) |
0f2d19dd JB |
138 | #define SCM_REAL(x) (*(((scm_dbl *) (SCM2PTR(x)))->real)) |
139 | #define SCM_IMAG(x) (*((double *)(SCM_CHARS(x)+sizeof(double)))) | |
140 | /* ((&SCM_REAL(x))[1]) */ | |
141 | ||
142 | ||
143 | #ifdef SCM_SINGLES | |
f1267706 MD |
144 | #define SCM_REALP(x) (SCM_NIMP(x) && ((~SCM_REAL_PART & SCM_UNPACK_CAR (x))==scm_tc_flo)) |
145 | #define SCM_SINGP(x) (SCM_NIMP(x) && (SCM_UNPACK_CAR (x)==scm_tc_flo)) | |
0f2d19dd JB |
146 | #define SCM_FLO(x) (((scm_flo *)(SCM2PTR(x)))->num) |
147 | #define SCM_REALPART(x) (SCM_SINGP(x)?0.0+SCM_FLO(x):SCM_REAL(x)) | |
148 | #else /* SCM_SINGLES */ | |
d3a6bc94 | 149 | #define SCM_REALP(x) (SCM_NIMP(x) && (SCM_CAR(x)==scm_tc_dblr)) |
0f2d19dd JB |
150 | #define SCM_REALPART SCM_REAL |
151 | #endif /* SCM_SINGLES */ | |
152 | ||
153 | ||
154 | /* Define SCM_BIGDIG to an integer type whose size is smaller than long if | |
155 | * you want bignums. SCM_BIGRAD is one greater than the biggest SCM_BIGDIG. | |
156 | * | |
157 | * Define SCM_DIGSTOOBIG if the digits equivalent to a long won't fit in a long. | |
158 | */ | |
159 | #ifdef BIGNUMS | |
160 | # ifdef _UNICOS | |
161 | # define SCM_DIGSTOOBIG | |
162 | # if (1L << 31) <= SCM_USHRT_MAX | |
163 | # define SCM_BIGDIG unsigned short | |
164 | # else | |
165 | # define SCM_BIGDIG unsigned int | |
166 | # endif /* (1L << 31) <= USHRT_MAX */ | |
167 | # define SCM_BITSPERDIG 32 | |
168 | # else | |
169 | # define SCM_BIGDIG unsigned short | |
170 | # define SCM_BITSPERDIG (sizeof(SCM_BIGDIG)*SCM_CHAR_BIT) | |
171 | # endif /* def _UNICOS */ | |
172 | ||
173 | # define SCM_BIGRAD (1L << SCM_BITSPERDIG) | |
174 | # define SCM_DIGSPERLONG ((scm_sizet)((sizeof(long)*SCM_CHAR_BIT+SCM_BITSPERDIG-1)/SCM_BITSPERDIG)) | |
0f2d19dd | 175 | # define SCM_BIGUP(x) ((unsigned long)(x) << SCM_BITSPERDIG) |
08b5b88c | 176 | # define SCM_LONGLONGBIGUP(x) ((ulong_long)(x) << SCM_BITSPERDIG) |
0f2d19dd JB |
177 | # define SCM_BIGDN(x) ((x) >> SCM_BITSPERDIG) |
178 | # define SCM_BIGLO(x) ((x) & (SCM_BIGRAD-1)) | |
34f74fe1 | 179 | |
0f2d19dd JB |
180 | #endif /* def BIGNUMS */ |
181 | ||
182 | #ifndef SCM_BIGDIG | |
183 | /* Definition is not really used but helps various function | |
184 | * prototypes to compile with conditionalization. | |
185 | */ | |
186 | # define SCM_BIGDIG unsigned short | |
08b5b88c | 187 | # define SCM_NO_BIGDIG |
0f2d19dd JB |
188 | # ifndef SCM_FLOATS |
189 | # define SCM_INUMS_ONLY | |
190 | # endif /* ndef SCM_FLOATS */ | |
191 | #endif /* ndef SCM_BIGDIG */ | |
192 | ||
193 | #ifdef SCM_FLOATS | |
194 | #define SCM_NUMBERP(x) (SCM_INUMP(x) || (SCM_NIMP(x) && SCM_NUMP(x))) | |
9d0b279f MD |
195 | #ifdef SCM_BIGDIG |
196 | #define SCM_NUM2DBL(x) (SCM_INUMP (x) \ | |
197 | ? (double) SCM_INUM (x) \ | |
198 | : (SCM_REALP (x) \ | |
199 | ? SCM_REALPART (x) \ | |
200 | : scm_big2dbl (x))) | |
201 | #else | |
202 | #define SCM_NUM2DBL(x) (SCM_INUMP (x) \ | |
203 | ? (double) SCM_INUM (x) \ | |
204 | : SCM_REALPART (x)) | |
205 | #endif | |
0f2d19dd JB |
206 | #else |
207 | #ifdef SCM_BIGDIG | |
208 | #define SCM_NUMBERP(x) (SCM_INUMP(x) || (SCM_NIMP(x) && SCM_NUMP(x))) | |
9d0b279f MD |
209 | #define SCM_NUM2DBL(x) (SCM_INUMP (x) \ |
210 | ? (double) SCM_INUM (x) \ | |
211 | : scm_big2dbl (x)) | |
0f2d19dd JB |
212 | #else |
213 | #define SCM_NUMBERP SCM_INUMP | |
9d0b279f | 214 | #define SCM_NUM2DBL(x) ((double) SCM_INUM (x)) |
0f2d19dd JB |
215 | #endif |
216 | #endif | |
d3a6bc94 GB |
217 | #define SCM_NUMP(x) (SCM_NIMP(x) && (0xfcff & (int)SCM_CAR(x))==scm_tc7_smob) |
218 | #define SCM_BIGP(x) (SCM_NIMP(x) && SCM_TYP16S(x)==scm_tc16_bigpos) | |
0f2d19dd JB |
219 | #define SCM_BIGSIGN(x) (0x0100 & (int)SCM_CAR(x)) |
220 | #define SCM_BDIGITS(x) ((SCM_BIGDIG *)(SCM_CDR(x))) | |
f1267706 | 221 | #define SCM_NUMDIGS(x) ((scm_sizet)(SCM_UNPACK_CAR (x)>>16)) |
a6c64c3c | 222 | #define SCM_SETNUMDIGS(x, v, t) SCM_SETCAR(x, (((v)+0L)<<16)+(t)) |
0f2d19dd JB |
223 | \f |
224 | ||
225 | #ifdef SCM_FLOATS | |
226 | typedef struct scm_dblproc | |
227 | { | |
228 | char *scm_string; | |
229 | double (*cproc) (); | |
230 | } scm_dblproc; | |
231 | ||
232 | #ifdef SCM_SINGLES | |
233 | typedef struct scm_flo | |
234 | { | |
235 | SCM type; | |
236 | float num; | |
237 | } scm_flo; | |
238 | #endif | |
239 | ||
240 | typedef struct scm_dbl | |
241 | { | |
242 | SCM type; | |
243 | double *real; | |
244 | } scm_dbl; | |
245 | #endif | |
246 | ||
247 | ||
248 | ||
249 | \f | |
0f2d19dd | 250 | |
7866a09b GB |
251 | extern SCM scm_exact_p (SCM x); |
252 | extern SCM scm_odd_p (SCM n); | |
253 | extern SCM scm_even_p (SCM n); | |
254 | extern SCM scm_abs (SCM x); | |
255 | extern SCM scm_quotient (SCM x, SCM y); | |
256 | extern SCM scm_remainder (SCM x, SCM y); | |
257 | extern SCM scm_modulo (SCM x, SCM y); | |
258 | extern SCM scm_gcd (SCM x, SCM y); | |
259 | extern SCM scm_lcm (SCM n1, SCM n2); | |
260 | extern SCM scm_logand (SCM n1, SCM n2); | |
261 | extern SCM scm_logior (SCM n1, SCM n2); | |
262 | extern SCM scm_logxor (SCM n1, SCM n2); | |
263 | extern SCM scm_logtest (SCM n1, SCM n2); | |
264 | extern SCM scm_logbit_p (SCM n1, SCM n2); | |
265 | extern SCM scm_lognot (SCM n); | |
266 | extern SCM scm_integer_expt (SCM z1, SCM z2); | |
267 | extern SCM scm_ash (SCM n, SCM cnt); | |
268 | extern SCM scm_bit_extract (SCM n, SCM start, SCM end); | |
269 | extern SCM scm_logcount (SCM n); | |
270 | extern SCM scm_integer_length (SCM n); | |
271 | extern SCM scm_mkbig (scm_sizet nlen, int sign); | |
272 | extern SCM scm_big2inum (SCM b, scm_sizet l); | |
273 | extern SCM scm_adjbig (SCM b, scm_sizet nlen); | |
274 | extern SCM scm_normbig (SCM b); | |
275 | extern SCM scm_copybig (SCM b, int sign); | |
276 | extern SCM scm_long2big (long n); | |
277 | extern SCM scm_long_long2big (long_long n); | |
278 | extern SCM scm_2ulong2big (unsigned long * np); | |
279 | extern SCM scm_ulong2big (unsigned long n); | |
280 | extern int scm_bigcomp (SCM x, SCM y); | |
281 | extern long scm_pseudolong (long x); | |
282 | extern void scm_longdigs (long x, SCM_BIGDIG digs[]); | |
283 | extern SCM scm_addbig (SCM_BIGDIG *x, scm_sizet nx, int xsgn, SCM bigy, int sgny); | |
284 | extern SCM scm_mulbig (SCM_BIGDIG *x, scm_sizet nx, SCM_BIGDIG *y, scm_sizet ny, int sgn); | |
285 | extern unsigned int scm_divbigdig (SCM_BIGDIG *ds, scm_sizet h, SCM_BIGDIG div); | |
286 | extern SCM scm_divbigint (SCM x, long z, int sgn, int mode); | |
287 | extern SCM scm_divbigbig (SCM_BIGDIG *x, scm_sizet nx, SCM_BIGDIG *y, scm_sizet ny, int sgn, int modes); | |
288 | extern scm_sizet scm_iint2str (long num, int rad, char *p); | |
289 | extern SCM scm_number_to_string (SCM x, SCM radix); | |
290 | extern int scm_floprint (SCM sexp, SCM port, scm_print_state *pstate); | |
291 | extern int scm_bigprint (SCM exp, SCM port, scm_print_state *pstate); | |
292 | extern SCM scm_istr2int (char *str, long len, long radix); | |
293 | extern SCM scm_istr2flo (char *str, long len, long radix); | |
294 | extern SCM scm_istring2number (char *str, long len, long radix); | |
295 | extern SCM scm_string_to_number (SCM str, SCM radix); | |
296 | extern SCM scm_makdbl (double x, double y); | |
297 | extern SCM scm_bigequal (SCM x, SCM y); | |
298 | extern SCM scm_floequal (SCM x, SCM y); | |
299 | extern SCM scm_number_p (SCM x); | |
300 | extern SCM scm_real_p (SCM x); | |
301 | extern SCM scm_integer_p (SCM x); | |
302 | extern SCM scm_inexact_p (SCM x); | |
303 | extern SCM scm_num_eq_p (SCM x, SCM y); | |
304 | extern SCM scm_less_p (SCM x, SCM y); | |
305 | extern SCM scm_gr_p (SCM x, SCM y); | |
306 | extern SCM scm_leq_p (SCM x, SCM y); | |
307 | extern SCM scm_geq_p (SCM x, SCM y); | |
308 | extern SCM scm_zero_p (SCM z); | |
309 | extern SCM scm_positive_p (SCM x); | |
310 | extern SCM scm_negative_p (SCM x); | |
311 | extern SCM scm_max (SCM x, SCM y); | |
312 | extern SCM scm_min (SCM x, SCM y); | |
313 | extern SCM scm_sum (SCM x, SCM y); | |
314 | extern SCM scm_difference (SCM x, SCM y); | |
315 | extern SCM scm_product (SCM x, SCM y); | |
316 | extern double scm_num2dbl (SCM a, const char * why); | |
317 | extern SCM scm_divide (SCM x, SCM y); | |
318 | extern double scm_asinh (double x); | |
319 | extern double scm_acosh (double x); | |
320 | extern double scm_atanh (double x); | |
321 | extern double scm_truncate (double x); | |
322 | extern double scm_round (double x); | |
323 | extern double scm_exact_to_inexact (double z); | |
324 | extern SCM scm_sys_expt (SCM z1, SCM z2); | |
325 | extern SCM scm_sys_atan2 (SCM z1, SCM z2); | |
326 | extern SCM scm_make_rectangular (SCM z1, SCM z2); | |
327 | extern SCM scm_make_polar (SCM z1, SCM z2); | |
328 | extern SCM scm_real_part (SCM z); | |
329 | extern SCM scm_imag_part (SCM z); | |
330 | extern SCM scm_magnitude (SCM z); | |
331 | extern SCM scm_angle (SCM z); | |
332 | extern SCM scm_inexact_to_exact (SCM z); | |
333 | extern SCM scm_trunc (SCM x); | |
334 | extern SCM scm_dbl2big (double d); | |
335 | extern double scm_big2dbl (SCM b); | |
336 | extern SCM scm_long2num (long sl); | |
337 | extern SCM scm_long_long2num (long_long sl); | |
338 | extern SCM scm_ulong2num (unsigned long sl); | |
339 | extern long scm_num2long (SCM num, char *pos, const char *s_caller); | |
340 | extern long_long scm_num2long_long (SCM num, char *pos, | |
341 | const char *s_caller); | |
342 | extern unsigned long scm_num2ulong (SCM num, char *pos, | |
343 | const char *s_caller); | |
344 | extern void scm_init_numbers (void); | |
0f2d19dd JB |
345 | |
346 | #endif /* NUMBERSH */ |