Fix Int->Num promotions

This commit is contained in:
Bruce Hill 2024-08-13 02:51:10 -04:00
parent f4011eb489
commit 2c4f6536a3
3 changed files with 14 additions and 0 deletions

View File

@ -36,6 +36,14 @@ public Int_t Int$from_num(double n)
return Int$from_mpz(result);
}
public double Int$as_num(Int_t i)
{
if (__builtin_expect(i.small & 1, 1))
return (double)(i.small >> 2);
return mpz_get_d(*i.big);
}
public CORD Int$as_text(const Int_t *i, bool colorize, const TypeInfo *type) {
(void)type;
if (!i) return "Int";

View File

@ -79,6 +79,7 @@ Int_t Int$abs(Int_t x);
#define Int$as_i64(i) (((i).small & 1) ? (int64_t)((i).small >> 2) : mpz_get_si(*(i).big))
Int_t Int$from_i64(int64_t i);
Int_t Int$from_num(double n);
double Int$as_num(Int_t i);
#define I(i) ((int64_t)(i) == (int32_t)(i) ? ((Int_t){.small=((uint64_t)(i)<<2)|1}) : Int$from_i64(i))
Int_t Int$plus(Int_t x, Int_t y);

View File

@ -36,6 +36,11 @@ static bool promote(env_t *env, CORD *code, type_t *actual, type_t *needed)
return true;
}
if (actual->tag == IntType && Match(actual, IntType)->bits == 0 && needed->tag == NumType) {
*code = CORD_all("Int$as_num(", *code, ")");
return true;
}
if (actual->tag == IntType || actual->tag == NumType)
return true;