gmp_libmpz_import Method |
Namespace: Math.Gmp.Native
public static void mpz_import( mpz_t rop, size_t count, int order, size_t size, int endian, size_t nails, void_ptr op )
The parameters specify the format of the data. count many words are read, each size bytes. order can be 1 for most significant word first or -1 for least significant first. Within each word endian can be 1 for most significant byte first, -1 for least significant first, or 0 for the native endianness of the host CPU. The most significant nails bits of each word are skipped, this can be 0 to use the full words.
There is no sign taken from the data, rop will simply be a positive integer. An application can handle any sign itself, and apply it for instance with mpz_neg.
There are no data alignment restrictions on op, any address is allowed.
Here’s an example converting an array of unsigned long data, most significant element first, and host byte order within each value.
unsigned long a[20]; /* Initialize z and a */ mpz_import(z, 20, 1, sizeof(a[0]), 0, 0, a);
This example assumes the full sizeof bytes are used for data in the given type, which is usually true, and certainly true for unsigned long everywhere we know of. However on Cray vector systems it may be noted that short and int are always stored in 8 bytes (and with sizeof indicating that) but use only 32 or 46 bits. The nails feature can account for this, by passing for instance 8 * sizeof(int) - INT_BIT.
// Create, initialize, and set the value of rop to 0. mpz_t rop = new mpz_t(); gmp_lib.mpz_init(rop); // Copy 0x800000000000000000000001, 3 words of 4 bytes each, first word is lsb, and first byte in each word is msb. void_ptr data = gmp_lib.allocate(12); Marshal.Copy(new byte[] { 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 }, 0, data.ToIntPtr(), 12); // Import value into rop. gmp_lib.mpz_import(rop, 3, -1, 4, 1, 0, data); // Assert the value of rop. char_ptr value = gmp_lib.mpz_get_str(char_ptr.Zero, 16, rop); Assert.IsTrue(value.ToString() == "800000000000000000000001"); // Release unmanaged memory allocated for rop, data, and value. gmp_lib.mpz_clear(rop); gmp_lib.free(data); gmp_lib.free(value);