1 /* Copyright (C) 2004  The PARI group.
2 
3 This file is part of the PARI/GP package.
4 
5 PARI/GP is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 2 of the License, or (at your option) any later
8 version. It is distributed in the hope that it will be useful, but WITHOUT
9 ANY WARRANTY WHATSOEVER.
10 
11 Check the License for details. You should have received a copy of it, along
12 with the package; see the file 'COPYING'. If not, write to the Free Software
13 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
14 
15 /******************************************************************/
16 /*                                                                */
17 /*              PARI header file (common to all versions)         */
18 /*                                                                */
19 /******************************************************************/
20 #ifdef STMT_START /* perl headers */
21 #  undef STMT_START
22 #endif
23 #ifdef STMT_END
24 #  undef STMT_END
25 #endif
26 /* STMT_START { statements; } STMT_END;
27  * can be used as a single statement, as in
28  * if (x) STMT_START { ... } STMT_END; else ...
29  * [ avoid "dangling else" problem in macros ] */
30 #define STMT_START        do
31 #define STMT_END        while (0)
32 /*=====================================================================*/
33 /* pari_CATCH(numer) {
34  *   recovery
35  * } pari_TRY {
36  *   code
37  * } pari_ENDCATCH
38  * will execute 'code', then 'recovery' if exception 'numer' is thrown
39  * [ any exception if numer == CATCH_ALL ].
40  * pari_RETRY = as pari_TRY, but execute 'recovery', then 'code' again [still catching] */
41 
42 extern THREAD jmp_buf *iferr_env;
43 extern const long CATCH_ALL;
44 
45 #define pari_CATCH2(var,err) {         \
46   jmp_buf *var=iferr_env;    \
47   jmp_buf __env;             \
48   iferr_env = &__env;        \
49   if (setjmp(*iferr_env))    \
50   {                          \
51     GEN __iferr_data = pari_err_last(); \
52     iferr_env = var; \
53     if (err!=CATCH_ALL && err_get_num(__iferr_data) != err) \
54       pari_err(0, __iferr_data);
55 
56 #define pari_CATCH2_reset(var) (iferr_env = var)
57 #define pari_ENDCATCH2(var) iferr_env = var; } }
58 
59 #define pari_CATCH(err) pari_CATCH2(__iferr_old,err)
60 #define pari_RETRY } iferr_env = &__env; {
61 #define pari_TRY } else {
62 #define pari_CATCH_reset() pari_CATCH2_reset(__iferr_old)
63 #define pari_ENDCATCH pari_ENDCATCH2(__iferr_old)
64 
65 #define pari_APPLY_same(EXPR)\
66   { \
67     long i, _l; \
68     GEN _y = cgetg_copy(x, &_l);\
69     for (i=1; i<_l; i++) gel(_y,i) = EXPR;\
70     return _y;\
71   }
72 
73 #define pari_APPLY_type(TYPE, EXPR)\
74   { \
75     long i, _l = lg(x); \
76     GEN _y = cgetg(_l, TYPE);\
77     for (i=1; i<_l; i++) gel(_y,i) = EXPR;\
78     return _y;\
79   }
80 
81 #define pari_APPLY_long(EXPR)\
82   { \
83     long i, _l = lg(x); \
84     GEN _y = cgetg(_l, t_VECSMALL);\
85     for (i=1; i<_l; i++) _y[i] = EXPR;\
86     return _y;\
87   }
88 
89 #define pari_APPLY_ulong(EXPR)\
90   { \
91     long i, _l = lg(x); \
92     GEN _y = cgetg(_l, t_VECSMALL);\
93     for (i=1; i<_l; i++) ((ulong*)_y)[i] = EXPR;\
94     return _y;\
95   }
96 
97 extern const double LOG10_2, LOG2_10;
98 #ifndef  M_PI
99 #  define M_PI 3.14159265358979323846264338327950288
100 #endif
101 #ifndef  M_LN2
102 #  define M_LN2 0.693147180559945309417232121458176568
103 #endif
104 
105 /* Common global variables: */
106 extern int new_galois_format, factor_add_primes, factor_proven;
107 extern ulong DEBUGFILES, DEBUGLEVEL, DEBUGMEM, precdl;
108 extern long DEBUGVAR;
109 extern ulong pari_mt_nbthreads;
110 extern THREAD GEN  zetazone, bernzone, primetab;
111 extern GEN gen_m1,gen_1,gen_2,gen_m2,ghalf,gen_0,gnil,err_e_STACK;
112 extern THREAD VOLATILE int PARI_SIGINT_block, PARI_SIGINT_pending;
113 
114 extern const long lontyp[];
115 extern void (*cb_pari_ask_confirm)(const char *);
116 extern void (*cb_pari_init_histfile)(void);
117 extern int  (*cb_pari_whatnow)(PariOUT *out, const char *, int);
118 extern void (*cb_pari_quit)(long);
119 extern void (*cb_pari_sigint)(void);
120 extern int (*cb_pari_handle_exception)(long);
121 extern int (*cb_pari_err_handle)(GEN);
122 extern void (*cb_pari_pre_recover)(long);
123 extern void (*cb_pari_err_recover)(long);
124 extern int (*cb_pari_break_loop)(int);
125 extern int (*cb_pari_is_interactive)(void);
126 extern void (*cb_pari_start_output)(void);
127 extern const char *pari_library_path;
128 extern THREAD long *varpriority;
129 
130 /* pari_init_opts */
131 enum {
132   INIT_JMPm = 1,
133   INIT_SIGm = 2,
134   INIT_DFTm = 4,
135   INIT_noPRIMEm = 8,
136   INIT_noIMTm = 16,
137   INIT_noINTGMPm = 32
138 };
139 
140 #ifndef HAS_EXP2
141 #  undef exp2
142 #  define exp2(x) (exp((double)(x)*M_LN2))
143 #endif
144 #ifndef HAS_LOG2
145 #  undef log2
146 #  define log2(x) (log((double)(x))/M_LN2)
147 #endif
148 
149 #define ONLY_REM ((GEN*)0x1L)
150 #define ONLY_DIVIDES ((GEN*)0x2L)
151 
152 #define NEXT_PRIME_VIADIFF(p,d) STMT_START { (p) += *(d)++; } STMT_END
153 #define PREC_PRIME_VIADIFF(p,d) STMT_START { (p) -= *--(d); } STMT_END
154 #define NEXT_PRIME_VIADIFF_CHECK(p,d)  STMT_START \
155   { if (!*(d)) pari_err_MAXPRIME(0); NEXT_PRIME_VIADIFF(p,d); } STMT_END
156