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

Add $$is_fpclass builtin. #1000

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 0.5.0 Change List

### Changes / improvements
- Added `$$is_fpclass` builtin.
- Added `$$atomic_fetch_*` builtins.
- vectors may now contain pointers.
- `void!` does not convert to `anyfault`.
Expand Down
1 change: 1 addition & 0 deletions src/compiler/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,7 @@ typedef enum
BUILTIN_FSHR,
BUILTIN_GATHER,
BUILTIN_GET_ROUNDING_MODE,
BUILTIN_IS_FPCLASS,
BUILTIN_LOG,
BUILTIN_LOG10,
BUILTIN_LOG2,
Expand Down
1 change: 1 addition & 0 deletions src/compiler/llvm_codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ static void llvm_codegen_setup()
#else
intrinsic_id.get_rounding = lookup_intrinsic("llvm.get.rounding");
#endif
intrinsic_id.is_fpclass = lookup_intrinsic("llvm.is.fpclass");
intrinsic_id.lifetime_end = lookup_intrinsic("llvm.lifetime.end");
intrinsic_id.lifetime_start = lookup_intrinsic("llvm.lifetime.start");
intrinsic_id.llrint = lookup_intrinsic("llvm.llrint");
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/llvm_codegen_builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,9 @@ void llvm_emit_builtin_call(GenContext *c, BEValue *result_value, Expr *expr)
case BUILTIN_GET_ROUNDING_MODE:
llvm_value_set(result_value, llvm_emit_call_intrinsic(c, intrinsic_id.get_rounding, NULL, 0, NULL, 0), expr->type);
return;
case BUILTIN_IS_FPCLASS:
llvm_emit_simple_builtin(c, result_value, expr, intrinsic_id.is_fpclass);
return;
case BUILTIN_LOG:
llvm_emit_simple_builtin(c, result_value, expr, intrinsic_id.log);
return;
Expand Down
1 change: 1 addition & 0 deletions src/compiler/llvm_codegen_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ typedef struct
unsigned fshr;
unsigned gather;
unsigned get_rounding;
unsigned is_fpclass;
unsigned lifetime_end;
unsigned lifetime_start;
unsigned llrint;
Expand Down
18 changes: 18 additions & 0 deletions src/compiler/sema_builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,23 @@ bool sema_expr_analyse_builtin_call(SemaContext *context, Expr *expr)
break;
case BUILTIN_SYSCALL:
UNREACHABLE
case BUILTIN_IS_FPCLASS:
{
assert(arg_count == 2);
if (!sema_check_builtin_args(args, (BuiltinArg[]) { BA_FLOATLIKE, BA_INTEGER }, 2)) return false;
if (!expr_is_const(args[1])) RETURN_SEMA_ERROR(args[1], "A constant value is required.");
if (!cast_implicit(context, args[1], type_int)) return false;

if (type_flat_is_vector(args[0]->type))
{
rtype = type_get_vector(type_bool, args[0]->type->array.len);
}
else
{
rtype = type_bool;
}
break;
}
case BUILTIN_VECCOMPGE:
case BUILTIN_VECCOMPEQ:
case BUILTIN_VECCOMPLE:
Expand Down Expand Up @@ -933,6 +950,7 @@ static inline int builtin_expected_args(BuiltinFunction func)
case BUILTIN_EXACT_MUL:
case BUILTIN_EXACT_SUB:
case BUILTIN_EXPECT:
case BUILTIN_IS_FPCLASS:
case BUILTIN_MAX:
case BUILTIN_MIN:
case BUILTIN_POW:
Expand Down
1 change: 1 addition & 0 deletions src/compiler/symtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ void symtab_init(uint32_t capacity)
builtin_list[BUILTIN_FSHR] = KW_DEF("fshr");
builtin_list[BUILTIN_GATHER] = KW_DEF("gather");
builtin_list[BUILTIN_GET_ROUNDING_MODE] = KW_DEF("get_rounding_mode");
builtin_list[BUILTIN_IS_FPCLASS] = KW_DEF("is_fpclass");
builtin_list[BUILTIN_LOG] = KW_DEF("log");
builtin_list[BUILTIN_LOG2] = KW_DEF("log2");
builtin_list[BUILTIN_LOG10] = KW_DEF("log10");
Expand Down
24 changes: 24 additions & 0 deletions test/test_suite/builtins/is_fpclass.c3t
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// #target: macos-x64
module test;

fn void main()
{
double d1 = 1.0;
double d2 = double.nan;

double[<*>] vd1 = { 0, 0 };
double[<*>] vd2 = { 0.0, double.nan };

bool b1 = $$is_fpclass(d1, 1);
bool b2 = $$is_fpclass(d2, 2);

bool[<*>] b3 = $$is_fpclass(vd1, 1);
bool[<*>] b4 = $$is_fpclass(vd2, 2);
}

/* #expect: test.ll

%1 = call i1 @llvm.is.fpclass.f64(double %0, i32 1)
%4 = call i1 @llvm.is.fpclass.f64(double %3, i32 2)
%7 = call <2 x i1> @llvm.is.fpclass.v2f64(<2 x double> %6, i32 1)
%10 = call <2 x i1> @llvm.is.fpclass.v2f64(<2 x double> %9, i32 2)