File indexing completed on 2026-11-07 10:15:12
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef FMPZ_EXTRAS_H
0013 #define FMPZ_EXTRAS_H
0014
0015 #include "fmpz.h"
0016
0017 #ifdef __cplusplus
0018 extern "C" {
0019 #endif
0020
0021 static inline void
0022 fmpz_add_inline(fmpz_t z, const fmpz_t x, const fmpz_t y)
0023 {
0024 fmpz f, g;
0025
0026 f = *x;
0027 g = *y;
0028
0029 if (!COEFF_IS_MPZ(f) && !COEFF_IS_MPZ(g))
0030 fmpz_set_si(z, f + g);
0031 else
0032 fmpz_add(z, x, y);
0033 }
0034
0035 static inline void
0036 fmpz_add_si_inline(fmpz_t z, const fmpz_t x, slong y)
0037 {
0038 fmpz f;
0039
0040 f = *x;
0041
0042 if (!COEFF_IS_MPZ(f) && (COEFF_MIN <= y && y <= COEFF_MAX))
0043 fmpz_set_si(z, f + y);
0044 else
0045 fmpz_add_si(z, x, y);
0046 }
0047
0048 static inline void
0049 fmpz_sub_si_inline(fmpz_t z, const fmpz_t x, slong y)
0050 {
0051 fmpz f;
0052
0053 f = *x;
0054
0055 if (!COEFF_IS_MPZ(f) && (COEFF_MIN <= y && y <= COEFF_MAX))
0056 fmpz_set_si(z, f - y);
0057 else
0058 fmpz_sub_si(z, x, y);
0059 }
0060
0061 static inline void
0062 fmpz_add_ui_inline(fmpz_t z, const fmpz_t x, ulong y)
0063 {
0064 fmpz f = *x;
0065
0066 if (!COEFF_IS_MPZ(f) && y <= COEFF_MAX)
0067 fmpz_set_si(z, f + y);
0068 else
0069 fmpz_add_ui(z, x, y);
0070 }
0071
0072 static inline void
0073 fmpz_add2_fmpz_si_inline(fmpz_t z, const fmpz_t x, const fmpz_t y, slong c)
0074 {
0075 fmpz f, g, h;
0076
0077 f = *x;
0078 g = *y;
0079
0080 if (!COEFF_IS_MPZ(f) && !COEFF_IS_MPZ(g))
0081 {
0082 h = f + g;
0083
0084 if ((COEFF_MIN <= h && h <= COEFF_MAX) &&
0085 (COEFF_MIN <= c && c <= COEFF_MAX))
0086 {
0087 fmpz_set_si(z, h + c);
0088 return;
0089 }
0090 }
0091
0092 fmpz_add(z, x, y);
0093 fmpz_add_si(z, z, c);
0094 }
0095
0096 static inline void
0097 fmpz_adiv_q_2exp(fmpz_t z, const fmpz_t x, flint_bitcnt_t exp)
0098 {
0099 int sign = fmpz_sgn(x);
0100
0101 if (sign > 0)
0102 fmpz_cdiv_q_2exp(z, x, exp);
0103 else
0104 fmpz_fdiv_q_2exp(z, x, exp);
0105 }
0106
0107 static inline void
0108 _fmpz_set_si_small(fmpz_t x, slong v)
0109 {
0110 fmpz_clear(x);
0111 *x = v;
0112 }
0113
0114 slong _fmpz_sub_small_large(const fmpz_t x, const fmpz_t y);
0115
0116 static inline slong
0117 _fmpz_sub_small(const fmpz_t x, const fmpz_t y)
0118 {
0119 if (!COEFF_IS_MPZ(*x) && !COEFF_IS_MPZ(*y))
0120 {
0121 return (*x) - (*y);
0122 }
0123 else
0124 {
0125 return _fmpz_sub_small_large(x, y);
0126 }
0127 }
0128
0129 static inline slong
0130 _fmpz_size(const fmpz_t f)
0131 {
0132 fmpz d = *f;
0133
0134 if (!COEFF_IS_MPZ(d))
0135 return 1;
0136 else
0137 return FLINT_ABS(FMPZ_TO_ZZ(d)->size);
0138 }
0139
0140 static inline void
0141 fmpz_ui_mul_ui(fmpz_t r, ulong a, ulong b)
0142 {
0143 if (a < (UWORD(1) << (FLINT_BITS / 2)) && b < (UWORD(1) << (FLINT_BITS / 2)))
0144 {
0145 fmpz_set_ui(r, a * b);
0146 }
0147 else
0148 {
0149 fmpz_set_ui(r, a);
0150 fmpz_mul_ui(r, r, b);
0151 }
0152 }
0153
0154 static inline void
0155 fmpz_max(fmpz_t z, const fmpz_t x, const fmpz_t y)
0156 {
0157 if (fmpz_cmp(x, y) >= 0)
0158 fmpz_set(z, x);
0159 else
0160 fmpz_set(z, y);
0161 }
0162
0163 static inline void
0164 fmpz_min(fmpz_t z, const fmpz_t x, const fmpz_t y)
0165 {
0166 if (fmpz_cmp(x, y) < 0)
0167 fmpz_set(z, x);
0168 else
0169 fmpz_set(z, y);
0170 }
0171
0172 #define FMPZ_GET_MPN_READONLY(zsign, zn, zptr, ztmp, zv) \
0173 if (!COEFF_IS_MPZ(zv)) \
0174 { \
0175 (zsign) = (zv) < 0; \
0176 (ztmp) = FLINT_ABS(zv); \
0177 (zptr) = &(ztmp); \
0178 (zn) = 1; \
0179 } \
0180 else \
0181 { \
0182 mpz_ptr ___zz = COEFF_TO_PTR(zv); \
0183 (zptr) = ___zz->_mp_d; \
0184 (zn) = ___zz->_mp_size; \
0185 (zsign) = (zn) < 0; \
0186 (zn) = FLINT_ABS(zn); \
0187 }
0188
0189 void fmpz_lshift_mpn(fmpz_t z, nn_srcptr src, slong n, int negative, flint_bitcnt_t shift);
0190
0191 static inline slong
0192 fmpz_allocated_bytes(const fmpz_t x)
0193 {
0194 if (COEFF_IS_MPZ(*x))
0195 return sizeof(zz_struct) + FMPZ_TO_ZZ(*x)->alloc * sizeof(ulong);
0196 else
0197 return 0;
0198 }
0199
0200 #ifdef __cplusplus
0201 }
0202 #endif
0203
0204 #endif