Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-disallow-128-bit flag #4487

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion base/runtime/error_checks.odin
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ when ODIN_NO_RTTI {
case 2: idx = int((^u16)(tag_ptr)^) - 1
case 4: idx = int((^u32)(tag_ptr)^) - 1
case 8: idx = int((^u64)(tag_ptr)^) - 1
case 16: idx = int((^u128)(tag_ptr)^) - 1
case 16:
when ODIN_ALLOW_128_BIT {
idx = int((^u128)(tag_ptr)^) - 1
}
}
if idx < 0 {
return nil
Expand Down
4 changes: 2 additions & 2 deletions base/runtime/internal.odin
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ extendhfsf2 :: proc "c" (value: __float16) -> f32 {
}



when ODIN_ALLOW_128_BIT {
@(link_name="__floattidf", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
floattidf :: proc "c" (a: i128) -> f64 {
DBL_MANT_DIG :: 53
Expand Down Expand Up @@ -1086,7 +1086,7 @@ fixdfti :: proc "c" (a: u64) -> i128 {
}

}

}


__write_bits :: proc "contextless" (dst, src: [^]byte, offset: uintptr, size: uintptr) {
Expand Down
4 changes: 4 additions & 0 deletions base/runtime/udivmod128.odin
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package runtime

import "base:intrinsics"

when ODIN_ALLOW_128_BIT {

udivmod128 :: proc "c" (a, b: u128, rem: ^u128) -> u128 {
_ctz :: intrinsics.count_trailing_zeros
_clz :: intrinsics.count_leading_zeros
Expand Down Expand Up @@ -154,3 +156,5 @@ udivmod128 :: proc "c" (a, b: u128, rem: ^u128) -> u128 {

return q_all
}

}
Binary file modified bin/radlink.exe
Binary file not shown.
148 changes: 105 additions & 43 deletions core/encoding/json/marshal.odin
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,39 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err:

case runtime.Type_Info_Integer:
buf: [40]byte
u := cast_any_int_to_u128(a)
when ODIN_ALLOW_128_BIT {
u := cast_any_int_to_u128(a)
} else {
u := cast_any_int_to_u64(a)
}

s: string

// allow uints to be printed as hex
if opt.write_uint_as_hex && (opt.spec == .JSON5 || opt.spec == .MJSON) {
switch i in a {
case u8, u16, u32, u64, u128:
s = strconv.append_bits_128(buf[:], u, 16, info.signed, 8*ti.size, "0123456789abcdef", { .Prefix })
when ODIN_ALLOW_128_BIT {
switch i in a {
case u8, u16, u32, u64, u128:
s = strconv.append_bits_128(buf[:], u, 16, info.signed, 8*ti.size, "0123456789abcdef", { .Prefix })

case:
s = strconv.append_bits_128(buf[:], u, 10, info.signed, 8*ti.size, "0123456789", nil)
case:
s = strconv.append_bits_128(buf[:], u, 10, info.signed, 8*ti.size, "0123456789", nil)
}
} else {
switch i in a {
case u8, u16, u32, u64:
s = strconv.append_bits(buf[:], u, 16, info.signed, 8*ti.size, "0123456789abcdef", { .Prefix })

case:
s = strconv.append_bits(buf[:], u, 10, info.signed, 8*ti.size, "0123456789", nil)
}
}
} else {
s = strconv.append_bits_128(buf[:], u, 10, info.signed, 8*ti.size, "0123456789", nil)
when ODIN_ALLOW_128_BIT {
s = strconv.append_bits_128(buf[:], u, 10, info.signed, 8*ti.size, "0123456789", nil)
} else {
s = strconv.append_bits(buf[:], u, 10, info.signed, 8*ti.size, "0123456789", nil)
}
}

io.write_string(w, s) or_return
Expand Down Expand Up @@ -275,8 +293,14 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err:
opt_write_key(w, opt, name) or_return
case runtime.Type_Info_Integer:
buf: [40]byte
u := cast_any_int_to_u128(ka)
name = strconv.append_bits_128(buf[:], u, 10, info.signed, 8*kti.size, "0123456789", nil)
when ODIN_ALLOW_128_BIT {
u := cast_any_int_to_u128(ka)
name = strconv.append_bits_128(buf[:], u, 10, info.signed, 8*kti.size, "0123456789", nil)
} else {
u := cast_any_int_to_u64(ka)
name = strconv.append_bits(buf[:], u, 10, info.signed, 8*kti.size, "0123456789", nil)

}

opt_write_key(w, opt, name) or_return
case: return .Unsupported_Type
Expand Down Expand Up @@ -631,40 +655,78 @@ opt_write_indentation :: proc(w: io.Writer, opt: ^Marshal_Options) -> (err: io.E
return
}

@(private)
cast_any_int_to_u128 :: proc(any_int_value: any) -> u128 {
u: u128 = 0
switch i in any_int_value {
case i8: u = u128(i)
case i16: u = u128(i)
case i32: u = u128(i)
case i64: u = u128(i)
case i128: u = u128(i)
case int: u = u128(i)
case u8: u = u128(i)
case u16: u = u128(i)
case u32: u = u128(i)
case u64: u = u128(i)
case u128: u = u128(i)
case uint: u = u128(i)
case uintptr: u = u128(i)

case i16le: u = u128(i)
case i32le: u = u128(i)
case i64le: u = u128(i)
case u16le: u = u128(i)
case u32le: u = u128(i)
case u64le: u = u128(i)
case u128le: u = u128(i)

case i16be: u = u128(i)
case i32be: u = u128(i)
case i64be: u = u128(i)
case u16be: u = u128(i)
case u32be: u = u128(i)
case u64be: u = u128(i)
case u128be: u = u128(i)

when ODIN_ALLOW_128_BIT {
@(private)
cast_any_int_to_u128 :: proc(any_int_value: any) -> u128 {
u: u128 = 0
switch i in any_int_value {
case i8: u = u128(i)
case i16: u = u128(i)
case i32: u = u128(i)
case i64: u = u128(i)
case i128: u = u128(i)
case int: u = u128(i)
case u8: u = u128(i)
case u16: u = u128(i)
case u32: u = u128(i)
case u64: u = u128(i)
case u128: u = u128(i)
case uint: u = u128(i)
case uintptr: u = u128(i)

case i16le: u = u128(i)
case i32le: u = u128(i)
case i64le: u = u128(i)
case u16le: u = u128(i)
case u32le: u = u128(i)
case u64le: u = u128(i)
case u128le: u = u128(i)

case i16be: u = u128(i)
case i32be: u = u128(i)
case i64be: u = u128(i)
case u16be: u = u128(i)
case u32be: u = u128(i)
case u64be: u = u128(i)
case u128be: u = u128(i)
}

return u
}
} else {
@(private)
cast_any_int_to_u64 :: proc(any_int_value: any) -> u64 {
u: u64 = 0
switch i in any_int_value {
case i8: u = u64(i)
case i16: u = u64(i)
case i32: u = u64(i)
case i64: u = u64(i)
case int: u = u64(i)
case u8: u = u64(i)
case u16: u = u64(i)
case u32: u = u64(i)
case u64: u = u64(i)
case uint: u = u64(i)
case uintptr: u = u64(i)

case i16le: u = u64(i)
case i32le: u = u64(i)
case i64le: u = u64(i)
case u16le: u = u64(i)
case u32le: u = u64(i)
case u64le: u = u64(i)

case i16be: u = u64(i)
case i32be: u = u64(i)
case i64be: u = u64(i)
case u16be: u = u64(i)
case u32be: u = u64(i)
case u64be: u = u64(i)
}

return u
}

return u
}
35 changes: 26 additions & 9 deletions core/encoding/json/unmarshal.odin
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ assign_int :: proc(val: any, i: $T) -> bool {
case i64: dst = i64 (i)
case i64le: dst = i64le (i)
case i64be: dst = i64be (i)
case i128: dst = i128 (i)
case i128le: dst = i128le (i)
case i128be: dst = i128be (i)
case u8: dst = u8 (i)
case u16: dst = u16 (i)
case u16le: dst = u16le (i)
Expand All @@ -110,13 +107,21 @@ assign_int :: proc(val: any, i: $T) -> bool {
case u64: dst = u64 (i)
case u64le: dst = u64le (i)
case u64be: dst = u64be (i)
case u128: dst = u128 (i)
case u128le: dst = u128le (i)
case u128be: dst = u128be (i)
case int: dst = int (i)
case uint: dst = uint (i)
case uintptr: dst = uintptr(i)
case:
when ODIN_ALLOW_128_BIT {
switch &dst in v {
case i128: dst = i128 (i); return true
case i128le: dst = i128le (i); return true
case i128be: dst = i128be (i); return true
case u128: dst = u128 (i); return true
case u128le: dst = u128le (i); return true
case u128be: dst = u128be (i); return true
}
}

ti := type_info_of(v.id)
if _, ok := ti.variant.(runtime.Type_Info_Bit_Set); ok {
do_byte_swap := !reflect.bit_set_is_big_endian(v)
Expand Down Expand Up @@ -213,7 +218,11 @@ unmarshal_string_token :: proc(p: ^Parser, val: any, str: string, ti: ^reflect.T
return true, nil

case reflect.Type_Info_Integer:
i, pok := strconv.parse_i128(str)
when ODIN_ALLOW_128_BIT {
i, pok := strconv.parse_i128(str)
} else {
i, pok := strconv.parse_i64(str)
}
if !pok {
return false, nil
}
Expand Down Expand Up @@ -296,7 +305,11 @@ unmarshal_value :: proc(p: ^Parser, v: any) -> (err: Unmarshal_Error) {

case .Integer:
advance_token(p)
i, _ := strconv.parse_i128(token.text)
when ODIN_ALLOW_128_BIT {
i, _ := strconv.parse_i128(token.text)
} else {
i, _ := strconv.parse_i64(token.text)
}
if assign_int(v, i) {
return
}
Expand Down Expand Up @@ -556,7 +569,11 @@ unmarshal_object :: proc(p: ^Parser, v: any, end_token: Token_Kind) -> (err: Unm
key_ptr = &key_cstr
}
case runtime.Type_Info_Integer:
i, ok := strconv.parse_i128(key)
when ODIN_ALLOW_128_BIT {
i, ok := strconv.parse_i128(key)
} else {
i, ok := strconv.parse_i64(key)
}
if !ok { return UNSUPPORTED_TYPE }
key_ptr = rawptr(&i)
case: return UNSUPPORTED_TYPE
Expand Down
Loading
Loading