mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-31 11:50:07 +00:00
Bug fix: comparisons with shifts
This commit is contained in:
+12
-3
@@ -30,6 +30,14 @@ Type_Info_Enum_Value :: raw_union {
|
||||
i: i64;
|
||||
}
|
||||
|
||||
// NOTE(bill): This much the same as the compiler's
|
||||
Calling_Convention :: enum {
|
||||
ODIN = 0,
|
||||
C = 1,
|
||||
STD = 2,
|
||||
FAST = 3,
|
||||
}
|
||||
|
||||
Type_Info :: union {
|
||||
Named: struct #ordered {
|
||||
name: string;
|
||||
@@ -52,9 +60,10 @@ Type_Info :: union {
|
||||
elem: ^Type_Info;
|
||||
};
|
||||
Procedure: struct #ordered {
|
||||
params: ^Type_Info; // Type_Info.Tuple
|
||||
results: ^Type_Info; // Type_Info.Tuple
|
||||
variadic: bool;
|
||||
params: ^Type_Info; // Type_Info.Tuple
|
||||
results: ^Type_Info; // Type_Info.Tuple
|
||||
variadic: bool;
|
||||
convention: Calling_Convention;
|
||||
};
|
||||
Array: struct #ordered {
|
||||
elem: ^Type_Info;
|
||||
|
||||
+358
-42
@@ -421,8 +421,7 @@ fmt_write_padding :: proc(fi: ^Fmt_Info, width: int) {
|
||||
}
|
||||
|
||||
fmt_integer :: proc(fi: ^Fmt_Info, u: u64, base: int, signed: bool, digits: string) {
|
||||
u_i64 := u as i64;
|
||||
negative := signed && u_i64 < 0;
|
||||
negative := signed && u as i64 < 0;
|
||||
if negative {
|
||||
u = -u;
|
||||
}
|
||||
@@ -548,49 +547,366 @@ fmt_int :: proc(fi: ^Fmt_Info, u: u64, signed: bool, verb: rune) {
|
||||
fmt_bad_verb(fi, verb);
|
||||
}
|
||||
}
|
||||
fmt_float :: proc(fi: ^Fmt_Info, v: f64, bits: int, verb: rune) {
|
||||
// TODO(bill): Actually print a float correctly
|
||||
// THIS IS FUCKING SHIT!
|
||||
|
||||
__bot := [23]f64{1e+000,1e+001,1e+002,1e+003,1e+004,1e+005,1e+006,1e+007,1e+008,1e+009,1e+010,1e+011,1e+012,1e+013,1e+014,1e+015,1e+016,1e+017,1e+018,1e+019,1e+020,1e+021,1e+022};
|
||||
__negbot := [22]f64{1e-001,1e-002,1e-003,1e-004,1e-005,1e-006,1e-007,1e-008,1e-009,1e-010,1e-011,1e-012,1e-013,1e-014,1e-015,1e-016,1e-017,1e-018,1e-019,1e-020,1e-021,1e-022};
|
||||
__negboterr := [22]f64{-5.551115123125783e-018,-2.0816681711721684e-019,-2.0816681711721686e-020,-4.7921736023859299e-021,-8.1803053914031305e-022,4.5251888174113741e-023,4.5251888174113739e-024,-2.0922560830128471e-025,-6.2281591457779853e-026,-3.6432197315497743e-027,6.0503030718060191e-028,2.0113352370744385e-029,-3.0373745563400371e-030,1.1806906454401013e-032,-7.7705399876661076e-032,2.0902213275965398e-033,-7.1542424054621921e-034,-7.1542424054621926e-035,2.4754073164739869e-036,5.4846728545790429e-037,9.2462547772103625e-038,-4.8596774326570872e-039};
|
||||
__top := [13]f64{1e+023,1e+046,1e+069,1e+092,1e+115,1e+138,1e+161,1e+184,1e+207,1e+230,1e+253,1e+276,1e+299};
|
||||
__negtop := [13]f64{1e-023,1e-046,1e-069,1e-092,1e-115,1e-138,1e-161,1e-184,1e-207,1e-230,1e-253,1e-276,1e-299};
|
||||
__toperr := [13]f64{8388608,6.8601809640529717e+028,-7.253143638152921e+052,-4.3377296974619174e+075,-1.5559416129466825e+098,-3.2841562489204913e+121,-3.7745893248228135e+144,-1.7356668416969134e+167,-3.8893577551088374e+190,-9.9566444326005119e+213,6.3641293062232429e+236,-5.2069140800249813e+259,-5.2504760255204387e+282};
|
||||
__negtoperr := [13]f64{3.9565301985100693e-040,-2.299904345391321e-063,3.6506201437945798e-086,1.1875228833981544e-109,-5.0644902316928607e-132,-6.7156837247865426e-155,-2.812077463003139e-178,-5.7778912386589953e-201,7.4997100559334532e-224,-4.6439668915134491e-247,-6.3691100762962136e-270,-9.436808465446358e-293,8.0970921678014997e-317};
|
||||
|
||||
__digitpair := "00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899";
|
||||
|
||||
|
||||
__powten := [20]u64{1,10,100,1000, 10000,100000,1000000,10000000, 100000000,1000000000,10000000000,100000000000, 1000000000000,10000000000000,100000000000000,1000000000000000, 10000000000000000,100000000000000000,1000000000000000000,10000000000000000000 };
|
||||
|
||||
__TEN_TO_19TH :: 1000000000000000000;
|
||||
|
||||
__ddmulthi :: proc(ol: f64, xh, yh: f64) -> f64 {
|
||||
bt: i64;
|
||||
oh := xh * yh;
|
||||
bt = xh transmute i64;
|
||||
bt &= (~(0 as u64)<<27) as i64;
|
||||
ahi := bt transmute f64;
|
||||
alo := xh-ahi;
|
||||
bt = yh transmute i64;
|
||||
bt &= (~(0 as u64)<<27) as i64;
|
||||
bhi := bt transmute f64;
|
||||
blo := yh-bhi;
|
||||
return ((ahi*bhi-oh)+ahi*blo+alo*bhi)+alo*blo;
|
||||
}
|
||||
|
||||
__ddtoi64 :: proc(xh, xl: f64) -> i64 {
|
||||
ob := xh as i64;
|
||||
vh := ob as f64;
|
||||
ahi := xh-vh;
|
||||
t := ahi-xh;
|
||||
alo := (xh-(ahi-t)) - (vh+t);
|
||||
ob += (ahi+alo+xl) as i64;
|
||||
return ob;
|
||||
}
|
||||
|
||||
__ddrenorm :: proc(oh, ol: f64) -> f64 {
|
||||
s := oh + ol;
|
||||
ol = ol - (s-oh);
|
||||
return s;
|
||||
}
|
||||
|
||||
__ddmultlo :: proc(oh, ol, xh, xl, yh, yl: f64) -> f64 {
|
||||
return ol + (xh*yl + xl*yh);
|
||||
}
|
||||
|
||||
__ddmutlos :: proc(oh, ol, xh, yl: f64) -> f64 {
|
||||
return ol + (xh*yl);
|
||||
}
|
||||
|
||||
__raise_to_power10 :: proc(ohi, olo: ^f64, d: f64, power: i32) { // power can be -323 to +350
|
||||
ph, pl: f64;
|
||||
|
||||
if 0<=power&&power<=22 {
|
||||
ph = __ddmulthi(pl, d, __bot[power]);
|
||||
} else {
|
||||
p2h, p2l: f64;
|
||||
|
||||
e := power; if power<0 { e = -e; }
|
||||
et := (e*0x2c9)>>14;
|
||||
if et>13 {
|
||||
et = 13;
|
||||
}
|
||||
eb := e-(et*23);
|
||||
|
||||
ph = d;
|
||||
pl = 0.0;
|
||||
if power<0 {
|
||||
if eb != 0 {
|
||||
eb -= 1;
|
||||
ph = __ddmulthi(pl, d, __negbot[eb]);
|
||||
ph = __ddmutlos(ph, pl, d, __negboterr[eb]);
|
||||
}
|
||||
if et != 0 {
|
||||
ph = __ddrenorm(ph, pl);
|
||||
et -= 1;
|
||||
p2h = __ddmulthi(p2l, ph, __negtop[et]);
|
||||
p2h = __ddmultlo(p2h, p2l, ph, pl, __negtop[et], __negtoperr[et]);
|
||||
ph = p2h;
|
||||
pl = p2l;
|
||||
}
|
||||
} else {
|
||||
if eb != 0 {
|
||||
e = eb;
|
||||
if eb > 22 {
|
||||
eb = 22;
|
||||
}
|
||||
e -= eb;
|
||||
ph = __ddmulthi(pl, d, __bot[eb]);
|
||||
if e != 0 {
|
||||
ph = __ddrenorm(ph, pl);
|
||||
p2h = __ddmulthi(p2l, ph, __bot[e]);
|
||||
p2h = __ddmutlos(p2h, p2l, __bot[e], pl);
|
||||
ph = p2h;
|
||||
pl = p2l;
|
||||
}
|
||||
}
|
||||
if et != 0 {
|
||||
ph = __ddrenorm(ph, pl);
|
||||
et -= 1;
|
||||
p2h = __ddmulthi(p2l, ph, __top[et]);
|
||||
p2h = __ddmultlo(p2h, p2l, ph, pl, __top[et], __toperr[et]);
|
||||
ph = p2h;
|
||||
pl = p2l;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ph = __ddrenorm(ph, pl);
|
||||
ohi^ = ph;
|
||||
olo^ = pl;
|
||||
}
|
||||
|
||||
__SPECIAL :: 0x7000;
|
||||
|
||||
__real_to_string :: proc(start: ^string, out: []byte, decimal_pos: ^i32, val: f64, frac_digits: i32, verb: rune) -> bool {
|
||||
e, tens: i32;
|
||||
d: f64 = val;
|
||||
|
||||
bits := d transmute i64;
|
||||
expo := (bits>>52 & 2047) as i32;
|
||||
neg := (bits>>63) as i32 != 0;
|
||||
if neg {
|
||||
d = -d;
|
||||
}
|
||||
|
||||
if expo == 2047 {
|
||||
x: i64 = 1<<52-1;
|
||||
if bits&x != 0 {
|
||||
start^ = "NaN";
|
||||
} else {
|
||||
start^ = "Inf";
|
||||
}
|
||||
decimal_pos^ = __SPECIAL;
|
||||
return neg;
|
||||
}
|
||||
|
||||
if expo == 0 { // is zero or denormal
|
||||
if bits<<1 == 0 {
|
||||
decimal_pos^ = 1;
|
||||
out[0] = '0';
|
||||
start^ = out[:1] as string;
|
||||
return neg;
|
||||
}
|
||||
// find the right expo for denormals
|
||||
v: i64 = 1<<51;
|
||||
while bits&v == 0 {
|
||||
expo -=1;
|
||||
v >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
// find the decimal exponent as well as the decimal bits of the value
|
||||
{
|
||||
// log10 estimate - very specifically tweaked to hit or undershoot by no more than 1 of log10 of all expos 1..2046
|
||||
ph, pl: f64;
|
||||
tens = expo-1023;
|
||||
if tens < 0 {
|
||||
tens = (tens*617)/2048;
|
||||
} else {
|
||||
tens = ((tens*1233)/4096) + 1;
|
||||
}
|
||||
|
||||
// move the significant bits into position and stick them into an int
|
||||
__raise_to_power10(^ph, ^pl, d, 18-tens);
|
||||
|
||||
// get full as much precision from double-double as possible
|
||||
bits = __ddtoi64(ph, pl);
|
||||
|
||||
// check if we undershot
|
||||
if bits as u64 >= __TEN_TO_19TH {
|
||||
tens += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// now do the rounding in integer land
|
||||
match verb {
|
||||
case 'e', 'E', 'f', 'F', 'g', 'G', 'v':
|
||||
break;
|
||||
case 'e', 'E', 'g', 'G':
|
||||
frac_digits += 1;
|
||||
default:
|
||||
frac_digits += tens;
|
||||
}
|
||||
|
||||
if frac_digits < 24 {
|
||||
skip := false;
|
||||
dg: u32 = 1;
|
||||
if bits as u64 >= __powten[9] {
|
||||
dg = 10;
|
||||
}
|
||||
while bits as u64 >= __powten[dg] {
|
||||
dg += 1;
|
||||
if dg == 20 {
|
||||
skip = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!skip) {
|
||||
r: u64;
|
||||
// add 0.5 at the right position and round
|
||||
e = dg as i32 - frac_digits;
|
||||
if e as u32 < 24 {
|
||||
r = __powten[e];
|
||||
bits += (r/2) as i64;
|
||||
if bits as u64 >= __powten[dg] {
|
||||
tens += 1;
|
||||
}
|
||||
bits /= r as i64;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// kill long trailing runs of zeros
|
||||
if bits != 0 {
|
||||
skip := false;
|
||||
while true {
|
||||
if bits <= 0xffffffff {
|
||||
break;
|
||||
}
|
||||
if bits%1000 != 0 {
|
||||
skip = true;
|
||||
break;
|
||||
}
|
||||
bits /= 1000;
|
||||
}
|
||||
if !skip {
|
||||
n := bits as u32;
|
||||
while n%1000 == 0 {
|
||||
n /= 1000;
|
||||
}
|
||||
bits = n as i64;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
e = 0;
|
||||
outp := ^out[64];
|
||||
while true {
|
||||
n: u32;
|
||||
o := outp-8;
|
||||
// do the conversion in chunks of u32s (avoid most 64-bit divides, worth it, constant denomiators be damned)
|
||||
if bits >= 100000000 {
|
||||
n = (bits%100000000) as u32;
|
||||
bits /= 100000000;
|
||||
} else {
|
||||
n = bits as u32;
|
||||
bits = 0;
|
||||
}
|
||||
while n != 0 {
|
||||
outp -= 2;
|
||||
(outp as ^u16)^ = (^__digitpair[(n%100)*2] as ^u16)^;
|
||||
n /= 100;
|
||||
e += 2;
|
||||
}
|
||||
if bits == 0 {
|
||||
if e != 0 && outp^ == '0' {
|
||||
outp += 1;
|
||||
e -= 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
while outp != o {
|
||||
outp -= 1;
|
||||
outp^ = '0';
|
||||
e += 1;
|
||||
}
|
||||
}
|
||||
|
||||
decimal_pos^ = tens;
|
||||
start^ = slice_ptr(outp, e) as string;
|
||||
return neg;
|
||||
}
|
||||
|
||||
|
||||
generic_ftoa :: proc(buf: []byte, val: f64, verb: rune, prec, bit_size: int) -> []byte {
|
||||
Float_Info :: struct {
|
||||
mantbits: uint;
|
||||
expbits: uint;
|
||||
bias: int;
|
||||
};
|
||||
f32info := Float_Info{23, 8, -127};
|
||||
f64info := Float_Info{52, 11, -1023};
|
||||
|
||||
|
||||
bits: u64;
|
||||
flt: ^Float_Info;
|
||||
match bit_size {
|
||||
case 32:
|
||||
bits = ((val as f32) transmute u32) as u64;
|
||||
flt = ^f32info;
|
||||
case 64:
|
||||
bits = val transmute u64;
|
||||
flt = ^f64info;
|
||||
default:
|
||||
panic("illegal float bit_size");
|
||||
}
|
||||
|
||||
neg := bits>>(flt.expbits+flt.mantbits) != 0;
|
||||
exp := (bits>>flt.mantbits) as int & (1<<flt.expbits - 1);
|
||||
mant := bits & ((1 as u64)<<flt.mantbits - 1);
|
||||
|
||||
match exp {
|
||||
case 1<<flt.expbits-1:
|
||||
s: string;
|
||||
match {
|
||||
case mant!=0: s = "NaN";
|
||||
case neg: s = "-Inf";
|
||||
default: s = "+Inf";
|
||||
}
|
||||
copy(buf, s as []byte);
|
||||
return buf[:s.count];
|
||||
|
||||
case 0: // denormalized
|
||||
exp+=1;
|
||||
default: // add implicit top bit
|
||||
mant |= (1 as u64)<<flt.mantbits;
|
||||
}
|
||||
|
||||
|
||||
i := 0;
|
||||
match verb {
|
||||
case 'e', 'E':
|
||||
|
||||
case 'v', 'f', 'F':
|
||||
if neg {
|
||||
buf[i] = '-'; i+=1;
|
||||
}
|
||||
buf[i] = '0'; i+=1;
|
||||
if prec > 0 {
|
||||
buf[i] = '.'; i+=1;
|
||||
for j : 0..<prec {
|
||||
ch: byte = '0';
|
||||
}
|
||||
}
|
||||
|
||||
case 'g', 'G':
|
||||
|
||||
}
|
||||
|
||||
return buf[:0];
|
||||
}
|
||||
|
||||
fmt_float :: proc(fi: ^Fmt_Info, v: f64, bit_size: int, verb: rune) {
|
||||
buf: [512]byte;
|
||||
|
||||
match verb {
|
||||
// case 'e', 'E', 'f', 'F', 'g', 'G', 'v':
|
||||
// case 'f', 'F', 'v':
|
||||
|
||||
case 'f', 'F', 'v':
|
||||
b := generic_ftoa(buf[:], v, verb, fi.prec, bit_size);
|
||||
buffer_write(fi.buf, b);
|
||||
default:
|
||||
fmt_bad_verb(fi, verb);
|
||||
return;
|
||||
}
|
||||
|
||||
f := v;
|
||||
|
||||
if f == 0 {
|
||||
buffer_write_byte(fi.buf, '0');
|
||||
return;
|
||||
}
|
||||
|
||||
if f < 0 {
|
||||
buffer_write_byte(fi.buf, '-');
|
||||
f = -f;
|
||||
}
|
||||
i := f as u64;
|
||||
fmt_int(fi, i, false, 'd');
|
||||
f -= i as f64;
|
||||
buffer_write_byte(fi.buf, '.');
|
||||
|
||||
decimal_places := 5;
|
||||
match bits {
|
||||
case 32: decimal_places = 7;
|
||||
case 64: decimal_places = 15;
|
||||
}
|
||||
if fi.prec_set {
|
||||
decimal_places = fi.prec;
|
||||
}
|
||||
|
||||
while mult: f64 = 10.0; decimal_places >= 0 {
|
||||
i = (f * mult) as u64;
|
||||
fmt_int(fi, i, false, 'd');
|
||||
f -= i as f64 / mult;
|
||||
mult *= 10;
|
||||
decimal_places -= 1;
|
||||
}
|
||||
}
|
||||
fmt_string :: proc(fi: ^Fmt_Info, s: string, verb: rune) {
|
||||
match verb {
|
||||
@@ -833,8 +1149,8 @@ fmt_arg :: proc(fi: ^Fmt_Info, arg: any, verb: rune) {
|
||||
|
||||
if verb == 'T' {
|
||||
ti := arg.type_info;
|
||||
if ti == type_info(^Type_Info) {
|
||||
ti = (arg.data as ^^Type_Info)^;
|
||||
match type a : arg {
|
||||
case ^Type_Info: ti = a;
|
||||
}
|
||||
buffer_write_type(fi.buf, ti);
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user