1/* mpc-impl.h -- Internal include file for mpc. 2 3Copyright (C) 2002, 2004, 2005, 2008, 2009, 2010, 2011, 2012 INRIA 4 5This file is part of GNU MPC. 6 7GNU MPC is free software; you can redistribute it and/or modify it under 8the terms of the GNU Lesser General Public License as published by the 9Free Software Foundation; either version 3 of the License, or (at your 10option) any later version. 11 12GNU MPC is distributed in the hope that it will be useful, but WITHOUT ANY 13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 14FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for 15more details. 16 17You should have received a copy of the GNU Lesser General Public License 18along with this program. If not, see http://www.gnu.org/licenses/ . 19*/ 20 21#ifndef __MPC_IMPL_H 22#define __MPC_IMPL_H 23#define __MPC_LIBRARY_BUILD 24 /* to indicate we are inside the library build */ 25 26#include "config.h" 27#ifdef HAVE_STDLIB_H 28#include <stdlib.h> 29#endif 30#include "mpc.h" 31 32/* 33 * Miscellaneous useful macros 34 */ 35 36#define MPC_MIN(h,i) ((h) < (i) ? (h) : (i)) 37#define MPC_MAX(h,i) ((h) > (i) ? (h) : (i)) 38 39/* Safe absolute value (to avoid possible integer overflow) */ 40/* type is the target (unsigned) type (copied from mpfr-impl.h) */ 41#ifdef SAFE_ABS 42#undef SAFE_ABS 43#endif 44#define SAFE_ABS(type,x) ((x) >= 0 ? (type)(x) : -(type)(x)) 45 46 47/* 48 * MPFR constants and macros 49 */ 50 51#ifndef BITS_PER_MP_LIMB 52#define BITS_PER_MP_LIMB mp_bits_per_limb 53#endif 54 55#define MPFR_SIGNBIT(x) (mpfr_signbit (x) ? -1 : 1) 56#define MPC_MPFR_SIGN(x) (mpfr_zero_p (x) ? 0 : MPFR_SIGNBIT (x)) 57 /* should be called MPFR_SIGN, but this is taken in mpfr.h */ 58#define MPFR_CHANGE_SIGN(x) mpfr_neg(x,x,GMP_RNDN) 59#define MPFR_COPYSIGN(x,y,z,rnd) (mpfr_nan_p (z) ? \ 60 mpfr_setsign (x, y, 0, rnd) : \ 61 mpfr_copysign (x, y, z, rnd)) 62 /* work around spurious signs in nan */ 63#define MPFR_ADD_ONE_ULP(x) mpfr_add_one_ulp (x, GMP_RNDN) 64#define MPFR_SUB_ONE_ULP(x) mpfr_sub_one_ulp (x, GMP_RNDN) 65 /* drop unused rounding mode from macroes */ 66#define MPFR_SWAP(a,b) do { mpfr_srcptr tmp; tmp = a; a = b; b = tmp; } while (0) 67 68 69/* 70 * Macro implementing rounding away from zero, to ease compatibility with 71 * mpfr < 3. f is the complete function call with a rounding mode of 72 * MPFR_RNDA, rop the name of the variable containing the result; it is 73 * already contained in f, but needs to be repeated so that the macro can 74 * modify the variable. 75 * Usage: replace each call to a function such as 76 * mpfr_add (rop, a, b, MPFR_RNDA) 77 * by 78 * ROUND_AWAY (mpfr_add (rop, a, b, MPFR_RNDA), rop) 79*/ 80#if MPFR_VERSION_MAJOR < 3 81 /* round towards zero, add 1 ulp if not exact */ 82#define MPFR_RNDA GMP_RNDZ 83#define ROUND_AWAY(f,rop) \ 84 ((f) ? MPFR_ADD_ONE_ULP (rop), MPFR_SIGNBIT (rop) : 0) 85#else 86#define ROUND_AWAY(f,rop) \ 87 (f) 88#endif /* mpfr < 3 */ 89 90#if MPFR_VERSION_MAJOR < 3 91/* declare missing functions, defined in get_version.c */ 92__MPC_DECLSPEC void mpfr_set_zero (mpfr_ptr, int); 93__MPC_DECLSPEC int mpfr_regular_p (mpfr_srcptr); 94#endif /* mpfr < 3 */ 95 96 97/* 98 * MPC macros 99 */ 100 101#define MPC_PREC_RE(x) (mpfr_get_prec(mpc_realref(x))) 102#define MPC_PREC_IM(x) (mpfr_get_prec(mpc_imagref(x))) 103#define MPC_MAX_PREC(x) MPC_MAX(MPC_PREC_RE(x), MPC_PREC_IM(x)) 104 105#define INV_RND(r) \ 106 (((r) == GMP_RNDU) ? GMP_RNDD : (((r) == GMP_RNDD) ? GMP_RNDU : (r))) 107 108#define mpc_inf_p(z) (mpfr_inf_p(mpc_realref(z))||mpfr_inf_p(mpc_imagref(z))) 109 /* Convention in C99 (G.3): z is regarded as an infinity if at least one of 110 its parts is infinite */ 111#define mpc_zero_p(z) (mpfr_zero_p(mpc_realref(z))&&mpfr_zero_p(mpc_imagref(z))) 112 /* Convention in C99 (G.3): z is regarded as a zero if each of its parts is 113 a zero */ 114#define mpc_fin_p(z) (mpfr_number_p(mpc_realref(z))&&mpfr_number_p(mpc_imagref(z))) 115 /* Convention in C99 (G.3): z is regarded as finite if both its parts are */ 116#define mpc_nan_p(z) ((mpfr_nan_p(mpc_realref(z)) && !mpfr_inf_p(mpc_imagref(z))) || (mpfr_nan_p(mpc_imagref(z)) && !mpfr_inf_p(mpc_realref(z)))) 117 /* Consider as NaN all other numbers containing at least one NaN */ 118 119 120/* 121 * ASSERT macros 122 */ 123 124#ifdef NDEBUG 125#define MPC_ASSERT(expr) \ 126 do { \ 127 } while (0) 128#else 129#define MPC_ASSERT(expr) \ 130 do { \ 131 if (!(expr)) \ 132 { \ 133 fprintf (stderr, "%s:%d: MPC assertion failed: %s\n", \ 134 __FILE__, __LINE__, #expr); \ 135 abort(); \ 136 } \ 137 } while (0) 138#endif 139 140 141/* 142 * Debug macros 143 */ 144 145#define MPC_OUT(x) \ 146do { \ 147 printf (#x "[%lu,%lu]=", (unsigned long int) MPC_PREC_RE (x), \ 148 (unsigned long int) MPC_PREC_IM (x)); \ 149 mpc_out_str (stdout, 2, 0, x, MPC_RNDNN); \ 150 printf ("\n"); \ 151} while (0) 152 153#define MPFR_OUT(x) \ 154do { \ 155 printf (#x "[%lu]=", (unsigned long int) mpfr_get_prec (x)); \ 156 mpfr_out_str (stdout, 2, 0, x, GMP_RNDN); \ 157 printf ("\n"); \ 158} while (0) 159 160 161/* 162 * Constants 163 */ 164 165#ifndef MUL_KARATSUBA_THRESHOLD 166#define MUL_KARATSUBA_THRESHOLD 23 167#endif 168 169 170/* 171 * Define internal functions 172 */ 173 174#if defined (__cplusplus) 175extern "C" { 176#endif 177 178 179__MPC_DECLSPEC int mpc_mul_naive (mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t); 180__MPC_DECLSPEC int mpc_mul_karatsuba (mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t); 181__MPC_DECLSPEC int mpc_fma_naive (mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t); 182__MPC_DECLSPEC int mpc_pow_usi (mpc_ptr, mpc_srcptr, unsigned long, int, mpc_rnd_t); 183__MPC_DECLSPEC char* mpc_alloc_str (size_t); 184__MPC_DECLSPEC char* mpc_realloc_str (char*, size_t, size_t); 185__MPC_DECLSPEC void mpc_free_str (char*); 186__MPC_DECLSPEC mpfr_prec_t mpc_ceil_log2 (mpfr_prec_t); 187__MPC_DECLSPEC int set_pi_over_2 (mpfr_ptr, int, mpfr_rnd_t); 188 189#if defined (__cplusplus) 190} 191#endif 192 193 194#endif 195