Initial git import
[sxemacs] / src / ent / ent-mpfr.h
1 /*
2   ent-mpfr.c -- Numeric types for SXEmacs
3   Copyright (C) 2005, 2006 Sebastian Freundt
4
5   Author:  Sebastian Freundt
6
7 This file is part of SXEmacs
8
9 SXEmacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 SXEmacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
21
22
23 #ifndef INCLUDED_number_mpfr_h_
24 #define INCLUDED_number_mpfr_h_
25
26 #ifdef UNO
27 /* Uno complains about several inline functions that include conditions with
28    assignments and side effects if we don't do this */
29 #undef __GNUC__
30 #endif
31
32 #include <mpfr.h>
33
34 typedef mpfr_t bigfr;
35 typedef mp_prec_t bigfr_prec;
36 typedef mp_rnd_t bigfr_rnd;
37
38 extern Lisp_Object Qbigfrp;
39
40 extern Lisp_Object Veuler;
41 extern Lisp_Object Veuler_mascheroni;
42 extern Lisp_Object Vpi;
43
44 \f
45 typedef struct Lisp_Bigfr Lisp_Bigfr;
46 struct Lisp_Bigfr
47 {
48         struct lrecord_header lheader;
49         bigfr data;
50 };
51
52 DECLARE_LRECORD(bigfr, Lisp_Bigfr);
53 #define XBIGFR(x) XRECORD(x, bigfr, Lisp_Bigfr)
54 #define wrap_bigfr(p) wrap_object(p)
55 #define BIGFRP(x) RECORDP(x, bigfr)
56 #define CHECK_BIGFR(x) CHECK_RECORD(x, bigfr)
57 #define CONCHECK_BIGFR(x) CONCHECK_RECORD(x, bigfr)
58
59 #define bigfr_data(f) ((f)->data)
60 #define XBIGFR_DATA(x) bigfr_data(XBIGFR(x))
61 #define XBIGFR_GET_PREC(x) bigfr_get_prec(XBIGFR_DATA(x))
62 #define XBIGFR_SET_PREC(x,p) bigfr_set_prec(XBIGFR_DATA(x), p)
63
64 #define BIGFR_ARITH_RETURN(f,op) do                                     \
65 {                                                                       \
66         Lisp_Object retval = make_bigfr_bfr(XBIGFR_DATA(f));            \
67         bigfr_##op(XBIGFR_DATA(retval), XBIGFR_DATA(f));                \
68         return retval;                                                  \
69 } while (0)
70
71 #define BIGFR_ARITH_RETURN1(f,op,arg) do                                \
72 {                                                                       \
73         Lisp_Object retval = make_bigfr_bfr(XBIGFR_DATA(f));            \
74         bigfr_##op(XBIGFR_DATA(retval), XBIGFR_DATA(f), arg);           \
75         return retval;                                                  \
76 } while (0)
77
78 #define BIGFR_INIT_PREC(f, prec) do                                     \
79 {                                                                       \
80         bigfr_init_prec(f, internal_get_precision(prec));               \
81 } while (0)
82
83 extern bigfr ent_scratch_bigfr;
84
85 \f
86 /******************************** Bigfrs ********************************/
87
88 /***** Bigfr: basic functions *****/
89 #define bigfr_init(f)                mpfr_init(f)
90 #define bigfr_init_prec(f,prec)      mpfr_init2(f, prec)
91 #define bigfr_fini(f)                mpfr_clear(f)
92 #define bigfr_hashcode(f)            mpfr_get_ui(f, GMP_RNDN)
93 #define bigfr_sign(f)                mpfr_sgn(f)
94 #define bigfr_get_prec(f)            mpfr_get_prec(f)
95 #define bigfr_set_prec(f, prec)      mpfr_set_prec(f, prec)
96 #define bigfr_set_default_prec(prec) mpfr_set_default_prec(prec)
97 #define bigfr_get_default_prec()     mpfr_get_default_prec()
98
99 /***** Bigfr: conversions *****/
100 extern Bufbyte *bigfr_to_string (bigfr, int);
101 #define bigfr_to_int(f)              ((int)mpfr_get_si(f), GMP_RNDN)
102 #define bigfr_to_uint(f)             ((unsigned int)mpfr_get_ui(f), GMP_RNDN)
103 #define bigfr_to_long(f)             mpfr_get_si(f, GMP_RNDN)
104 #define bigfr_to_ulong(f)            mpfr_get_ui(f, GMP_RNDN)
105 #if fpfloat_double_p
106 #define bigfr_to_fpfloat(f)             mpfr_get_d(f, GMP_RNDN)
107 #elif fpfloat_long_double_p
108 #define bigfr_to_fpfloat(f)             mpfr_get_ld(f, GMP_RNDN)
109 #endif
110
111 #if defined HAVE_MPZ && (defined WITH_GMP || defined WITH_MP)
112 #define bigz_set_bigfr(b,f)          mpfr_get_z(b, f, GMP_RNDN)
113 #endif
114 #if defined HAVE_MPQ && defined WITH_GMP
115 #define bigq_set_bigfr(q, f)         mpfr_get_q(q, f, GMP_RNDN);
116 #endif
117 #if defined HAVE_MPF && defined WITH_GMP
118 #define bigf_set_bigfr(f, fr)        mpfr_get_f(f, fr, GMP_RNDN);
119 #endif
120
121 /***** Bigfr: conversion predicates *****/
122 #define bigfr_fits_int(f)            mpfr_fits_sint_p(f, GMP_RNDN)
123 #define bigfr_fits_uint(f)           mpfr_fits_uint_p(f, GMP_RNDN)
124 #define bigfr_fits_long(f)           mpfr_fits_slong_p(f, GMP_RNDN)
125 #define bigfr_fits_ulong(f)          mpfr_fits_ulong_p(f, GMP_RNDN)
126
127 /***** Bigfr: converting assignments *****/
128 #define bigfr_set(f1,f2)             mpfr_set(f1, f2, GMP_RNDN)
129 #define bigfr_set_string(f,str,base) mpfr_set_str(f, str, base, GMP_RNDN)
130 #define bigfr_set_long(f,l)          mpfr_set_si(f, l, GMP_RNDN)
131 #define bigfr_set_ulong(f,l)         mpfr_set_ui(f, l, GMP_RNDN)
132 #if fpfloat_double_p
133 #define bigfr_set_fpfloat(f, d)         mpfr_set_d(f, d, GMP_RNDN)
134 #elif fpfloat_long_double_p
135 #define bigfr_set_fpfloat(f, d)         mpfr_set_ld(f, d, GMP_RNDN)
136 #endif
137 #define bigfr_set_bigz(f,b)          mpfr_set_z(f, b, GMP_RNDN)
138 #define bigfr_set_bigq(f,r)          mpfr_set_q(f, r, GMP_RNDN)
139 #define bigfr_set_bigf(f,r)          mpfr_set_f(f, r, GMP_RNDN)
140
141 #define bigfr_set_pinf(f)            mpfr_set_inf(f, 1)
142 #define bigfr_set_ninf(f)            mpfr_set_inf(f, -1)
143 #define bigfr_set_nan(f)             mpfr_set_nan(f)
144
145 #define bigfr_inf_p(f)               mpfr_inf_p(f)
146 #define bigfr_nan_p(f)               mpfr_nan_p(f)
147
148 /***** Bigfr: comparisons *****/
149 #define bigfr_cmp(f1,f2)             mpfr_cmp(f1, f2)
150 #define bigfr_lt(f1,f2)              mpfr_less_p(f1, f2)
151 #define bigfr_le(f1,f2)              mpfr_lessequal_p(f1, f2)
152 #define bigfr_eq(f1,f2)                 mpfr_equal_p(f1, f2)
153 #define bigfr_ge(f1,f2)              mpfr_greaterequal_p(f1, f2)
154 #define bigfr_gt(f1,f2)              mpfr_greater_p(f1, f2)
155 #define bigfr_ne(f1, f2)             mpfr_lessgreater_p(f1, f2)
156 #define bigfr_nc(f1, f2)             mpfr_unordered_p(f1, f2)
157
158 /***** Bigfr: arithmetic *****/
159 #define bigfr_neg(f,f2)              mpfr_neg(f, f2, GMP_RNDN)
160 #define bigfr_abs(f,f2)              mpfr_abs(f, f2, GMP_RNDN)
161 #define bigfr_add(res,f1,f2)         mpfr_add(res, f1, f2, GMP_RNDN)
162 #define bigfr_sub(res,f1,f2)         mpfr_sub(res, f1, f2, GMP_RNDN)
163 #define bigfr_mul(res,f1,f2)         mpfr_mul(res, f1, f2, GMP_RNDN)
164 #define bigfr_div(res,f1,f2)         mpfr_div(res, f1, f2, GMP_RNDN)
165
166 #define bigfr_rint(res,f)            mpfr_rint(res, f, GMP_RNDN)
167 #define bigfr_ceil(res,f)            mpfr_ceil(res, f)
168 #define bigfr_floor(res,f)           mpfr_floor(res, f)
169 #define bigfr_trunc(res,f)           mpfr_trunc(res, f)
170 #define bigfr_sqrt(res,f)            mpfr_sqrt(res, f, GMP_RNDN)
171 #define bigfr_sqrt_ui(res,f)         mpfr_sqrt_ui(res, f, GMP_RNDN)
172 #define bigfr_cbrt(res,f)            mpfr_cbrt(res, f, GMP_RNDN)
173 #define bigfr_root(res,f,rad)        mpfr_root(res, f, rad, GMP_RNDN)
174 #define bigfr_pow(res,f,exp)         mpfr_pow_ui(res, f, exp, GMP_RNDN)
175
176 /* Advanced functions */
177 #define bigfr_exp(res, f)            mpfr_exp(res, f, GMP_RNDN);
178 #define bigfr_exp2(res, f)           mpfr_exp2(res, f, GMP_RNDN);
179 #define bigfr_exp10(res, f)          mpfr_exp10(res, f, GMP_RNDN);
180
181 #define bigfr_log(res, f)            mpfr_log(res, f, GMP_RNDN);
182 #define bigfr_log2(res, f)           mpfr_log2(res, f, GMP_RNDN);
183 #define bigfr_log10(res, f)          mpfr_log10(res, f, GMP_RNDN);
184
185 #define bigfr_erf(res, f)            mpfr_erf(res, f, GMP_RNDN);
186 #define bigfr_erfc(res, f)           mpfr_erfc(res, f, GMP_RNDN);
187 #define bigfr_lgamma(res, f)         mpfr_lngamma(res, f, GMP_RNDN);
188
189 #define bigfr_cos(res, f)            mpfr_cos(res, f, GMP_RNDN);
190 #define bigfr_sin(res, f)            mpfr_sin(res, f, GMP_RNDN);
191 #define bigfr_tan(res, f)            mpfr_tan(res, f, GMP_RNDN);
192 #define bigfr_sec(res, f)            mpfr_sec(res, f, GMP_RNDN);
193 #define bigfr_csc(res, f)            mpfr_csc(res, f, GMP_RNDN);
194 #define bigfr_cot(res, f)            mpfr_cot(res, f, GMP_RNDN);
195
196 #define bigfr_acos(res, f)           mpfr_acos(res, f, GMP_RNDN);
197 #define bigfr_asin(res, f)           mpfr_asin(res, f, GMP_RNDN);
198 #define bigfr_atan(res, f)           mpfr_atan(res, f, GMP_RNDN);
199 #define bigfr_atan2(res, f, g)       mpfr_atan2(res, f, g, GMP_RNDN);
200
201 #define bigfr_cosh(res, f)           mpfr_cosh(res, f, GMP_RNDN);
202 #define bigfr_sinh(res, f)           mpfr_sinh(res, f, GMP_RNDN);
203 #define bigfr_tanh(res, f)           mpfr_tanh(res, f, GMP_RNDN);
204 #define bigfr_sech(res, f)           mpfr_sech(res, f, GMP_RNDN);
205 #define bigfr_csch(res, f)           mpfr_csch(res, f, GMP_RNDN);
206 #define bigfr_coth(res, f)           mpfr_coth(res, f, GMP_RNDN);
207
208 #define bigfr_acosh(res, f)          mpfr_acosh(res, f, GMP_RNDN);
209 #define bigfr_asinh(res, f)          mpfr_asinh(res, f, GMP_RNDN);
210 #define bigfr_atanh(res, f)          mpfr_atanh(res, f, GMP_RNDN);
211
212 extern Lisp_Object read_bigfr_string(char*);
213 extern Lisp_Object make_bigfr(fpfloat, unsigned long);
214 #if defined HAVE_MPF && defined WITH_GMP
215 extern Lisp_Object make_bigfr_bf(bigf);
216 #endif
217 extern Lisp_Object make_bigfr_bfr(bigfr);
218 extern Lisp_Object make_indef_bfr(bigfr);
219 extern_inline Lisp_Object ent_mpfr_wipe_indef(bigfr);
220
221 extern_inline Lisp_Object
222 ent_mpfr_wipe_indef(bigfr n)
223 {
224         if (!bigfr_nan_p(n) && !bigfr_inf_p(n))
225                 return make_bigfr_bfr(n);
226         else if (bigfr_nan_p(n))
227                 return make_indef(NOT_A_NUMBER);
228         else if (bigfr_inf_p(n)) {
229                 if (bigfr_sign(n) > 0)
230                         return make_indef(POS_INFINITY);
231                 else
232                         return make_indef(NEG_INFINITY);
233         }
234         return ent_nullop_zero(BIGFR_T);
235 }
236
237 extern void init_optables_BIGFR_T(void);
238 extern void init_ent_mpfr(void);
239 extern void vars_of_ent_mpfr(void);
240 extern void syms_of_ent_mpfr(void);
241
242 #endif /* INCLUDED_number_mpfr_h_ */