Created
September 14, 2012 13:20
-
-
Save aycabta/3721855 to your computer and use it in GitHub Desktop.
test to curry up with GMP low-level functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <limits.h> | |
#include <gmp.h> | |
#define NUM32BIT 4294967296L | |
int main(void) | |
{ | |
mpz_t mpz_i; | |
mpz_t step; | |
mpz_t end; | |
mpz_t next; | |
mpz_t zero; | |
mp_limb_t cy; | |
mpz_init(mpz_i); | |
mpz_i->_mp_size = 0; | |
mpz_i->_mp_alloc = 1; | |
mpz_init(next); | |
mpz_set_ui(next, 0L); | |
mpz_init(zero); | |
mpz_set_ui(zero, 0L); | |
mpz_init(step); | |
mpz_init(end); | |
mpz_set_ui(step, (unsigned long long)(ULONG_LONG_MAX / 10)); | |
mpz_set_ui(end, ULONG_LONG_MAX); | |
mpz_add(end, end, step); | |
mpz_add(end, end, step); | |
mpz_set(next, step); | |
while (mpz_cmp(mpz_i, end) <= 0) { | |
if (mpz_i->_mp_alloc < mpz_i->_mp_size + 1) { | |
_mpz_realloc(mpz_i, mpz_i->_mp_size + 1); | |
} | |
if (mpz_i->_mp_size == 0) { | |
mpz_i->_mp_size = 1; | |
mpz_i->_mp_d[0] = 1; | |
} else { | |
cy = mpn_add_1(mpz_i->_mp_d, mpz_i->_mp_d, mpz_i->_mp_size, (mp_limb_t)NUM32BIT); | |
if (cy == 1) { | |
gmp_printf("curry up!\n\n"); | |
mpz_i->_mp_d[mpz_i->_mp_size] = 1; | |
mpz_i->_mp_size++; | |
mpz_i->_mp_d[mpz_i->_mp_size] = 0; | |
} | |
} | |
if (mpz_cmp(mpz_i, next) >= 0) { | |
gmp_printf("%llu\n", ULONG_LONG_MAX); | |
gmp_printf("%Zd\n\n", mpz_i); | |
mpz_add(next, next, step); | |
} | |
} | |
gmp_printf("reverse!\n"); | |
mpz_sub(next, next, step); | |
while (mpz_cmp(mpz_i, zero) > 0) { | |
cy = mpn_sub_1(mpz_i->_mp_d, mpz_i->_mp_d, mpz_i->_mp_size, (mp_limb_t)NUM32BIT); | |
if (cy == 1) { | |
gmp_printf("curry down!\n\n"); | |
mpz_i->_mp_d[mpz_i->_mp_size] = 0; | |
mpz_i->_mp_size--; | |
mpz_i->_mp_d[mpz_i->_mp_size] = 0; | |
} | |
if (mpz_cmp(mpz_i, next) <= 0) { | |
gmp_printf("%llu\n", ULONG_LONG_MAX); | |
gmp_printf("%Zd\n\n", mpz_i); | |
mpz_sub(next, next, step); | |
} | |
} | |
mpz_clear(mpz_i); | |
mpz_clear(step); | |
mpz_clear(end); | |
mpz_clear(next); | |
mpz_clear(zero); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment