File: wrappers.c

package info (click to toggle)
haskell-hgmp 0.1.2.1-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 184 kB
  • sloc: haskell: 913; ansic: 16; makefile: 6
file content (18 lines) | stat: -rw-r--r-- 619 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <assert.h>
#include <HsFFI.h>
#include <ghc-gmp.h>

void mpz_set_HsInt(mpz_ptr dst, const HsInt n) {
  if (sizeof(HsInt) == sizeof(signed long int)) {
    mpz_set_si(dst, n);
  } else if (sizeof(HsInt) == sizeof(signed long int) + sizeof(unsigned long int)) {
    // Win64, see comments in integer-gmp/src/GHC/Integer/Type.hs
#define lobits (8 * sizeof(unsigned long int))
#define lomask ((1ULL << lobits) - 1)
    mpz_set_si(dst, n >> lobits); // warns when branch will be unused
    mpz_mul_2exp(dst, dst, lobits);
    mpz_add_ui(dst, dst, n & lomask);
  } else {
    assert(! "supported HsInt size");
  }
}