mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
Add intrinsics.constant_(floor|truncate|ceil|round)
This commit is contained in:
@@ -244,6 +244,11 @@ constant_utf16_cstring :: proc($literal: string) -> [^]u16 ---
|
|||||||
|
|
||||||
constant_log2 :: proc($v: $T) -> T where type_is_integer(T) ---
|
constant_log2 :: proc($v: $T) -> T where type_is_integer(T) ---
|
||||||
|
|
||||||
|
constant_floor :: proc($v: $T) -> T where type_is_integer(T) || type_is_float(T) ---
|
||||||
|
constant_truncate :: proc($v: $T) -> T where type_is_integer(T) || type_is_float(T) ---
|
||||||
|
constant_ceil :: proc($v: $T) -> T where type_is_integer(T) || type_is_float(T) ---
|
||||||
|
constant_round :: proc($v: $T) -> T where type_is_integer(T) || type_is_float(T) ---
|
||||||
|
|
||||||
// SIMD related
|
// SIMD related
|
||||||
simd_add :: proc(a, b: #simd[N]T) -> #simd[N]T ---
|
simd_add :: proc(a, b: #simd[N]T) -> #simd[N]T ---
|
||||||
simd_sub :: proc(a, b: #simd[N]T) -> #simd[N]T ---
|
simd_sub :: proc(a, b: #simd[N]T) -> #simd[N]T ---
|
||||||
|
|||||||
@@ -4768,6 +4768,42 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case BuiltinProc_constant_floor:
|
||||||
|
case BuiltinProc_constant_truncate:
|
||||||
|
case BuiltinProc_constant_ceil:
|
||||||
|
case BuiltinProc_constant_round:
|
||||||
|
{
|
||||||
|
Operand o = {};
|
||||||
|
check_expr(c, &o, ce->args[0]);
|
||||||
|
|
||||||
|
if (!is_type_integer_or_float(o.type) && (o.mode != Addressing_Constant)) {
|
||||||
|
error(ce->args[0], "Expected a constant number for '%.*s'", LIT(builtin_name));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
operand->mode = Addressing_Constant;
|
||||||
|
operand->type = o.type;
|
||||||
|
|
||||||
|
ExactValue value = o.value;
|
||||||
|
if (value.kind == ExactValue_Integer) {
|
||||||
|
// do nothing
|
||||||
|
} else if (value.kind == ExactValue_Float) {
|
||||||
|
f64 f = value.value_float;
|
||||||
|
switch (id) {
|
||||||
|
case BuiltinProc_constant_floor: f = floor(f); break;
|
||||||
|
case BuiltinProc_constant_truncate: f = trunc(f); break;
|
||||||
|
case BuiltinProc_constant_ceil: f = ceil(f); break;
|
||||||
|
case BuiltinProc_constant_round: f = round(f); break;
|
||||||
|
default:
|
||||||
|
GB_PANIC("Unhandled built-in: %.*s", LIT(builtin_name));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
value = exact_value_float(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
operand->value = value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case BuiltinProc_soa_struct: {
|
case BuiltinProc_soa_struct: {
|
||||||
Operand x = {};
|
Operand x = {};
|
||||||
Operand y = {};
|
Operand y = {};
|
||||||
|
|||||||
@@ -49,6 +49,11 @@ enum BuiltinProcId {
|
|||||||
|
|
||||||
BuiltinProc_constant_log2,
|
BuiltinProc_constant_log2,
|
||||||
|
|
||||||
|
BuiltinProc_constant_floor,
|
||||||
|
BuiltinProc_constant_truncate,
|
||||||
|
BuiltinProc_constant_ceil,
|
||||||
|
BuiltinProc_constant_round,
|
||||||
|
|
||||||
BuiltinProc_transpose,
|
BuiltinProc_transpose,
|
||||||
BuiltinProc_outer_product,
|
BuiltinProc_outer_product,
|
||||||
BuiltinProc_hadamard_product,
|
BuiltinProc_hadamard_product,
|
||||||
@@ -420,7 +425,11 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = {
|
|||||||
|
|
||||||
{STR_LIT("has_target_feature"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
{STR_LIT("has_target_feature"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
|
|
||||||
{STR_LIT("constant_log2"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
{STR_LIT("constant_log2"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
|
{STR_LIT("constant_floor"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
|
{STR_LIT("constant_truncate"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
|
{STR_LIT("constant_ceil"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
|
{STR_LIT("constant_round"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
|
|
||||||
{STR_LIT("transpose"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
{STR_LIT("transpose"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
{STR_LIT("outer_product"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
{STR_LIT("outer_product"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
|
|||||||
@@ -1296,6 +1296,15 @@ gb_internal bool is_type_rune(Type *t) {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
gb_internal bool is_type_integer_or_float(Type *t) {
|
||||||
|
t = base_type(t);
|
||||||
|
if (t == nullptr) { return false; }
|
||||||
|
if (t->kind == Type_Basic) {
|
||||||
|
return (t->Basic.flags & (BasicFlag_Integer|BasicFlag_Float)) != 0;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
gb_internal bool is_type_numeric(Type *t) {
|
gb_internal bool is_type_numeric(Type *t) {
|
||||||
t = base_type(t);
|
t = base_type(t);
|
||||||
if (t == nullptr) { return false; }
|
if (t == nullptr) { return false; }
|
||||||
|
|||||||
Reference in New Issue
Block a user