1:  // Author & Copyright : Peter Luschny
   2:  // Created: 2010-01-15
   3:  // License: LGPL version 2.1 or (at your option)
   4:  // Creative Commons Attribution-ShareAlike 3.0
   5:   
   6:  // Adapter for library functions.
   7:   
   8:  #ifndef LMP_H_
   9:  #define LMP_H_
  10:   
  11:  #include <stdlib.h>
  12:   
  13:  #include "mpir.h"
  14:  //#include "gmp.h"
  15:   
  16:  typedef mpz_t Xint;
  17:  typedef long int slong;
  18:  typedef unsigned long int ulong;
  19:   
  20:  class lmp {
  21:  public:
  22:   
  23:      static void Init(Xint res) {
  24:          mpz_init(res);
  25:      }
  26:      static void SetUi(Xint res, ulong n) {
  27:          mpz_set_ui(res, n);
  28:      }
  29:      static void InitSetUi(Xint res, ulong n) {
  30:          mpz_init_set_ui(res, n);
  31:      }
  32:      static void MulUi(Xint res, Xint op1, ulong op2) {
  33:          mpz_mul_ui(res, op1, op2);
  34:      }
  35:      static void Mul(Xint res, Xint op1, Xint op2) {
  36:          mpz_mul(res, op1, op2);
  37:      }
  38:      // Assuming that mul detects the special case of squaring.
  39:      // Otherwise the use of res = pow(bas, 2) might be better.
  40:      static void Pow2(Xint res, Xint bas) {
  41:          mpz_mul(res, bas, bas);
  42:      }
  43:      static void Mul2Exp(Xint res, Xint op1, slong op2) {
  44:          mpz_mul_2exp(res, op1, op2);
  45:      }
  46:      static void FacUi(Xint res, ulong n) {
  47:          mpz_fac_ui(res, n);
  48:      }
  49:      static slong Cmp(Xint op1, Xint op2) {
  50:          return mpz_cmp(op1, op2);
  51:      }
  52:      static void Clear(Xint b) {
  53:          mpz_clear(b);
  54:      }
  55:      static slong* Malloc(ulong card) {
  56:          return (slong*) (malloc)(card * sizeof(slong));
  57:      }
  58:      static ulong* MallocUi(ulong card) {
  59:          return (ulong*) (malloc)(card * sizeof(ulong));
  60:      }
  61:      static void Free(slong* list, ulong card) {
  62:          free(list);
  63:      }
  64:      static void FreeUi(ulong* list, ulong card) {
  65:          free(list);
  66:      }
  67:  };
  68:   
  69:  #endif // LMP_H_