Skip to content

Commit c585ad1

Browse files
doogledpgeorge
authored andcommitted
py: Move mp_float_t related defines to misc.h
1 parent 6b63673 commit c585ad1

2 files changed

Lines changed: 22 additions & 15 deletions

File tree

py/misc.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,17 @@ static inline mp_uint_t count_lead_ones(byte val) {
183183
}
184184
#endif
185185

186+
/** float internals *************/
187+
188+
#if MICROPY_PY_BUILTINS_FLOAT
189+
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
190+
#define MP_FLOAT_EXP_BITS (11)
191+
#define MP_FLOAT_FRAC_BITS (52)
192+
#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
193+
#define MP_FLOAT_EXP_BITS (8)
194+
#define MP_FLOAT_FRAC_BITS (23)
195+
#endif
196+
#define MP_FLOAT_EXP_BIAS ((1 << (MP_FLOAT_EXP_BITS - 1)) - 1)
197+
#endif // MICROPY_PY_BUILTINS_FLOAT
198+
186199
#endif // __MICROPY_INCLUDED_PY_MISC_H__

py/mpz.c

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -698,29 +698,25 @@ void mpz_set_from_ll(mpz_t *z, long long val, bool is_signed) {
698698
#if MICROPY_PY_BUILTINS_FLOAT
699699
void mpz_set_from_float(mpz_t *z, mp_float_t src) {
700700
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
701-
#define EXP_SZ 11
702-
#define FRC_SZ 52
703701
typedef uint64_t mp_float_int_t;
704-
#else
705-
#define EXP_SZ 8
706-
#define FRC_SZ 23
702+
#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
707703
typedef uint32_t mp_float_int_t;
708704
#endif
709705
union {
710706
mp_float_t f;
711-
struct { mp_float_int_t frc:FRC_SZ, exp:EXP_SZ, sgn:1; } p;
707+
struct { mp_float_int_t frc:MP_FLOAT_FRAC_BITS, exp:MP_FLOAT_EXP_BITS, sgn:1; } p;
712708
} u = {src};
713709

714710
z->neg = u.p.sgn;
715711
if (u.p.exp == 0) {
716712
// value == 0 || value < 1
717713
mpz_set_from_int(z, 0);
718-
} else if (u.p.exp == ((1 << EXP_SZ) - 1)) {
714+
} else if (u.p.exp == ((1 << MP_FLOAT_EXP_BITS) - 1)) {
719715
// u.p.frc == 0 indicates inf, else NaN
720716
// should be handled by caller
721717
mpz_set_from_int(z, 0);
722718
} else {
723-
const int adj_exp = (int)u.p.exp - ((1 << (EXP_SZ - 1)) - 1);
719+
const int adj_exp = (int)u.p.exp - MP_FLOAT_EXP_BIAS;
724720
if (adj_exp < 0) {
725721
// value < 1 , truncates to 0
726722
mpz_set_from_int(z, 0);
@@ -732,15 +728,15 @@ typedef uint32_t mp_float_int_t;
732728
const int dig_cnt = (adj_exp + 1 + (DIG_SIZE - 1)) / DIG_SIZE;
733729
const unsigned int rem = adj_exp % DIG_SIZE;
734730
int dig_ind, shft;
735-
mp_float_int_t frc = u.p.frc | ((mp_float_int_t)1 << FRC_SZ);
731+
mp_float_int_t frc = u.p.frc | ((mp_float_int_t)1 << MP_FLOAT_FRAC_BITS);
736732

737-
if (adj_exp < FRC_SZ) {
733+
if (adj_exp < MP_FLOAT_FRAC_BITS) {
738734
shft = 0;
739735
dig_ind = 0;
740-
frc >>= FRC_SZ - adj_exp;
736+
frc >>= MP_FLOAT_FRAC_BITS - adj_exp;
741737
} else {
742-
shft = (rem - FRC_SZ) % DIG_SIZE;
743-
dig_ind = (adj_exp - FRC_SZ) / DIG_SIZE;
738+
shft = (rem - MP_FLOAT_FRAC_BITS) % DIG_SIZE;
739+
dig_ind = (adj_exp - MP_FLOAT_FRAC_BITS) / DIG_SIZE;
744740
}
745741
mpz_need_dig(z, dig_cnt);
746742
z->len = dig_cnt;
@@ -758,8 +754,6 @@ typedef uint32_t mp_float_int_t;
758754
}
759755
}
760756
}
761-
#undef EXP_SZ
762-
#undef FRC_SZ
763757
#endif
764758

765759
// returns number of bytes from str that were processed

0 commit comments

Comments
 (0)