#ifndef SAGE_LIBS_PARI_MISC_H
#define SAGE_LIBS_PARI_MISC_H
#include <pari/pari.h>
#include "interrupt.h"
#define _pari_sig_on() sig_on(); _pari_catch;
#define _pari_sig_str(s) sig_str(s); _pari_catch;
#define _pari_sig_off() _pari_endcatch; sig_off();
inline int strcmp_to_cmp(int f) {
if (f > 0) {
return 1;
} else if (f) {
return -1;
} else {
return 0;
}
}
inline int
gcmp_sage(GEN x, GEN y)
{
long tx = typ(x), ty = typ(y), f;
GEN tmp;
pari_sp av;
if (is_intreal_t(tx) && is_intreal_t(ty)) {
return mpcmp(x,y);
}
if (tx==t_STR) {
if (ty != t_STR) {
return 1;
}
return strcmp_to_cmp(strcmp(GSTR(x),GSTR(y)));
}
if (ty == t_STR)
return -1;
av = avma;
char *c, *d;
c = GENtostr(x);
d = GENtostr(y);
f = strcmp_to_cmp(strcmp(c, d));
free(c);
free(d);
avma = av;
return f;
}
int factorint_withproof_sage(GEN* ans, GEN x, GEN cutoff) {
GEN F = factorint(x, 0);
*ans = F;
long i, l;
if (lg(F) == 1) return F;
F = gel(F,1); l = lg(F);
for (i = 1; i < l; i++) {
GEN p = gel(F,i);
if (mpcmp(p, cutoff) > 0 && !isprime(p)) {
char *c, *d;
c = GENtostr(x);
d = GENtostr(p);
fprintf(stderr, "***\nPARI's factor(%s): Found composite pseudoprime %s (very rare and exciting -- PLEASE REPORT!!)\n***\n",
c, d);
fprintf(stderr, "Do not worry, SAGE will further factor the number until each factor is proven prime.\n");
free(c);
free(d);
return 1;
}
}
return 0;
}
#endif