1 /* 2 Copyright (C) 2014 Fredrik Johansson 3 4 This file is part of Arb. 5 6 Arb is free software: you can redistribute it and/or modify it under 7 the terms of the GNU Lesser General Public License (LGPL) as published 8 by the Free Software Foundation; either version 2.1 of the License, or 9 (at your option) any later version. See <http://www.gnu.org/licenses/>. 10 */ 11 12 #include "mag.h" 13 14 void mag_set_ui(mag_t z,ulong x)15mag_set_ui(mag_t z, ulong x) 16 { 17 _fmpz_demote(MAG_EXPREF(z)); 18 19 if (x == 0) 20 { 21 MAG_EXP(z) = 0; 22 MAG_MAN(z) = 0; 23 } 24 else 25 { 26 slong bits; 27 mp_limb_t overflow; 28 29 count_leading_zeros(bits, x); 30 bits = FLINT_BITS - bits; 31 32 if (bits <= MAG_BITS) 33 { 34 x = x << (MAG_BITS - bits); 35 } 36 else 37 { 38 x = (x >> (bits - MAG_BITS)) + 1; 39 overflow = x >> MAG_BITS; 40 bits += overflow; 41 x >>= overflow; 42 } 43 44 MAG_EXP(z) = bits; 45 MAG_MAN(z) = x; 46 } 47 } 48 49 void mag_set_ui_lower(mag_t z,ulong x)50mag_set_ui_lower(mag_t z, ulong x) 51 { 52 _fmpz_demote(MAG_EXPREF(z)); 53 54 if (x == 0) 55 { 56 MAG_EXP(z) = 0; 57 MAG_MAN(z) = 0; 58 } 59 else 60 { 61 unsigned int bits; 62 63 count_leading_zeros(bits, x); 64 bits = FLINT_BITS - bits; 65 66 if (bits <= MAG_BITS) 67 x <<= (MAG_BITS - bits); 68 else 69 x >>= (bits - MAG_BITS); 70 71 MAG_EXP(z) = bits; 72 MAG_MAN(z) = x; 73 } 74 } 75 76