Browse Source

Test

etomic
jl777 7 years ago
parent
commit
f19966e28f
  1. 165
      iguana/mini-gmp.c

165
iguana/mini-gmp.c

@ -2836,89 +2836,90 @@ static int gmp_detect_endian (void)
void *mpz_export (void *r, size_t *countp, int order, size_t size, int endian,size_t nails, const mpz_t u) void *mpz_export (void *r, size_t *countp, int order, size_t size, int endian,size_t nails, const mpz_t u)
{ {
size_t count; size_t count;
mp_size_t un; mp_size_t un;
if (nails != 0) if (nails != 0)
gmp_die ("mpz_import: Nails not supported."); gmp_die ("mpz_import: Nails not supported.");
assert (order == 1 || order == -1); assert (order == 1 || order == -1);
assert (endian >= -1 && endian <= 1); assert (endian >= -1 && endian <= 1);
assert (size > 0 || u->_mp_size == 0); assert (size > 0 || u->_mp_size == 0);
un = u->_mp_size; un = u->_mp_size;
count = 0; count = 0;
if (un != 0) if (un != 0)
{ {
size_t k; size_t k;
uint8_t *p; uint8_t *p;
ptrdiff_t word_step; ptrdiff_t word_step;
/* The current (partial) limb. */ /* The current (partial) limb. */
mp_limb_t limb; mp_limb_t limb;
/* The number of bytes left to to in this limb. */ /* The number of bytes left to to in this limb. */
size_t bytes; size_t bytes;
/* The index where the limb was read. */ /* The index where the limb was read. */
mp_size_t i; mp_size_t i;
un = GMP_ABS (un); un = GMP_ABS (un);
/* Count bytes in top limb. */ /* Count bytes in top limb. */
limb = u->_mp_d[un-1]; limb = u->_mp_d[un-1];
assert (limb != 0); assert (limb != 0);
k = 0; k = 0;
do { do {
k++; limb >>= CHAR_BIT; k++; limb >>= CHAR_BIT;
} while (limb != 0); } while (limb != 0);
count = (k + (un-1) * sizeof (mp_limb_t) + size - 1) / size; count = (k + (un-1) * sizeof (mp_limb_t) + size - 1) / size;
if (!r) if (!r)
r = malloc (count * size); r = malloc (count * size);
if (endian == 0) if (endian == 0)
endian = gmp_detect_endian (); endian = gmp_detect_endian ();
p = (uint8_t *) r; p = (uint8_t *) r;
word_step = (order != endian) ? 2 * size : 0; word_step = (order != endian) ? 2 * size : 0;
/* Process bytes from the least significant end, so point p at the /* Process bytes from the least significant end, so point p at the
least significant word. */ least significant word. */
if (order == 1) if (order == 1)
{ {
p += size * (count - 1); p += size * (count - 1);
word_step = - word_step; word_step = - word_step;
} }
/* And at least significant byte of that word. */ /* And at least significant byte of that word. */
if (endian == 1) if (endian == 1)
p += (size - 1); p += (size - 1);
for (bytes = 0, i = 0, k = 0; k < count; k++, p += word_step) for (bytes = 0, i = 0, k = 0; k < count; k++, p += word_step)
{ {
size_t j; size_t j;
for (j = 0; j < size; j++, p -= (ptrdiff_t) endian) for (j = 0; j < size; j++, p -= (ptrdiff_t) endian)
{ {
if (bytes == 0) if (bytes == 0)
{ {
if (i < un) if (i < un)
limb = u->_mp_d[i++]; limb = u->_mp_d[i++];
bytes = sizeof (mp_limb_t); bytes = sizeof (mp_limb_t);
} }
*p = limb; *p = limb;
limb >>= CHAR_BIT; printf("{%02x} ",*p);
bytes--; limb >>= CHAR_BIT;
} bytes--;
} }
assert (i == un); }
assert (k == count); assert (i == un);
assert (k == count);
} }
if (countp) if (countp)
*countp = count; *countp = count;
printf("mpz_export.%d\n",(int32_t)count);
return r; return r;
} }
///////////////////////////////// /////////////////////////////////

Loading…
Cancel
Save