* dynl-dl.c (sysdep_dynl_link): Added parenthesis around the
[bpt/guile.git] / libguile / numbers.h
CommitLineData
0f2d19dd
JB
1/* classes: h_files */
2
3#ifndef NUMBERSH
4#define NUMBERSH
7dc6e754 5/* Copyright (C) 1995, 1996, 1998 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. */
0f2d19dd
JB
45\f
46
b4309c3c 47#include "libguile/__scm.h"
0f2d19dd 48
34f74fe1
MD
49#include "libguile/print.h"
50
0f2d19dd
JB
51\f
52
53
54/* Immediate Numbers
55 *
56 * Inums are exact integer data that fits within an SCM word.
57 *
58 * SCM_INUMP applies only to values known to be Scheme objects.
59 * In particular, SCM_INUMP (SCM_CAR (x)) is valid only if x is known
60 * to be a SCM_CONSP. If x is only known to be a SCM_NIMP,
61 * SCM_INUMP (SCM_CAR (x)) can give wrong answers.
62 */
63
64#define SCM_INUMP(x) (2 & (int)(x))
65#define SCM_NINUMP(x) (!SCM_INUMP(x))
66
67#ifdef __TURBOC__
68/* shifts of more than one are done by a library call, single shifts are
69 * performed in registers
70 */
71# define SCM_MAKINUM(x) ((((x)<<1)<<1)+2L)
72#else
73# define SCM_MAKINUM(x) (((x)<<2)+2L)
74#endif /* def __TURBOC__ */
75
76
77/* SCM_SRS is signed right shift */
78/* Turbo C++ v1.0 has a bug with right shifts of signed longs!
79 * It is believed to be fixed in Turbo C++ v1.01
80 */
81#if (-1==(((-1)<<2)+2)>>2) && (__TURBOC__ != 0x295)
82# define SCM_SRS(x, y) ((x)>>y)
83# ifdef __TURBOC__
84# define SCM_INUM(x) (((x)>>1)>>1)
85# else
86# define SCM_INUM(x) SCM_SRS(x, 2)
87# endif /* def __TURBOC__ */
88#else
89# define SCM_SRS(x, y) (((x)<0) ? ~((~(x))>>y) : (x)>>y)
90# define SCM_INUM(x) SCM_SRS(x, 2)
91#endif /* (-1==(((-1)<<2)+2)>>2) && (__TURBOC__ != 0x295) */
92
93
94/* A name for 0.
95 */
96#define SCM_INUM0 ((SCM) 2)
97
98
99
100/* SCM_FIXABLE is non-0 if its long argument can be encoded in an SCM_INUM.
101 */
08b5b88c
MD
102#define SCM_POSFIXABLE(n) ((n) <= SCM_MOST_POSITIVE_FIXNUM)
103#define SCM_NEGFIXABLE(n) ((n) >= SCM_MOST_NEGATIVE_FIXNUM)
104#define SCM_UNEGFIXABLE(n) ((n) <= -SCM_MOST_NEGATIVE_FIXNUM)
105#define SCM_FIXABLE(n) (SCM_POSFIXABLE(n) && SCM_NEGFIXABLE(n))
0f2d19dd
JB
106
107/* SCM_INTBUFLEN is the maximum number of characters neccessary for the
108 * printed or scm_string representation of an exact immediate.
109 */
110
111#ifndef SCM_CHAR_BIT
112# define SCM_CHAR_BIT 8
113#endif /* ndef SCM_CHAR_BIT */
114#ifndef SCM_LONG_BIT
115# define SCM_LONG_BIT (SCM_CHAR_BIT*sizeof(long)/sizeof(char))
116#endif /* ndef SCM_LONG_BIT */
117#define SCM_INTBUFLEN (5+SCM_LONG_BIT)
118
119/* SCM_FLOBUFLEN is the maximum number of characters neccessary for the
120 * printed or scm_string representation of an inexact number.
121 */
122
123#define SCM_FLOBUFLEN (10+2*(sizeof(double)/sizeof(char)*SCM_CHAR_BIT*3+9)/10)
124
125
126\f
127
128/* Numbers
129 */
130
131#define SCM_INEXP(x) (SCM_TYP16(x)==scm_tc16_flo)
132#define SCM_CPLXP(x) (SCM_CAR(x)==scm_tc_dblc)
133#define SCM_REAL(x) (*(((scm_dbl *) (SCM2PTR(x)))->real))
134#define SCM_IMAG(x) (*((double *)(SCM_CHARS(x)+sizeof(double))))
135/* ((&SCM_REAL(x))[1]) */
136
137
138#ifdef SCM_SINGLES
139#define SCM_REALP(x) ((~SCM_REAL_PART & SCM_CAR(x))==scm_tc_flo)
140#define SCM_SINGP(x) (SCM_CAR(x)==scm_tc_flo)
141#define SCM_FLO(x) (((scm_flo *)(SCM2PTR(x)))->num)
142#define SCM_REALPART(x) (SCM_SINGP(x)?0.0+SCM_FLO(x):SCM_REAL(x))
143#else /* SCM_SINGLES */
144#define SCM_REALP(x) (SCM_CAR(x)==scm_tc_dblr)
145#define SCM_REALPART SCM_REAL
146#endif /* SCM_SINGLES */
147
148
149/* Define SCM_BIGDIG to an integer type whose size is smaller than long if
150 * you want bignums. SCM_BIGRAD is one greater than the biggest SCM_BIGDIG.
151 *
152 * Define SCM_DIGSTOOBIG if the digits equivalent to a long won't fit in a long.
153 */
154#ifdef BIGNUMS
155# ifdef _UNICOS
156# define SCM_DIGSTOOBIG
157# if (1L << 31) <= SCM_USHRT_MAX
158# define SCM_BIGDIG unsigned short
159# else
160# define SCM_BIGDIG unsigned int
161# endif /* (1L << 31) <= USHRT_MAX */
162# define SCM_BITSPERDIG 32
163# else
164# define SCM_BIGDIG unsigned short
165# define SCM_BITSPERDIG (sizeof(SCM_BIGDIG)*SCM_CHAR_BIT)
166# endif /* def _UNICOS */
167
168# define SCM_BIGRAD (1L << SCM_BITSPERDIG)
169# define SCM_DIGSPERLONG ((scm_sizet)((sizeof(long)*SCM_CHAR_BIT+SCM_BITSPERDIG-1)/SCM_BITSPERDIG))
170# define SCM_DIGSPERLONGLONG ((scm_sizet)((sizeof(long long)*SCM_CHAR_BIT+SCM_BITSPERDIG-1)/SCM_BITSPERDIG))
171# define SCM_BIGUP(x) ((unsigned long)(x) << SCM_BITSPERDIG)
08b5b88c 172# define SCM_LONGLONGBIGUP(x) ((ulong_long)(x) << SCM_BITSPERDIG)
0f2d19dd
JB
173# define SCM_BIGDN(x) ((x) >> SCM_BITSPERDIG)
174# define SCM_BIGLO(x) ((x) & (SCM_BIGRAD-1))
34f74fe1 175
0f2d19dd
JB
176#endif /* def BIGNUMS */
177
178#ifndef SCM_BIGDIG
179/* Definition is not really used but helps various function
180 * prototypes to compile with conditionalization.
181 */
182# define SCM_BIGDIG unsigned short
08b5b88c 183# define SCM_NO_BIGDIG
0f2d19dd
JB
184# ifndef SCM_FLOATS
185# define SCM_INUMS_ONLY
186# endif /* ndef SCM_FLOATS */
187#endif /* ndef SCM_BIGDIG */
188
189#ifdef SCM_FLOATS
190#define SCM_NUMBERP(x) (SCM_INUMP(x) || (SCM_NIMP(x) && SCM_NUMP(x)))
9d0b279f
MD
191#ifdef SCM_BIGDIG
192#define SCM_NUM2DBL(x) (SCM_INUMP (x) \
193 ? (double) SCM_INUM (x) \
194 : (SCM_REALP (x) \
195 ? SCM_REALPART (x) \
196 : scm_big2dbl (x)))
197#else
198#define SCM_NUM2DBL(x) (SCM_INUMP (x) \
199 ? (double) SCM_INUM (x) \
200 : SCM_REALPART (x))
201#endif
0f2d19dd
JB
202#else
203#ifdef SCM_BIGDIG
204#define SCM_NUMBERP(x) (SCM_INUMP(x) || (SCM_NIMP(x) && SCM_NUMP(x)))
9d0b279f
MD
205#define SCM_NUM2DBL(x) (SCM_INUMP (x) \
206 ? (double) SCM_INUM (x) \
207 : scm_big2dbl (x))
0f2d19dd
JB
208#else
209#define SCM_NUMBERP SCM_INUMP
9d0b279f 210#define SCM_NUM2DBL(x) ((double) SCM_INUM (x))
0f2d19dd
JB
211#endif
212#endif
213#define SCM_NUMP(x) ((0xfcff & (int)SCM_CAR(x))==scm_tc7_smob)
214#define SCM_BIGP(x) (SCM_TYP16S(x)==scm_tc16_bigpos)
215#define SCM_BIGSIGN(x) (0x0100 & (int)SCM_CAR(x))
216#define SCM_BDIGITS(x) ((SCM_BIGDIG *)(SCM_CDR(x)))
217#define SCM_NUMDIGS(x) ((scm_sizet)(SCM_CAR(x)>>16))
a6c64c3c 218#define SCM_SETNUMDIGS(x, v, t) SCM_SETCAR(x, (((v)+0L)<<16)+(t))
0f2d19dd
JB
219\f
220
221#ifdef SCM_FLOATS
222typedef struct scm_dblproc
223{
224 char *scm_string;
225 double (*cproc) ();
226} scm_dblproc;
227
228#ifdef SCM_SINGLES
229typedef struct scm_flo
230{
231 SCM type;
232 float num;
233} scm_flo;
234#endif
235
236typedef struct scm_dbl
237{
238 SCM type;
239 double *real;
240} scm_dbl;
241#endif
242
243
244
245\f
0f2d19dd 246
1cc91f1b
JB
247extern SCM scm_exact_p SCM_P ((SCM x));
248extern SCM scm_odd_p SCM_P ((SCM n));
249extern SCM scm_even_p SCM_P ((SCM n));
250extern SCM scm_abs SCM_P ((SCM x));
251extern SCM scm_quotient SCM_P ((SCM x, SCM y));
252extern SCM scm_remainder SCM_P ((SCM x, SCM y));
253extern SCM scm_modulo SCM_P ((SCM x, SCM y));
254extern SCM scm_gcd SCM_P ((SCM x, SCM y));
255extern SCM scm_lcm SCM_P ((SCM n1, SCM n2));
256extern SCM scm_logand SCM_P ((SCM n1, SCM n2));
257extern SCM scm_logior SCM_P ((SCM n1, SCM n2));
258extern SCM scm_logxor SCM_P ((SCM n1, SCM n2));
259extern SCM scm_logtest SCM_P ((SCM n1, SCM n2));
260extern SCM scm_logbit_p SCM_P ((SCM n1, SCM n2));
261extern SCM scm_lognot SCM_P ((SCM n));
262extern SCM scm_integer_expt SCM_P ((SCM z1, SCM z2));
263extern SCM scm_ash SCM_P ((SCM n, SCM cnt));
264extern SCM scm_bit_extract SCM_P ((SCM n, SCM start, SCM end));
265extern SCM scm_logcount SCM_P ((SCM n));
266extern SCM scm_integer_length SCM_P ((SCM n));
267extern SCM scm_mkbig SCM_P ((scm_sizet nlen, int sign));
268extern SCM scm_big2inum SCM_P ((SCM b, scm_sizet l));
269extern SCM scm_adjbig SCM_P ((SCM b, scm_sizet nlen));
270extern SCM scm_normbig SCM_P ((SCM b));
271extern SCM scm_copybig SCM_P ((SCM b, int sign));
272extern SCM scm_long2big SCM_P ((long n));
273extern SCM scm_long_long2big SCM_P ((long_long n));
274extern SCM scm_2ulong2big SCM_P ((unsigned long * np));
275extern SCM scm_ulong2big SCM_P ((unsigned long n));
276extern int scm_bigcomp SCM_P ((SCM x, SCM y));
277extern long scm_pseudolong SCM_P ((long x));
278extern void scm_longdigs SCM_P ((long x, SCM_BIGDIG digs[]));
279extern SCM scm_addbig SCM_P ((SCM_BIGDIG *x, scm_sizet nx, int xsgn, SCM bigy, int sgny));
280extern SCM scm_mulbig SCM_P ((SCM_BIGDIG *x, scm_sizet nx, SCM_BIGDIG *y, scm_sizet ny, int sgn));
281extern unsigned int scm_divbigdig SCM_P ((SCM_BIGDIG *ds, scm_sizet h, SCM_BIGDIG div));
282extern SCM scm_divbigint SCM_P ((SCM x, long z, int sgn, int mode));
283extern SCM scm_divbigbig SCM_P ((SCM_BIGDIG *x, scm_sizet nx, SCM_BIGDIG *y, scm_sizet ny, int sgn, int modes));
284extern scm_sizet scm_iint2str SCM_P ((long num, int rad, char *p));
285extern SCM scm_number_to_string SCM_P ((SCM x, SCM radix));
286extern int scm_floprint SCM_P ((SCM sexp, SCM port, scm_print_state *pstate));
287extern int scm_bigprint SCM_P ((SCM exp, SCM port, scm_print_state *pstate));
288extern SCM scm_istr2int SCM_P ((char *str, long len, long radix));
289extern SCM scm_istr2flo SCM_P ((char *str, long len, long radix));
290extern SCM scm_istring2number SCM_P ((char *str, long len, long radix));
291extern SCM scm_string_to_number SCM_P ((SCM str, SCM radix));
292extern SCM scm_makdbl SCM_P ((double x, double y));
293extern SCM scm_bigequal SCM_P ((SCM x, SCM y));
294extern SCM scm_floequal SCM_P ((SCM x, SCM y));
295extern SCM scm_number_p SCM_P ((SCM x));
296extern SCM scm_real_p SCM_P ((SCM x));
bebdc83a 297extern SCM scm_integer_p SCM_P ((SCM x));
1cc91f1b
JB
298extern SCM scm_inexact_p SCM_P ((SCM x));
299extern SCM scm_num_eq_p SCM_P ((SCM x, SCM y));
300extern SCM scm_less_p SCM_P ((SCM x, SCM y));
301extern SCM scm_gr_p SCM_P ((SCM x, SCM y));
302extern SCM scm_leq_p SCM_P ((SCM x, SCM y));
303extern SCM scm_geq_p SCM_P ((SCM x, SCM y));
304extern SCM scm_zero_p SCM_P ((SCM z));
305extern SCM scm_positive_p SCM_P ((SCM x));
306extern SCM scm_negative_p SCM_P ((SCM x));
307extern SCM scm_max SCM_P ((SCM x, SCM y));
308extern SCM scm_min SCM_P ((SCM x, SCM y));
309extern SCM scm_sum SCM_P ((SCM x, SCM y));
310extern SCM scm_difference SCM_P ((SCM x, SCM y));
311extern SCM scm_product SCM_P ((SCM x, SCM y));
3eeba8d4 312extern double scm_num2dbl SCM_P ((SCM a, const char * why));
1cc91f1b
JB
313extern SCM scm_divide SCM_P ((SCM x, SCM y));
314extern double scm_asinh SCM_P ((double x));
315extern double scm_acosh SCM_P ((double x));
316extern double scm_atanh SCM_P ((double x));
317extern double scm_truncate SCM_P ((double x));
318extern double scm_round SCM_P ((double x));
319extern double scm_exact_to_inexact SCM_P ((double z));
320extern SCM scm_sys_expt SCM_P ((SCM z1, SCM z2));
321extern SCM scm_sys_atan2 SCM_P ((SCM z1, SCM z2));
322extern SCM scm_make_rectangular SCM_P ((SCM z1, SCM z2));
323extern SCM scm_make_polar SCM_P ((SCM z1, SCM z2));
324extern SCM scm_real_part SCM_P ((SCM z));
325extern SCM scm_imag_part SCM_P ((SCM z));
326extern SCM scm_magnitude SCM_P ((SCM z));
327extern SCM scm_angle SCM_P ((SCM z));
328extern SCM scm_inexact_to_exact SCM_P ((SCM z));
329extern SCM scm_trunc SCM_P ((SCM x));
330extern SCM scm_dbl2big SCM_P ((double d));
331extern double scm_big2dbl SCM_P ((SCM b));
332extern SCM scm_long2num SCM_P ((long sl));
333extern SCM scm_long_long2num SCM_P ((long_long sl));
334extern SCM scm_ulong2num SCM_P ((unsigned long sl));
3eeba8d4
JB
335extern long scm_num2long SCM_P ((SCM num, char *pos, const char *s_caller));
336extern long_long scm_num2long_long SCM_P ((SCM num, char *pos,
337 const char *s_caller));
338extern unsigned long scm_num2ulong SCM_P ((SCM num, char *pos,
339 const char *s_caller));
1cc91f1b 340extern void scm_init_numbers SCM_P ((void));
0f2d19dd
JB
341
342#endif /* NUMBERSH */