1/* 2 Name: gmp_compat.h 3 Purpose: Provide GMP compatiable routines for imath library 4 Author: David Peixotto 5 6 Copyright (c) 2012 Qualcomm Innovation Center, Inc. All rights reserved. 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 SOFTWARE. 25 */ 26 27#ifndef IMATH_GMP_COMPAT_H_ 28#define IMATH_GMP_COMPAT_H_ 29#include "imath.h" 30#include "imrat.h" 31#include <stddef.h> 32 33#define GMPZAPI(fun) impz_ ## fun 34#define GMPQAPI(fun) impq_ ## fun 35 36#ifdef __cplusplus 37extern "C" { 38#endif 39/************************************************************************* 40 * 41 * Functions with direct translations 42 * 43 *************************************************************************/ 44/* gmp: mpq_clear */ 45void GMPQAPI(clear)(mp_rat x); 46 47/* gmp: mpq_cmp */ 48int GMPQAPI(cmp)(mp_rat op1, mp_rat op2); 49 50/* gmp: mpq_init */ 51void GMPQAPI(init)(mp_rat x); 52 53/* gmp: mpq_mul */ 54void GMPQAPI(mul)(mp_rat product, mp_rat multiplier, mp_rat multiplicand); 55 56/* gmp: mpq_set */ 57void GMPQAPI(set)(mp_rat rop, mp_rat op); 58 59/* gmp: mpz_abs */ 60void GMPZAPI(abs)(mp_int rop, mp_int op); 61 62/* gmp: mpz_add */ 63void GMPZAPI(add)(mp_int rop, mp_int op1, mp_int op2); 64 65/* gmp: mpz_clear */ 66void GMPZAPI(clear)(mp_int x); 67 68/* gmp: mpz_cmp_si */ 69int GMPZAPI(cmp_si)(mp_int op1, long op2); 70 71/* gmp: mpz_cmpabs */ 72int GMPZAPI(cmpabs)(mp_int op1, mp_int op2); 73 74/* gmp: mpz_cmp */ 75int GMPZAPI(cmp)(mp_int op1, mp_int op2); 76 77/* gmp: mpz_init */ 78void GMPZAPI(init)(mp_int x); 79 80/* gmp: mpz_mul */ 81void GMPZAPI(mul)(mp_int rop, mp_int op1, mp_int op2); 82 83/* gmp: mpz_neg */ 84void GMPZAPI(neg)(mp_int rop, mp_int op); 85 86/* gmp: mpz_set_si */ 87void GMPZAPI(set_si)(mp_int rop, long op); 88 89/* gmp: mpz_set */ 90void GMPZAPI(set)(mp_int rop, mp_int op); 91 92/* gmp: mpz_sub */ 93void GMPZAPI(sub)(mp_int rop, mp_int op1, mp_int op2); 94 95/* gmp: mpz_swap */ 96void GMPZAPI(swap)(mp_int rop1, mp_int rop2); 97 98/* gmp: mpq_sgn */ 99int GMPQAPI(sgn)(mp_rat op); 100 101/* gmp: mpz_sgn */ 102int GMPZAPI(sgn)(mp_int op); 103 104/* gmp: mpq_set_ui */ 105void GMPQAPI(set_ui)(mp_rat rop, unsigned long op1, unsigned long op2); 106 107/* gmp: mpz_set_ui */ 108void GMPZAPI(set_ui)(mp_int rop, unsigned long op); 109 110/* gmp: mpq_den_ref */ 111mp_int GMPQAPI(denref)(mp_rat op); 112 113/* gmp: mpq_num_ref */ 114mp_int GMPQAPI(numref)(mp_rat op); 115 116/* gmp: mpq_canonicalize */ 117void GMPQAPI(canonicalize)(mp_rat op); 118 119/************************************************************************* 120 * 121 * Functions that can be implemented as a combination of imath functions 122 * 123 *************************************************************************/ 124/* gmp: mpz_addmul */ 125void GMPZAPI(addmul)(mp_int rop, mp_int op1, mp_int op2); 126 127/* gmp: mpz_divexact */ 128void GMPZAPI(divexact)(mp_int q, mp_int n, mp_int d); 129 130/* gmp: mpz_divisible_p */ 131int GMPZAPI(divisible_p)(mp_int n, mp_int d); 132 133/* gmp: mpz_submul */ 134void GMPZAPI(submul)(mp_int rop, mp_int op1, mp_int op2); 135 136/* gmp: mpz_add_ui */ 137void GMPZAPI(add_ui)(mp_int rop, mp_int op1, unsigned long op2); 138 139/* gmp: mpz_divexact_ui */ 140void GMPZAPI(divexact_ui)(mp_int q, mp_int n, unsigned long d); 141 142/* gmp: mpz_mul_ui */ 143void GMPZAPI(mul_ui)(mp_int rop, mp_int op1, unsigned long op2); 144 145/* gmp: mpz_pow_ui */ 146void GMPZAPI(pow_ui)(mp_int rop, mp_int base, unsigned long exp); 147 148/* gmp: mpz_sub_ui */ 149void GMPZAPI(sub_ui)(mp_int rop, mp_int op1, unsigned long op2); 150 151/* gmp: mpz_fdiv_q_ui */ 152unsigned long GMPZAPI(fdiv_q_ui)(mp_int q, mp_int n, unsigned long d); 153 154/* gmp: mpz_sizeinbase */ 155size_t GMPZAPI(sizeinbase)(mp_int op, int base); 156 157/************************************************************************* 158 * 159 * Functions with different behavior in corner cases 160 * 161 *************************************************************************/ 162/* gmp: mpz_gcd */ 163/* gmp: When op1 = 0 and op2 = 0, return 0.*/ 164void GMPZAPI(gcd)(mp_int rop, mp_int op1, mp_int op2); 165 166/* gmp: mpz_get_str */ 167/* gmp: If str is NULL then allocate space using the default allocator. */ 168char* GMPZAPI(get_str)(char *str, int radix, mp_int op); 169 170/* gmp: mpq_get_str */ 171/* gmp: If str is NULL then allocate space using the default allocator. */ 172/* gmp: If value is a whole number do not print denomenator. */ 173/* TODO: Need to handle 0 values better. GMP prints 0/4 instead of 0.*/ 174char* GMPQAPI(get_str)(char *str, int radix, mp_rat op); 175 176/* gmp: mpz_set_str */ 177/* gmp: Allow and ignore spaces in string. */ 178int GMPZAPI(set_str)(mp_int rop, char *str, int base); 179 180/* gmp: mpq_set_str */ 181int GMPQAPI(set_str)(mp_rat rop, char *str, int base); 182 183/* gmp: mpz_get_ui */ 184/* gmp: Return least significant bits if value is too big for a long. */ 185unsigned long GMPZAPI(get_ui)(mp_int op); 186 187/* gmp: mpz_get_si */ 188/* gmp: Return least significant bits if value is too bit for a long. */ 189/* gmp: If value is too big for long, return the least significant 190 (8*sizeof(long)-1) bits from the op and set the sign bit according to 191 the sign of the op. */ 192long GMPZAPI(get_si)(mp_int op); 193 194/* gmp: mpz_lcm */ 195/* gmp: When op1 = 0 or op2 = 0, return 0.*/ 196/* gmp: The resutl of lcm(a,b) is always positive. */ 197void GMPZAPI(lcm)(mp_int rop, mp_int op1, mp_int op2); 198 199/* gmp: mpz_mul_2exp */ 200/* gmp: allow big values for op2 when op1 == 0 */ 201void GMPZAPI(mul_2exp)(mp_int rop, mp_int op1, unsigned long op2); 202 203/************************************************************************* 204 * 205 * Functions needing expanded functionality 206 * 207 *************************************************************************/ 208/* gmp: mpz_cdiv_q */ 209void GMPZAPI(cdiv_q)(mp_int q, mp_int n, mp_int d); 210 211/* gmp: mpz_fdiv_q */ 212void GMPZAPI(fdiv_q)(mp_int q, mp_int n, mp_int d); 213 214/* gmp: mpz_fdiv_r */ 215void GMPZAPI(fdiv_r)(mp_int r, mp_int n, mp_int d); 216 217/* gmp: mpz_tdiv_q */ 218void GMPZAPI(tdiv_q)(mp_int q, mp_int n, mp_int d); 219 220/* gmp: mpz_export */ 221void* GMPZAPI(export)(void *rop, size_t *countp, int order, size_t size, int endian, size_t nails, mp_int op); 222 223/* gmp: mpz_import */ 224void GMPZAPI(import)(mp_int rop, size_t count, int order, size_t size, int endian, size_t nails, const void* op); 225 226#ifdef __cplusplus 227} 228#endif 229#endif /* end IMATH_GMP_COMPAT_H_ */ 230