42#define FAIL(E) return (imath_errno = (E), false)
48 if ((res_ = (X)) != (V)) { \
52#define CHECK(X) VCHECK(X, MP_OK)
53#define ECHECK(X) VCHECK(X, expect)
61#define OUTPUT_LIMIT 2048
63#define OTHER_ERROR -1024
197 *z = strtol(
str, &end, radix);
198 return (end !=
str && *end ==
'\0');
206 if (rval != NULL) *rval =
MP_OK;
209 for (
int i = 0; i <
t->num_inputs; ++i) {
215 int k =
abs(atoi(
str + 1)) - 1;
217 if (k < 0 || k >= i) {
218 fprintf(stderr,
"Line %d: Invalid input back-reference [%s]\n",
228 fprintf(stderr,
"Line %d: Invalid input value [%s]\n",
t->line,
str);
237 for (
int i = 0; i <
t->num_outputs; ++i) {
244 if (strcmp(
str,
"?") == 0)
246 else if (*
str ==
'$') {
250 fprintf(stderr,
"Line %d: Invalid result code [%s]\n",
t->line,
str);
252 }
else if (rval == NULL) {
253 fprintf(stderr,
"Line %d: Result code not permitted here [%s]\n",
262 fprintf(stderr,
"Line %d: Invalid output value [%s]\n",
t->line,
str);
266 if (out != NULL) out[i] = reg;
277 if (rval != NULL) *rval =
MP_OK;
280 for (
int i = 0; i <
t->num_inputs; ++i) {
286 int k =
abs(atoi(
str + 1)) - 1;
288 if (k < 0 || k >= i) {
289 fprintf(stderr,
"Line %d: Invalid input back-reference [%s]\n",
299 fprintf(stderr,
"Line %d: Invalid input value [%s]\n",
t->line,
str);
308 for (
int i = 0; i <
t->num_outputs; ++i) {
315 if (strcmp(
str,
"?") == 0)
317 else if (*
str ==
'$') {
321 fprintf(stderr,
"Line %d: Invalid result code [%s]\n",
t->line,
str);
323 }
else if (rval == NULL) {
324 fprintf(stderr,
"Line %d: Result code not permitted here [%s]\n",
333 fprintf(stderr,
"Line %d: Invalid output value [%s]\n",
t->line,
str);
337 if (out != NULL) out[i] = reg;
351 }
else if (strcmp(
str + 1,
"MP_OK") == 0 ||
352 strcmp(
str + 1,
"MP_FALSE") == 0) {
354 }
else if (strcmp(
str + 1,
"MP_TRUE") == 0) {
356 }
else if (strcmp(
str + 1,
"MP_MEMORY") == 0) {
358 }
else if (strcmp(
str + 1,
"MP_RANGE") == 0) {
360 }
else if (strcmp(
str + 1,
"MP_UNDEF") == 0) {
362 }
else if (strcmp(
str + 1,
"MP_TRUNC") == 0) {
364 }
else if (strcmp(
str + 1,
"MP_ROUND_UP") == 0) {
366 }
else if (strcmp(
str + 1,
"MP_ROUND_DOWN") == 0) {
368 }
else if (strcmp(
str + 1,
"MP_ROUND_HALF_UP") == 0) {
370 }
else if (strcmp(
str + 1,
"MP_ROUND_HALF_DOWN") == 0) {
386 for (tok = strtok(
str,
"."); tok != NULL &&
pos < limit;
387 tok = strtok(NULL,
".")) {
390 if (!
read_long(&v, tok) || v > UCHAR_MAX || v < 0)
return -1;
392 buf[
pos++] = (
unsigned char)v;
412 static int is_done = 0;
416 for (
int i = 0; i <
NUM_REGS; ++i) {
428 for (
int i = 0; i <
NUM_REGS; ++i) {
442 if (strcmp(
t->code,
"initu") == 0) {
466 if (strcmp(
t->code,
"setu") == 0) {
519 if (strcmp(
t->code,
"addv") == 0) {
541 if (strcmp(
t->code,
"subv") == 0) {
669 if (expect ==
MP_OK &&
676 sprintf(
str,
"%ld", rem);
765 if (expect !=
MP_OK)
return true;
904 sprintf(
g_output,
"Incorrect comparison result (want %d, got %d)", expect,
918 sprintf(
g_output,
"Incorrect comparison result (want %d, got %d)", expect,
932 sprintf(
g_output,
"Incorrect comparison result (want %d, got %d)", expect,
946 v = atoi(
t->input[1]);
948 sprintf(
g_output,
"Incorrect comparison result (want %d, got %d)", expect,
962 v = strtoul(
t->input[1], NULL, 0);
964 sprintf(
g_output,
"Incorrect comparison result (want %d, got %d)", expect,
993 int test_len, out_len;
1002 sprintf(
g_output,
"Output lengths do not match (want %d, got %d)", test_len,
1012 for (i = 0; i < test_len - 1; ++i)
1030 sprintf(
g_output,
"Incorrect value (got %ld)", v);
1045 sprintf(
g_output,
"Incorrect value (got %lu)", v);
1073 int test_len, out_len;
1082 sprintf(
g_output,
"Output lengths do not match (want %d, got %d)", test_len,
1092 for (i = 0; i < test_len - 1; ++i)
1122 mp_int *in = NULL, *out = NULL;
1126 if (
t->num_inputs > 0) {
1127 in = calloc(
t->num_inputs,
sizeof(
mp_int));
1129 if (
t->num_outputs > 0) {
1130 out = calloc(
t->num_outputs,
sizeof(
mp_int));
1134 if (in != NULL) free(in);
1135 if (out != NULL) free(out);
1139 fprintf(ofp,
"Test '%s' defined at line %d\n",
t->code,
t->line);
1140 fprintf(ofp,
"Expected result: %d\n", expect);
1141 fprintf(ofp,
"Input values: %d\n",
t->num_inputs);
1142 for (i = 0; i <
t->num_inputs; ++i) {
1145 fprintf(ofp,
" %2d.) %s", i + 1,
g_output);
1147 for (j = i - 1; j >= 0; --j)
1148 if (in[j] == in[i]) {
1149 fprintf(ofp,
" (=> %d)", j + 1);
1155 fprintf(ofp,
"Output values: %d\n",
t->num_outputs);
1156 for (i = 0; i <
t->num_outputs; ++i) {
1159 fprintf(ofp,
" %2d.) %s\n", i + 1,
g_output);
1161 if (in != NULL) free(in);
1162 if (out != NULL) free(out);
1272 "Line %d: Second argument must be an integer (test_qaddz)\n",
1294 "Line %d: Second argument must be an integer (test_qsubz)\n",
1316 "Line %d: Second argument must be an integer (test_qmulz)\n",
1338 "Line %d: Second argument must be an integer (test_qdivz)\n",
1361 "Line %d: Second argument must be an integer (test_qexpt)\n",
1386 fprintf(stderr,
"Line %d: Radix %ld out of range\n",
t->line, radix);
1402 long radix, prec,
m;
1409 fprintf(stderr,
"Line %d: Invalid result code [%s]\n",
t->line,
1423 fprintf(stderr,
"Line %d: Precision %ld out of range\n",
t->line, prec);
void GMPZAPI() abs(mp_int rop, mp_int op)
static mp_result mp_int_sqrt(mp_int a, mp_int c)
Sets c to the greatest integer not less than the square root of a.
static mp_int MP_NUMER_P(mp_rat Q)
bool test_qtodec(testspec_t *t, FILE *ofp)
bool test_qsubz(testspec_t *t, FILE *ofp)
bool test_divv(testspec_t *t, FILE *ofp)
static mpq_t g_qreg[NUM_REGS]
bool test_mod(testspec_t *t, FILE *ofp)
bool test_sqrt(testspec_t *t, FILE *ofp)
bool test_abs(testspec_t *t, FILE *ofp)
bool test_comp(testspec_t *t, FILE *ofp)
static unsigned char g_bin1[OUTPUT_LIMIT]
bool test_div(testspec_t *t, FILE *ofp)
bool test_read_uns(testspec_t *t, FILE *ofp)
bool test_mul(testspec_t *t, FILE *ofp)
bool test_qaddz(testspec_t *t, FILE *ofp)
bool test_exptf(testspec_t *t, FILE *ofp)
bool test_exptmod_ev(testspec_t *t, FILE *ofp)
bool test_qdivz(testspec_t *t, FILE *ofp)
bool test_qdiv(testspec_t *t, FILE *ofp)
bool test_set(testspec_t *t, FILE *ofp)
static bool read_long(long *z, char *str)
bool test_vcomp(testspec_t *t, FILE *ofp)
static unsigned char g_bin2[OUTPUT_LIMIT]
static void done_testing(void)
bool test_invmod(testspec_t *t, FILE *ofp)
bool test_zcomp(testspec_t *t, FILE *ofp)
bool test_qmul(testspec_t *t, FILE *ofp)
static bool parse_int_values(testspec_t *t, mp_int *in, mp_int *out, mp_result *rval)
static mp_result read_int_value(mp_int z, char *str)
bool test_sqr(testspec_t *t, FILE *ofp)
bool test_init(testspec_t *t, FILE *ofp)
bool test_add(testspec_t *t, FILE *ofp)
bool test_qadd(testspec_t *t, FILE *ofp)
static mp_result read_rat_value(mp_rat q, char *str)
bool test_expt(testspec_t *t, FILE *ofp)
bool test_gcd(testspec_t *t, FILE *ofp)
bool test_neg(testspec_t *t, FILE *ofp)
static mpz_t g_zreg[NUM_REGS]
bool test_qtostr(testspec_t *t, FILE *ofp)
bool test_exptmod_bv(testspec_t *t, FILE *ofp)
bool test_to_uint(testspec_t *t, FILE *ofp)
bool test_read_binary(testspec_t *t, FILE *ofp)
bool test_ucomp(testspec_t *t, FILE *ofp)
bool test_lcm(testspec_t *t, FILE *ofp)
bool test_egcd(testspec_t *t, FILE *ofp)
bool test_qrecip(testspec_t *t, FILE *ofp)
bool test_tostr(testspec_t *t, FILE *ofp)
bool test_mulv(testspec_t *t, FILE *ofp)
static char g_output[OUTPUT_LIMIT]
void trim_line(char *line)
Removes leading and trailing whitespace from a zero-terminated line.
bool test_qexpt(testspec_t *t, FILE *ofp)
bool test_qmulz(testspec_t *t, FILE *ofp)
bool test_meta(testspec_t *t, FILE *ofp)
bool test_uvcomp(testspec_t *t, FILE *ofp)
bool test_divp2(testspec_t *t, FILE *ofp)
bool test_to_int(testspec_t *t, FILE *ofp)
bool test_qrdec(testspec_t *t, FILE *ofp)
static bool parse_result_code(char *str, mp_result *code)
bool test_qneg(testspec_t *t, FILE *ofp)
bool test_mulp2(testspec_t *t, FILE *ofp)
static bool parse_rat_values(testspec_t *t, mp_rat *in, mp_rat *out, mp_result *rval)
bool test_qabs(testspec_t *t, FILE *ofp)
static int parse_binary(char *str, unsigned char *buf, int limit)
bool test_root(testspec_t *t, FILE *ofp)
bool test_sub(testspec_t *t, FILE *ofp)
bool test_qsub(testspec_t *t, FILE *ofp)
bool test_is_prime(testspec_t *t, FILE *OFP)
bool test_tobin(testspec_t *t, FILE *ofp)
bool test_exptmod(testspec_t *t, FILE *ofp)
bool test_exptv(testspec_t *t, FILE *ofp)
bool test_to_uns(testspec_t *t, FILE *ofp)
void reset_registers(void)
mp_result mp_int_is_prime(mp_int z)
static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
static isl_stat power(__isl_take isl_map *map, void *user)
#define mp_rat_is_integer
#define mp_rat_string_len
#define mp_int_read_binary
#define mp_rat_read_string
#define mp_int_compare_zero
#define mp_rat_to_decimal
#define mp_int_string_len
#define mp_int_compare_unsigned
#define mp_int_unsigned_len
#define mp_int_compare_uvalue
#define mp_int_to_unsigned
#define mp_int_exptmod_evalue
#define mp_int_read_string
#define mp_int_init_value
#define mp_int_read_unsigned
#define mp_int_expt_value
#define mp_rat_read_decimal
#define mp_int_set_uvalue
#define mp_int_compare_value
#define mp_int_exptmod_bvalue
#define mp_int_init_uvalue
#define mp_int_binary_len
#define mp_rat_decimal_len