mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-07 08:08:50 +00:00
Merge branch 'master' of https://github.com/odin-lang/Odin
This commit is contained in:
@@ -158,13 +158,14 @@ Error :: enum int {
|
|||||||
Invalid_Pointer = 2,
|
Invalid_Pointer = 2,
|
||||||
Invalid_Argument = 3,
|
Invalid_Argument = 3,
|
||||||
|
|
||||||
Assignment_To_Immutable = 4,
|
Assignment_To_Immutable = 10,
|
||||||
Max_Iterations_Reached = 5,
|
Max_Iterations_Reached = 11,
|
||||||
Buffer_Overflow = 6,
|
Buffer_Overflow = 12,
|
||||||
Integer_Overflow = 7,
|
Integer_Overflow = 13,
|
||||||
|
Integer_Underflow = 14,
|
||||||
|
|
||||||
Division_by_Zero = 8,
|
Division_by_Zero = 30,
|
||||||
Math_Domain_Error = 9,
|
Math_Domain_Error = 31,
|
||||||
|
|
||||||
Cannot_Open_File = 50,
|
Cannot_Open_File = 50,
|
||||||
Cannot_Read_File = 51,
|
Cannot_Read_File = 51,
|
||||||
|
|||||||
+40
-20
@@ -34,6 +34,7 @@ package math_big
|
|||||||
import "core:mem"
|
import "core:mem"
|
||||||
import "core:intrinsics"
|
import "core:intrinsics"
|
||||||
import rnd "core:math/rand"
|
import rnd "core:math/rand"
|
||||||
|
import "core:builtin"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Low-level addition, unsigned. Handbook of Applied Cryptography, algorithm 14.7.
|
Low-level addition, unsigned. Handbook of Applied Cryptography, algorithm 14.7.
|
||||||
@@ -1880,8 +1881,6 @@ internal_int_set_from_integer :: proc(dest: ^Int, src: $T, minimize := false, al
|
|||||||
where intrinsics.type_is_integer(T) {
|
where intrinsics.type_is_integer(T) {
|
||||||
context.allocator = allocator
|
context.allocator = allocator
|
||||||
|
|
||||||
src := src
|
|
||||||
|
|
||||||
internal_error_if_immutable(dest) or_return
|
internal_error_if_immutable(dest) or_return
|
||||||
/*
|
/*
|
||||||
Most internal procs asssume an Int to have already been initialize,
|
Most internal procs asssume an Int to have already been initialize,
|
||||||
@@ -1892,13 +1891,27 @@ internal_int_set_from_integer :: proc(dest: ^Int, src: $T, minimize := false, al
|
|||||||
dest.flags = {} // We're not -Inf, Inf, NaN or Immutable.
|
dest.flags = {} // We're not -Inf, Inf, NaN or Immutable.
|
||||||
|
|
||||||
dest.used = 0
|
dest.used = 0
|
||||||
dest.sign = .Zero_or_Positive if src >= 0 else .Negative
|
dest.sign = .Negative if src < 0 else .Zero_or_Positive
|
||||||
src = internal_abs(src)
|
|
||||||
|
|
||||||
#no_bounds_check for src != 0 {
|
temp := src
|
||||||
dest.digit[dest.used] = DIGIT(src) & _MASK
|
|
||||||
|
is_maximally_negative := src == min(T)
|
||||||
|
if is_maximally_negative {
|
||||||
|
/*
|
||||||
|
Prevent overflow on abs()
|
||||||
|
*/
|
||||||
|
temp += 1
|
||||||
|
}
|
||||||
|
temp = -temp if temp < 0 else temp
|
||||||
|
|
||||||
|
#no_bounds_check for temp != 0 {
|
||||||
|
dest.digit[dest.used] = DIGIT(temp) & _MASK
|
||||||
dest.used += 1
|
dest.used += 1
|
||||||
src >>= _DIGIT_BITS
|
temp >>= _DIGIT_BITS
|
||||||
|
}
|
||||||
|
|
||||||
|
if is_maximally_negative {
|
||||||
|
return internal_sub(dest, dest, 1)
|
||||||
}
|
}
|
||||||
internal_zero_unused(dest)
|
internal_zero_unused(dest)
|
||||||
return nil
|
return nil
|
||||||
@@ -2341,23 +2354,30 @@ internal_get_low_u64 :: proc(a: ^Int) -> u64 #no_bounds_check {
|
|||||||
and maybe return max(T), .Integer_Overflow if not?
|
and maybe return max(T), .Integer_Overflow if not?
|
||||||
*/
|
*/
|
||||||
internal_int_get :: proc(a: ^Int, $T: typeid) -> (res: T, err: Error) where intrinsics.type_is_integer(T) {
|
internal_int_get :: proc(a: ^Int, $T: typeid) -> (res: T, err: Error) where intrinsics.type_is_integer(T) {
|
||||||
size_in_bits := int(size_of(T) * 8)
|
/*
|
||||||
i := int((size_in_bits + _DIGIT_BITS - 1) / _DIGIT_BITS)
|
Calculate target bit size.
|
||||||
i = min(int(a.used), i)
|
*/
|
||||||
|
target_bit_size := int(size_of(T) * 8)
|
||||||
#no_bounds_check for ; i >= 0; i -= 1 {
|
when !intrinsics.type_is_unsigned(T) {
|
||||||
res <<= uint(0) if size_in_bits <= _DIGIT_BITS else _DIGIT_BITS
|
if a.sign == .Zero_or_Positive {
|
||||||
res |= T(a.digit[i])
|
target_bit_size -= 1
|
||||||
if size_in_bits <= _DIGIT_BITS {
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
bits_used := internal_count_bits(a)
|
||||||
|
|
||||||
|
if bits_used > target_bit_size {
|
||||||
|
if a.sign == .Negative {
|
||||||
|
return min(T), .Integer_Underflow
|
||||||
|
}
|
||||||
|
return max(T), .Integer_Overflow
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := a.used; i > 0; i -= 1 {
|
||||||
|
res <<= _DIGIT_BITS
|
||||||
|
res |= T(a.digit[i - 1])
|
||||||
|
}
|
||||||
|
|
||||||
when !intrinsics.type_is_unsigned(T) {
|
when !intrinsics.type_is_unsigned(T) {
|
||||||
/*
|
|
||||||
Mask off sign bit.
|
|
||||||
*/
|
|
||||||
res ~= 1 << uint(size_in_bits - 1)
|
|
||||||
/*
|
/*
|
||||||
Set the sign.
|
Set the sign.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -2,13 +2,15 @@ package runtime
|
|||||||
|
|
||||||
import "core:intrinsics"
|
import "core:intrinsics"
|
||||||
|
|
||||||
|
@(private="file")
|
||||||
|
IS_WASM :: ODIN_ARCH == "wasm32" || ODIN_ARCH == "wasm64"
|
||||||
|
|
||||||
@(private)
|
@(private)
|
||||||
RUNTIME_LINKAGE :: "strong" when (
|
RUNTIME_LINKAGE :: "strong" when (
|
||||||
(ODIN_USE_SEPARATE_MODULES ||
|
(ODIN_USE_SEPARATE_MODULES ||
|
||||||
ODIN_BUILD_MODE == "dynamic" ||
|
ODIN_BUILD_MODE == "dynamic" ||
|
||||||
!ODIN_NO_CRT) &&
|
!ODIN_NO_CRT) &&
|
||||||
!(ODIN_ARCH == "wasm32" ||
|
!IS_WASM) else "internal"
|
||||||
ODIN_ARCH == "wasm64")) else "internal"
|
|
||||||
RUNTIME_REQUIRE :: true
|
RUNTIME_REQUIRE :: true
|
||||||
|
|
||||||
|
|
||||||
@@ -752,6 +754,9 @@ extendhfsf2 :: proc "c" (value: u16) -> f32 {
|
|||||||
|
|
||||||
@(link_name="__floattidf", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
|
@(link_name="__floattidf", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
|
||||||
floattidf :: proc "c" (a: i128) -> f64 {
|
floattidf :: proc "c" (a: i128) -> f64 {
|
||||||
|
when IS_WASM {
|
||||||
|
return 0
|
||||||
|
} else {
|
||||||
DBL_MANT_DIG :: 53
|
DBL_MANT_DIG :: 53
|
||||||
if a == 0 {
|
if a == 0 {
|
||||||
return 0.0
|
return 0.0
|
||||||
@@ -791,10 +796,14 @@ floattidf :: proc "c" (a: i128) -> f64 {
|
|||||||
fb[0] = u32(a) // mantissa-low
|
fb[0] = u32(a) // mantissa-low
|
||||||
return transmute(f64)fb
|
return transmute(f64)fb
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@(link_name="__floattidf_unsigned", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
|
@(link_name="__floattidf_unsigned", linkage=RUNTIME_LINKAGE, require=RUNTIME_REQUIRE)
|
||||||
floattidf_unsigned :: proc "c" (a: u128) -> f64 {
|
floattidf_unsigned :: proc "c" (a: u128) -> f64 {
|
||||||
|
when IS_WASM {
|
||||||
|
return 0
|
||||||
|
} else {
|
||||||
DBL_MANT_DIG :: 53
|
DBL_MANT_DIG :: 53
|
||||||
if a == 0 {
|
if a == 0 {
|
||||||
return 0.0
|
return 0.0
|
||||||
@@ -832,6 +841,7 @@ floattidf_unsigned :: proc "c" (a: u128) -> f64 {
|
|||||||
fb[0] = u32(a) // mantissa-low
|
fb[0] = u32(a) // mantissa-low
|
||||||
return transmute(f64)fb
|
return transmute(f64)fb
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,23 +1,40 @@
|
|||||||
//+build wasm32
|
//+build wasm32
|
||||||
package runtime
|
package runtime
|
||||||
|
|
||||||
|
@(private="file")
|
||||||
|
ti_int :: struct #raw_union {
|
||||||
|
using s: struct { lo, hi: u64 },
|
||||||
|
all: i128,
|
||||||
|
}
|
||||||
|
|
||||||
@(link_name="__ashlti3", linkage="strong")
|
@(link_name="__ashlti3", linkage="strong")
|
||||||
__ashlti3 :: proc "c" (a: i64, b_: i32) -> i64 {
|
__ashlti3 :: proc "c" (a: i128, b_: u32) -> i128 {
|
||||||
/*
|
bits_in_dword :: size_of(u32)*8
|
||||||
b := u32(b_)
|
b := u32(b_)
|
||||||
input := transmute([2]i32)a
|
|
||||||
result: [2]i32
|
input, result: ti_int
|
||||||
if b & 32 != 0 {
|
input.all = a
|
||||||
result[0] = 0
|
if b & bits_in_dword != 0 {
|
||||||
result[1] = input[0] << (b - 32)
|
result.lo = 0
|
||||||
|
result.hi = input.lo << (b-bits_in_dword)
|
||||||
} else {
|
} else {
|
||||||
if b == 0 {
|
if b == 0 {
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
result[0] = input[0]<<b
|
result.lo = input.lo<<b
|
||||||
result[1] = (input[1]<<b) | (input[0]>>(32-b))
|
result.hi = (input.hi<<b) | (input.lo>>(bits_in_dword-b))
|
||||||
}
|
}
|
||||||
return transmute(i64)result
|
return result.all
|
||||||
*/
|
}
|
||||||
return 0
|
|
||||||
|
|
||||||
|
@(link_name="__multi3", linkage="strong")
|
||||||
|
__multi3 :: proc "c" (a, b: i128) -> i128 {
|
||||||
|
x, y, r: ti_int
|
||||||
|
|
||||||
|
x.all = a
|
||||||
|
y.all = b
|
||||||
|
r.all = i128(x.lo * y.lo) // TODO this is incorrect
|
||||||
|
r.hi += x.hi*y.lo + x.lo*y.hi
|
||||||
|
return r.all
|
||||||
}
|
}
|
||||||
+16
-11
@@ -127,17 +127,22 @@ def we_iterate():
|
|||||||
# Error enum values
|
# Error enum values
|
||||||
#
|
#
|
||||||
class Error(Enum):
|
class Error(Enum):
|
||||||
Okay = 0
|
Okay = 0
|
||||||
Out_Of_Memory = 1
|
Out_Of_Memory = 1
|
||||||
Invalid_Pointer = 2
|
Invalid_Pointer = 2
|
||||||
Invalid_Argument = 3
|
Invalid_Argument = 3
|
||||||
Unknown_Error = 4
|
Unknown_Error = 4
|
||||||
Max_Iterations_Reached = 5
|
Assignment_To_Immutable = 10
|
||||||
Buffer_Overflow = 6
|
Max_Iterations_Reached = 11
|
||||||
Integer_Overflow = 7
|
Buffer_Overflow = 12
|
||||||
Division_by_Zero = 8
|
Integer_Overflow = 13
|
||||||
Math_Domain_Error = 9
|
Integer_Underflow = 14
|
||||||
Unimplemented = 127
|
Division_by_Zero = 30
|
||||||
|
Math_Domain_Error = 31
|
||||||
|
Cannot_Open_File = 50
|
||||||
|
Cannot_Read_File = 51
|
||||||
|
Cannot_Write_File = 52
|
||||||
|
Unimplemented = 127
|
||||||
|
|
||||||
#
|
#
|
||||||
# Disable garbage collection
|
# Disable garbage collection
|
||||||
|
|||||||
Reference in New Issue
Block a user