From e6268c0ac75f2e7e31a4a9a1eda670be2fc74746 Mon Sep 17 00:00:00 2001 From: Pablo Paglilla Date: Fri, 19 Apr 2024 23:19:05 +0000 Subject: [PATCH] Add support for builtin float comparison operations --- toolchain/check/eval.cpp | 51 ++++ .../check/testdata/builtins/float/eq.carbon | 168 +++++++++++++ .../testdata/builtins/float/greater.carbon | 225 ++++++++++++++++++ .../testdata/builtins/float/greater_eq.carbon | 225 ++++++++++++++++++ .../check/testdata/builtins/float/less.carbon | 225 ++++++++++++++++++ .../testdata/builtins/float/less_eq.carbon | 225 ++++++++++++++++++ .../check/testdata/builtins/float/neq.carbon | 168 +++++++++++++ toolchain/lower/handle.cpp | 34 +++ .../lower/testdata/builtins/float.carbon | 54 +++++ toolchain/sem_ir/builtin_function_kind.cpp | 24 ++ toolchain/sem_ir/builtin_function_kind.def | 8 + 11 files changed, 1407 insertions(+) create mode 100644 toolchain/check/testdata/builtins/float/eq.carbon create mode 100644 toolchain/check/testdata/builtins/float/greater.carbon create mode 100644 toolchain/check/testdata/builtins/float/greater_eq.carbon create mode 100644 toolchain/check/testdata/builtins/float/less.carbon create mode 100644 toolchain/check/testdata/builtins/float/less_eq.carbon create mode 100644 toolchain/check/testdata/builtins/float/neq.carbon diff --git a/toolchain/check/eval.cpp b/toolchain/check/eval.cpp index 26e0d469eeb0c..0451afb6f87a3 100644 --- a/toolchain/check/eval.cpp +++ b/toolchain/check/eval.cpp @@ -650,6 +650,43 @@ static auto PerformBuiltinBinaryFloatOp(Context& context, return MakeFloatResult(context, lhs.type_id, std::move(result_val)); } +// Performs a builtin float comparison. +static auto PerformBuiltinFloatComparison( + Context& context, SemIR::BuiltinFunctionKind builtin_kind, + SemIR::InstId lhs_id, SemIR::InstId rhs_id, SemIR::TypeId bool_type_id) + -> SemIR::ConstantId { + auto lhs = context.insts().GetAs(lhs_id); + auto rhs = context.insts().GetAs(rhs_id); + const auto& lhs_val = context.floats().Get(lhs.float_id); + const auto& rhs_val = context.floats().Get(rhs.float_id); + + bool result; + switch (builtin_kind) { + case SemIR::BuiltinFunctionKind::FloatEq: + result = (lhs_val == rhs_val); + break; + case SemIR::BuiltinFunctionKind::FloatNeq: + result = (lhs_val != rhs_val); + break; + case SemIR::BuiltinFunctionKind::FloatLess: + result = lhs_val < rhs_val; + break; + case SemIR::BuiltinFunctionKind::FloatLessEq: + result = lhs_val <= rhs_val; + break; + case SemIR::BuiltinFunctionKind::FloatGreater: + result = lhs_val > rhs_val; + break; + case SemIR::BuiltinFunctionKind::FloatGreaterEq: + result = lhs_val >= rhs_val; + break; + default: + CARBON_FATAL() << "Unexpected operation kind."; + } + + return MakeBoolResult(context, bool_type_id, result); +} + static auto PerformBuiltinCall(Context& context, SemIRLoc loc, SemIR::Call call, SemIR::BuiltinFunctionKind builtin_kind, llvm::ArrayRef arg_ids, @@ -754,6 +791,20 @@ static auto PerformBuiltinCall(Context& context, SemIRLoc loc, SemIR::Call call, return PerformBuiltinBinaryFloatOp(context, builtin_kind, arg_ids[0], arg_ids[1]); } + + // Float comparisons. + case SemIR::BuiltinFunctionKind::FloatEq: + case SemIR::BuiltinFunctionKind::FloatNeq: + case SemIR::BuiltinFunctionKind::FloatLess: + case SemIR::BuiltinFunctionKind::FloatLessEq: + case SemIR::BuiltinFunctionKind::FloatGreater: + case SemIR::BuiltinFunctionKind::FloatGreaterEq: { + if (phase != Phase::Template) { + break; + } + return PerformBuiltinFloatComparison(context, builtin_kind, arg_ids[0], + arg_ids[1], call.type_id); + } } return SemIR::ConstantId::NotConstant; diff --git a/toolchain/check/testdata/builtins/float/eq.carbon b/toolchain/check/testdata/builtins/float/eq.carbon new file mode 100644 index 0000000000000..06fa1f5b677cd --- /dev/null +++ b/toolchain/check/testdata/builtins/float/eq.carbon @@ -0,0 +1,168 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// AUTOUPDATE + +// --- float_eq.carbon + +fn Eq(a: f64, b: f64) -> bool = "float.eq"; + +class True {} +class False {} + +fn F(true_: True, false_: False) { + true_ as (if Eq(1.0, 1.0) then True else False); + false_ as (if Eq(1.0, 2.0) then True else False); +} + +fn RuntimeCall(a: f64, b: f64) -> bool { + return Eq(a, b); +} + +// --- fail_bad_decl.carbon + +package FailBadDecl api; + +// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+3]]:1: ERROR: Invalid signature for builtin function "float.eq". +// CHECK:STDERR: fn WrongResult(a: f64, b: f64) -> f64 = "float.eq"; +// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +fn WrongResult(a: f64, b: f64) -> f64 = "float.eq"; + +// CHECK:STDOUT: --- float_eq.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %True: type = class_type @True [template] +// CHECK:STDOUT: %.1: type = struct_type {} [template] +// CHECK:STDOUT: %False: type = class_type @False [template] +// CHECK:STDOUT: %.2: type = tuple_type () [template] +// CHECK:STDOUT: %.3: type = ptr_type {} [template] +// CHECK:STDOUT: %.4: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.5: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.6: bool = bool_literal true [template] +// CHECK:STDOUT: %.7: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.8: f64 = float_literal 2 [template] +// CHECK:STDOUT: %.9: bool = bool_literal false [template] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: package: = namespace [template] { +// CHECK:STDOUT: .Core = %Core +// CHECK:STDOUT: .Eq = %Eq +// CHECK:STDOUT: .True = %True.decl +// CHECK:STDOUT: .False = %False.decl +// CHECK:STDOUT: .F = %F +// CHECK:STDOUT: .RuntimeCall = %RuntimeCall +// CHECK:STDOUT: } +// CHECK:STDOUT: %Core: = namespace [template] {} +// CHECK:STDOUT: %Eq: = fn_decl @Eq [template] { +// CHECK:STDOUT: %a.loc2_7.1: f64 = param a +// CHECK:STDOUT: @Eq.%a: f64 = bind_name a, %a.loc2_7.1 +// CHECK:STDOUT: %b.loc2_15.1: f64 = param b +// CHECK:STDOUT: @Eq.%b: f64 = bind_name b, %b.loc2_15.1 +// CHECK:STDOUT: @Eq.%return: ref bool = var +// CHECK:STDOUT: } +// CHECK:STDOUT: %True.decl: type = class_decl @True [template = constants.%True] {} +// CHECK:STDOUT: %False.decl: type = class_decl @False [template = constants.%False] {} +// CHECK:STDOUT: %F: = fn_decl @F [template] { +// CHECK:STDOUT: %True.ref: type = name_ref True, %True.decl [template = constants.%True] +// CHECK:STDOUT: %true_.loc7_6.1: True = param true_ +// CHECK:STDOUT: @F.%true_: True = bind_name true_, %true_.loc7_6.1 +// CHECK:STDOUT: %False.ref: type = name_ref False, %False.decl [template = constants.%False] +// CHECK:STDOUT: %false_.loc7_19.1: False = param false_ +// CHECK:STDOUT: @F.%false_: False = bind_name false_, %false_.loc7_19.1 +// CHECK:STDOUT: } +// CHECK:STDOUT: %RuntimeCall: = fn_decl @RuntimeCall [template] { +// CHECK:STDOUT: %a.loc12_16.1: f64 = param a +// CHECK:STDOUT: @RuntimeCall.%a: f64 = bind_name a, %a.loc12_16.1 +// CHECK:STDOUT: %b.loc12_24.1: f64 = param b +// CHECK:STDOUT: @RuntimeCall.%b: f64 = bind_name b, %b.loc12_24.1 +// CHECK:STDOUT: @RuntimeCall.%return: ref bool = var +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: class @True { +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = constants.%True +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: class @False { +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = constants.%False +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @Eq(%a: f64, %b: f64) -> bool = "float.eq"; +// CHECK:STDOUT: +// CHECK:STDOUT: fn @F(%true_: True, %false_: False) { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %true_.ref: True = name_ref true_, %true_ +// CHECK:STDOUT: %Eq.ref.loc8: = name_ref Eq, file.%Eq [template = file.%Eq] +// CHECK:STDOUT: %.loc8_19: f64 = float_literal 1 [template = constants.%.4] +// CHECK:STDOUT: %.loc8_24: f64 = float_literal 1 [template = constants.%.5] +// CHECK:STDOUT: %float.eq.loc8: init bool = call %Eq.ref.loc8(%.loc8_19, %.loc8_24) [template = constants.%.6] +// CHECK:STDOUT: %.loc8_13.1: bool = value_of_initializer %float.eq.loc8 [template = constants.%.6] +// CHECK:STDOUT: %.loc8_13.2: bool = converted %float.eq.loc8, %.loc8_13.1 [template = constants.%.6] +// CHECK:STDOUT: if %.loc8_13.2 br !if.expr.then.loc8 else br !if.expr.else.loc8 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc8: +// CHECK:STDOUT: %True.ref.loc8: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc8(%True.ref.loc8) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc8: +// CHECK:STDOUT: %False.ref.loc8: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc8(%False.ref.loc8) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc8: +// CHECK:STDOUT: %.loc8_13.3: type = block_arg !if.expr.result.loc8 [template = constants.%True] +// CHECK:STDOUT: %false_.ref: False = name_ref false_, %false_ +// CHECK:STDOUT: %Eq.ref.loc9: = name_ref Eq, file.%Eq [template = file.%Eq] +// CHECK:STDOUT: %.loc9_20: f64 = float_literal 1 [template = constants.%.7] +// CHECK:STDOUT: %.loc9_25: f64 = float_literal 2 [template = constants.%.8] +// CHECK:STDOUT: %float.eq.loc9: init bool = call %Eq.ref.loc9(%.loc9_20, %.loc9_25) [template = constants.%.9] +// CHECK:STDOUT: %.loc9_14.1: bool = value_of_initializer %float.eq.loc9 [template = constants.%.9] +// CHECK:STDOUT: %.loc9_14.2: bool = converted %float.eq.loc9, %.loc9_14.1 [template = constants.%.9] +// CHECK:STDOUT: if %.loc9_14.2 br !if.expr.then.loc9 else br !if.expr.else.loc9 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc9: +// CHECK:STDOUT: %True.ref.loc9: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc9(%True.ref.loc9) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc9: +// CHECK:STDOUT: %False.ref.loc9: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc9(%False.ref.loc9) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc9: +// CHECK:STDOUT: %.loc9_14.3: type = block_arg !if.expr.result.loc9 [template = constants.%False] +// CHECK:STDOUT: return +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @RuntimeCall(%a: f64, %b: f64) -> bool { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %Eq.ref: = name_ref Eq, file.%Eq [template = file.%Eq] +// CHECK:STDOUT: %a.ref: f64 = name_ref a, %a +// CHECK:STDOUT: %b.ref: f64 = name_ref b, %b +// CHECK:STDOUT: %float.eq: init bool = call %Eq.ref(%a.ref, %b.ref) +// CHECK:STDOUT: %.loc13_18.1: bool = value_of_initializer %float.eq +// CHECK:STDOUT: %.loc13_18.2: bool = converted %float.eq, %.loc13_18.1 +// CHECK:STDOUT: return %.loc13_18.2 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: --- fail_bad_decl.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: package: = namespace [template] { +// CHECK:STDOUT: .Core = %Core +// CHECK:STDOUT: .WrongResult = %WrongResult +// CHECK:STDOUT: } +// CHECK:STDOUT: %Core: = namespace [template] {} +// CHECK:STDOUT: %WrongResult: = fn_decl @WrongResult [template] { +// CHECK:STDOUT: %a.loc7_16.1: f64 = param a +// CHECK:STDOUT: @WrongResult.%a: f64 = bind_name a, %a.loc7_16.1 +// CHECK:STDOUT: %b.loc7_24.1: f64 = param b +// CHECK:STDOUT: @WrongResult.%b: f64 = bind_name b, %b.loc7_24.1 +// CHECK:STDOUT: @WrongResult.%return: ref f64 = var +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @WrongResult(%a: f64, %b: f64) -> f64; +// CHECK:STDOUT: diff --git a/toolchain/check/testdata/builtins/float/greater.carbon b/toolchain/check/testdata/builtins/float/greater.carbon new file mode 100644 index 0000000000000..3938c5b26ec5e --- /dev/null +++ b/toolchain/check/testdata/builtins/float/greater.carbon @@ -0,0 +1,225 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// AUTOUPDATE + +// --- float_greater.carbon + +fn Greater(a: f64, b: f64) -> bool = "float.greater"; +fn Negate(a: f64) -> f64 = "float.negate"; + +class True {} +class False {} + +fn F(true_: True, false_: False) { + false_ as (if Greater(1.0, 2.0) then True else False); + false_ as (if Greater(1.0, 1.0) then True else False); + true_ as (if Greater(1.0, 0.0) then True else False); + false_ as (if Greater(Negate(1.0), 0.0) then True else False); + true_ as (if Greater(0.0, Negate(1.0)) then True else False); +} + +fn RuntimeCall(a: f64, b: f64) -> bool { + return Greater(a, b); +} + +// CHECK:STDOUT: --- float_greater.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %True: type = class_type @True [template] +// CHECK:STDOUT: %.1: type = struct_type {} [template] +// CHECK:STDOUT: %False: type = class_type @False [template] +// CHECK:STDOUT: %.2: type = tuple_type () [template] +// CHECK:STDOUT: %.3: type = ptr_type {} [template] +// CHECK:STDOUT: %.4: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.5: f64 = float_literal 2 [template] +// CHECK:STDOUT: %.6: bool = bool_literal false [template] +// CHECK:STDOUT: %.7: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.8: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.9: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.10: f64 = float_literal 0 [template] +// CHECK:STDOUT: %.11: bool = bool_literal true [template] +// CHECK:STDOUT: %.12: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.13: f64 = float_literal -1 [template] +// CHECK:STDOUT: %.14: f64 = float_literal 0 [template] +// CHECK:STDOUT: %.15: f64 = float_literal 0 [template] +// CHECK:STDOUT: %.16: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.17: f64 = float_literal -1 [template] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: package: = namespace [template] { +// CHECK:STDOUT: .Core = %Core +// CHECK:STDOUT: .Greater = %Greater +// CHECK:STDOUT: .Negate = %Negate +// CHECK:STDOUT: .True = %True.decl +// CHECK:STDOUT: .False = %False.decl +// CHECK:STDOUT: .F = %F +// CHECK:STDOUT: .RuntimeCall = %RuntimeCall +// CHECK:STDOUT: } +// CHECK:STDOUT: %Core: = namespace [template] {} +// CHECK:STDOUT: %Greater: = fn_decl @Greater [template] { +// CHECK:STDOUT: %a.loc2_12.1: f64 = param a +// CHECK:STDOUT: @Greater.%a: f64 = bind_name a, %a.loc2_12.1 +// CHECK:STDOUT: %b.loc2_20.1: f64 = param b +// CHECK:STDOUT: @Greater.%b: f64 = bind_name b, %b.loc2_20.1 +// CHECK:STDOUT: @Greater.%return: ref bool = var +// CHECK:STDOUT: } +// CHECK:STDOUT: %Negate: = fn_decl @Negate [template] { +// CHECK:STDOUT: %a.loc3_11.1: f64 = param a +// CHECK:STDOUT: @Negate.%a: f64 = bind_name a, %a.loc3_11.1 +// CHECK:STDOUT: @Negate.%return: ref f64 = var +// CHECK:STDOUT: } +// CHECK:STDOUT: %True.decl: type = class_decl @True [template = constants.%True] {} +// CHECK:STDOUT: %False.decl: type = class_decl @False [template = constants.%False] {} +// CHECK:STDOUT: %F: = fn_decl @F [template] { +// CHECK:STDOUT: %True.ref: type = name_ref True, %True.decl [template = constants.%True] +// CHECK:STDOUT: %true_.loc8_6.1: True = param true_ +// CHECK:STDOUT: @F.%true_: True = bind_name true_, %true_.loc8_6.1 +// CHECK:STDOUT: %False.ref: type = name_ref False, %False.decl [template = constants.%False] +// CHECK:STDOUT: %false_.loc8_19.1: False = param false_ +// CHECK:STDOUT: @F.%false_: False = bind_name false_, %false_.loc8_19.1 +// CHECK:STDOUT: } +// CHECK:STDOUT: %RuntimeCall: = fn_decl @RuntimeCall [template] { +// CHECK:STDOUT: %a.loc16_16.1: f64 = param a +// CHECK:STDOUT: @RuntimeCall.%a: f64 = bind_name a, %a.loc16_16.1 +// CHECK:STDOUT: %b.loc16_24.1: f64 = param b +// CHECK:STDOUT: @RuntimeCall.%b: f64 = bind_name b, %b.loc16_24.1 +// CHECK:STDOUT: @RuntimeCall.%return: ref bool = var +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: class @True { +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = constants.%True +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: class @False { +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = constants.%False +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @Greater(%a: f64, %b: f64) -> bool = "float.greater"; +// CHECK:STDOUT: +// CHECK:STDOUT: fn @Negate(%a: f64) -> f64 = "float.negate"; +// CHECK:STDOUT: +// CHECK:STDOUT: fn @F(%true_: True, %false_: False) { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %false_.ref.loc9: False = name_ref false_, %false_ +// CHECK:STDOUT: %Greater.ref.loc9: = name_ref Greater, file.%Greater [template = file.%Greater] +// CHECK:STDOUT: %.loc9_25: f64 = float_literal 1 [template = constants.%.4] +// CHECK:STDOUT: %.loc9_30: f64 = float_literal 2 [template = constants.%.5] +// CHECK:STDOUT: %float.greater.loc9: init bool = call %Greater.ref.loc9(%.loc9_25, %.loc9_30) [template = constants.%.6] +// CHECK:STDOUT: %.loc9_14.1: bool = value_of_initializer %float.greater.loc9 [template = constants.%.6] +// CHECK:STDOUT: %.loc9_14.2: bool = converted %float.greater.loc9, %.loc9_14.1 [template = constants.%.6] +// CHECK:STDOUT: if %.loc9_14.2 br !if.expr.then.loc9 else br !if.expr.else.loc9 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc9: +// CHECK:STDOUT: %True.ref.loc9: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc9(%True.ref.loc9) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc9: +// CHECK:STDOUT: %False.ref.loc9: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc9(%False.ref.loc9) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc9: +// CHECK:STDOUT: %.loc9_14.3: type = block_arg !if.expr.result.loc9 [template = constants.%False] +// CHECK:STDOUT: %false_.ref.loc10: False = name_ref false_, %false_ +// CHECK:STDOUT: %Greater.ref.loc10: = name_ref Greater, file.%Greater [template = file.%Greater] +// CHECK:STDOUT: %.loc10_25: f64 = float_literal 1 [template = constants.%.7] +// CHECK:STDOUT: %.loc10_30: f64 = float_literal 1 [template = constants.%.8] +// CHECK:STDOUT: %float.greater.loc10: init bool = call %Greater.ref.loc10(%.loc10_25, %.loc10_30) [template = constants.%.6] +// CHECK:STDOUT: %.loc10_14.1: bool = value_of_initializer %float.greater.loc10 [template = constants.%.6] +// CHECK:STDOUT: %.loc10_14.2: bool = converted %float.greater.loc10, %.loc10_14.1 [template = constants.%.6] +// CHECK:STDOUT: if %.loc10_14.2 br !if.expr.then.loc10 else br !if.expr.else.loc10 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc10: +// CHECK:STDOUT: %True.ref.loc10: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc10(%True.ref.loc10) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc10: +// CHECK:STDOUT: %False.ref.loc10: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc10(%False.ref.loc10) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc10: +// CHECK:STDOUT: %.loc10_14.3: type = block_arg !if.expr.result.loc10 [template = constants.%False] +// CHECK:STDOUT: %true_.ref.loc11: True = name_ref true_, %true_ +// CHECK:STDOUT: %Greater.ref.loc11: = name_ref Greater, file.%Greater [template = file.%Greater] +// CHECK:STDOUT: %.loc11_24: f64 = float_literal 1 [template = constants.%.9] +// CHECK:STDOUT: %.loc11_29: f64 = float_literal 0 [template = constants.%.10] +// CHECK:STDOUT: %float.greater.loc11: init bool = call %Greater.ref.loc11(%.loc11_24, %.loc11_29) [template = constants.%.11] +// CHECK:STDOUT: %.loc11_13.1: bool = value_of_initializer %float.greater.loc11 [template = constants.%.11] +// CHECK:STDOUT: %.loc11_13.2: bool = converted %float.greater.loc11, %.loc11_13.1 [template = constants.%.11] +// CHECK:STDOUT: if %.loc11_13.2 br !if.expr.then.loc11 else br !if.expr.else.loc11 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc11: +// CHECK:STDOUT: %True.ref.loc11: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc11(%True.ref.loc11) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc11: +// CHECK:STDOUT: %False.ref.loc11: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc11(%False.ref.loc11) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc11: +// CHECK:STDOUT: %.loc11_13.3: type = block_arg !if.expr.result.loc11 [template = constants.%True] +// CHECK:STDOUT: %false_.ref.loc12: False = name_ref false_, %false_ +// CHECK:STDOUT: %Greater.ref.loc12: = name_ref Greater, file.%Greater [template = file.%Greater] +// CHECK:STDOUT: %Negate.ref.loc12: = name_ref Negate, file.%Negate [template = file.%Negate] +// CHECK:STDOUT: %.loc12_32: f64 = float_literal 1 [template = constants.%.12] +// CHECK:STDOUT: %float.negate.loc12: init f64 = call %Negate.ref.loc12(%.loc12_32) [template = constants.%.13] +// CHECK:STDOUT: %.loc12_38: f64 = float_literal 0 [template = constants.%.14] +// CHECK:STDOUT: %.loc12_24.1: f64 = value_of_initializer %float.negate.loc12 [template = constants.%.13] +// CHECK:STDOUT: %.loc12_24.2: f64 = converted %float.negate.loc12, %.loc12_24.1 [template = constants.%.13] +// CHECK:STDOUT: %float.greater.loc12: init bool = call %Greater.ref.loc12(%.loc12_24.2, %.loc12_38) [template = constants.%.6] +// CHECK:STDOUT: %.loc12_14.1: bool = value_of_initializer %float.greater.loc12 [template = constants.%.6] +// CHECK:STDOUT: %.loc12_14.2: bool = converted %float.greater.loc12, %.loc12_14.1 [template = constants.%.6] +// CHECK:STDOUT: if %.loc12_14.2 br !if.expr.then.loc12 else br !if.expr.else.loc12 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc12: +// CHECK:STDOUT: %True.ref.loc12: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc12(%True.ref.loc12) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc12: +// CHECK:STDOUT: %False.ref.loc12: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc12(%False.ref.loc12) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc12: +// CHECK:STDOUT: %.loc12_14.3: type = block_arg !if.expr.result.loc12 [template = constants.%False] +// CHECK:STDOUT: %true_.ref.loc13: True = name_ref true_, %true_ +// CHECK:STDOUT: %Greater.ref.loc13: = name_ref Greater, file.%Greater [template = file.%Greater] +// CHECK:STDOUT: %.loc13_24: f64 = float_literal 0 [template = constants.%.15] +// CHECK:STDOUT: %Negate.ref.loc13: = name_ref Negate, file.%Negate [template = file.%Negate] +// CHECK:STDOUT: %.loc13_36: f64 = float_literal 1 [template = constants.%.16] +// CHECK:STDOUT: %float.negate.loc13: init f64 = call %Negate.ref.loc13(%.loc13_36) [template = constants.%.17] +// CHECK:STDOUT: %.loc13_23.1: f64 = value_of_initializer %float.negate.loc13 [template = constants.%.17] +// CHECK:STDOUT: %.loc13_23.2: f64 = converted %float.negate.loc13, %.loc13_23.1 [template = constants.%.17] +// CHECK:STDOUT: %float.greater.loc13: init bool = call %Greater.ref.loc13(%.loc13_24, %.loc13_23.2) [template = constants.%.11] +// CHECK:STDOUT: %.loc13_13.1: bool = value_of_initializer %float.greater.loc13 [template = constants.%.11] +// CHECK:STDOUT: %.loc13_13.2: bool = converted %float.greater.loc13, %.loc13_13.1 [template = constants.%.11] +// CHECK:STDOUT: if %.loc13_13.2 br !if.expr.then.loc13 else br !if.expr.else.loc13 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc13: +// CHECK:STDOUT: %True.ref.loc13: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc13(%True.ref.loc13) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc13: +// CHECK:STDOUT: %False.ref.loc13: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc13(%False.ref.loc13) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc13: +// CHECK:STDOUT: %.loc13_13.3: type = block_arg !if.expr.result.loc13 [template = constants.%True] +// CHECK:STDOUT: return +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @RuntimeCall(%a: f64, %b: f64) -> bool { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %Greater.ref: = name_ref Greater, file.%Greater [template = file.%Greater] +// CHECK:STDOUT: %a.ref: f64 = name_ref a, %a +// CHECK:STDOUT: %b.ref: f64 = name_ref b, %b +// CHECK:STDOUT: %float.greater: init bool = call %Greater.ref(%a.ref, %b.ref) +// CHECK:STDOUT: %.loc17_23.1: bool = value_of_initializer %float.greater +// CHECK:STDOUT: %.loc17_23.2: bool = converted %float.greater, %.loc17_23.1 +// CHECK:STDOUT: return %.loc17_23.2 +// CHECK:STDOUT: } +// CHECK:STDOUT: diff --git a/toolchain/check/testdata/builtins/float/greater_eq.carbon b/toolchain/check/testdata/builtins/float/greater_eq.carbon new file mode 100644 index 0000000000000..1817a887ead4b --- /dev/null +++ b/toolchain/check/testdata/builtins/float/greater_eq.carbon @@ -0,0 +1,225 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// AUTOUPDATE + +// --- float_greater_eq.carbon + +fn GreaterEq(a: f64, b: f64) -> bool = "float.greater_eq"; +fn Negate(a: f64) -> f64 = "float.negate"; + +class True {} +class False {} + +fn F(true_: True, false_: False) { + false_ as (if GreaterEq(1.0, 2.0) then True else False); + true_ as (if GreaterEq(1.0, 1.0) then True else False); + true_ as (if GreaterEq(1.0, 0.0) then True else False); + false_ as (if GreaterEq(Negate(1.0), 0.0) then True else False); + true_ as (if GreaterEq(0.0, Negate(1.0)) then True else False); +} + +fn RuntimeCall(a: f64, b: f64) -> bool { + return GreaterEq(a, b); +} + +// CHECK:STDOUT: --- float_greater_eq.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %True: type = class_type @True [template] +// CHECK:STDOUT: %.1: type = struct_type {} [template] +// CHECK:STDOUT: %False: type = class_type @False [template] +// CHECK:STDOUT: %.2: type = tuple_type () [template] +// CHECK:STDOUT: %.3: type = ptr_type {} [template] +// CHECK:STDOUT: %.4: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.5: f64 = float_literal 2 [template] +// CHECK:STDOUT: %.6: bool = bool_literal false [template] +// CHECK:STDOUT: %.7: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.8: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.9: bool = bool_literal true [template] +// CHECK:STDOUT: %.10: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.11: f64 = float_literal 0 [template] +// CHECK:STDOUT: %.12: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.13: f64 = float_literal -1 [template] +// CHECK:STDOUT: %.14: f64 = float_literal 0 [template] +// CHECK:STDOUT: %.15: f64 = float_literal 0 [template] +// CHECK:STDOUT: %.16: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.17: f64 = float_literal -1 [template] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: package: = namespace [template] { +// CHECK:STDOUT: .Core = %Core +// CHECK:STDOUT: .GreaterEq = %GreaterEq +// CHECK:STDOUT: .Negate = %Negate +// CHECK:STDOUT: .True = %True.decl +// CHECK:STDOUT: .False = %False.decl +// CHECK:STDOUT: .F = %F +// CHECK:STDOUT: .RuntimeCall = %RuntimeCall +// CHECK:STDOUT: } +// CHECK:STDOUT: %Core: = namespace [template] {} +// CHECK:STDOUT: %GreaterEq: = fn_decl @GreaterEq [template] { +// CHECK:STDOUT: %a.loc2_14.1: f64 = param a +// CHECK:STDOUT: @GreaterEq.%a: f64 = bind_name a, %a.loc2_14.1 +// CHECK:STDOUT: %b.loc2_22.1: f64 = param b +// CHECK:STDOUT: @GreaterEq.%b: f64 = bind_name b, %b.loc2_22.1 +// CHECK:STDOUT: @GreaterEq.%return: ref bool = var +// CHECK:STDOUT: } +// CHECK:STDOUT: %Negate: = fn_decl @Negate [template] { +// CHECK:STDOUT: %a.loc3_11.1: f64 = param a +// CHECK:STDOUT: @Negate.%a: f64 = bind_name a, %a.loc3_11.1 +// CHECK:STDOUT: @Negate.%return: ref f64 = var +// CHECK:STDOUT: } +// CHECK:STDOUT: %True.decl: type = class_decl @True [template = constants.%True] {} +// CHECK:STDOUT: %False.decl: type = class_decl @False [template = constants.%False] {} +// CHECK:STDOUT: %F: = fn_decl @F [template] { +// CHECK:STDOUT: %True.ref: type = name_ref True, %True.decl [template = constants.%True] +// CHECK:STDOUT: %true_.loc8_6.1: True = param true_ +// CHECK:STDOUT: @F.%true_: True = bind_name true_, %true_.loc8_6.1 +// CHECK:STDOUT: %False.ref: type = name_ref False, %False.decl [template = constants.%False] +// CHECK:STDOUT: %false_.loc8_19.1: False = param false_ +// CHECK:STDOUT: @F.%false_: False = bind_name false_, %false_.loc8_19.1 +// CHECK:STDOUT: } +// CHECK:STDOUT: %RuntimeCall: = fn_decl @RuntimeCall [template] { +// CHECK:STDOUT: %a.loc16_16.1: f64 = param a +// CHECK:STDOUT: @RuntimeCall.%a: f64 = bind_name a, %a.loc16_16.1 +// CHECK:STDOUT: %b.loc16_24.1: f64 = param b +// CHECK:STDOUT: @RuntimeCall.%b: f64 = bind_name b, %b.loc16_24.1 +// CHECK:STDOUT: @RuntimeCall.%return: ref bool = var +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: class @True { +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = constants.%True +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: class @False { +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = constants.%False +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @GreaterEq(%a: f64, %b: f64) -> bool = "float.greater_eq"; +// CHECK:STDOUT: +// CHECK:STDOUT: fn @Negate(%a: f64) -> f64 = "float.negate"; +// CHECK:STDOUT: +// CHECK:STDOUT: fn @F(%true_: True, %false_: False) { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %false_.ref.loc9: False = name_ref false_, %false_ +// CHECK:STDOUT: %GreaterEq.ref.loc9: = name_ref GreaterEq, file.%GreaterEq [template = file.%GreaterEq] +// CHECK:STDOUT: %.loc9_27: f64 = float_literal 1 [template = constants.%.4] +// CHECK:STDOUT: %.loc9_32: f64 = float_literal 2 [template = constants.%.5] +// CHECK:STDOUT: %float.greater_eq.loc9: init bool = call %GreaterEq.ref.loc9(%.loc9_27, %.loc9_32) [template = constants.%.6] +// CHECK:STDOUT: %.loc9_14.1: bool = value_of_initializer %float.greater_eq.loc9 [template = constants.%.6] +// CHECK:STDOUT: %.loc9_14.2: bool = converted %float.greater_eq.loc9, %.loc9_14.1 [template = constants.%.6] +// CHECK:STDOUT: if %.loc9_14.2 br !if.expr.then.loc9 else br !if.expr.else.loc9 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc9: +// CHECK:STDOUT: %True.ref.loc9: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc9(%True.ref.loc9) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc9: +// CHECK:STDOUT: %False.ref.loc9: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc9(%False.ref.loc9) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc9: +// CHECK:STDOUT: %.loc9_14.3: type = block_arg !if.expr.result.loc9 [template = constants.%False] +// CHECK:STDOUT: %true_.ref.loc10: True = name_ref true_, %true_ +// CHECK:STDOUT: %GreaterEq.ref.loc10: = name_ref GreaterEq, file.%GreaterEq [template = file.%GreaterEq] +// CHECK:STDOUT: %.loc10_26: f64 = float_literal 1 [template = constants.%.7] +// CHECK:STDOUT: %.loc10_31: f64 = float_literal 1 [template = constants.%.8] +// CHECK:STDOUT: %float.greater_eq.loc10: init bool = call %GreaterEq.ref.loc10(%.loc10_26, %.loc10_31) [template = constants.%.9] +// CHECK:STDOUT: %.loc10_13.1: bool = value_of_initializer %float.greater_eq.loc10 [template = constants.%.9] +// CHECK:STDOUT: %.loc10_13.2: bool = converted %float.greater_eq.loc10, %.loc10_13.1 [template = constants.%.9] +// CHECK:STDOUT: if %.loc10_13.2 br !if.expr.then.loc10 else br !if.expr.else.loc10 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc10: +// CHECK:STDOUT: %True.ref.loc10: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc10(%True.ref.loc10) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc10: +// CHECK:STDOUT: %False.ref.loc10: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc10(%False.ref.loc10) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc10: +// CHECK:STDOUT: %.loc10_13.3: type = block_arg !if.expr.result.loc10 [template = constants.%True] +// CHECK:STDOUT: %true_.ref.loc11: True = name_ref true_, %true_ +// CHECK:STDOUT: %GreaterEq.ref.loc11: = name_ref GreaterEq, file.%GreaterEq [template = file.%GreaterEq] +// CHECK:STDOUT: %.loc11_26: f64 = float_literal 1 [template = constants.%.10] +// CHECK:STDOUT: %.loc11_31: f64 = float_literal 0 [template = constants.%.11] +// CHECK:STDOUT: %float.greater_eq.loc11: init bool = call %GreaterEq.ref.loc11(%.loc11_26, %.loc11_31) [template = constants.%.9] +// CHECK:STDOUT: %.loc11_13.1: bool = value_of_initializer %float.greater_eq.loc11 [template = constants.%.9] +// CHECK:STDOUT: %.loc11_13.2: bool = converted %float.greater_eq.loc11, %.loc11_13.1 [template = constants.%.9] +// CHECK:STDOUT: if %.loc11_13.2 br !if.expr.then.loc11 else br !if.expr.else.loc11 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc11: +// CHECK:STDOUT: %True.ref.loc11: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc11(%True.ref.loc11) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc11: +// CHECK:STDOUT: %False.ref.loc11: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc11(%False.ref.loc11) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc11: +// CHECK:STDOUT: %.loc11_13.3: type = block_arg !if.expr.result.loc11 [template = constants.%True] +// CHECK:STDOUT: %false_.ref.loc12: False = name_ref false_, %false_ +// CHECK:STDOUT: %GreaterEq.ref.loc12: = name_ref GreaterEq, file.%GreaterEq [template = file.%GreaterEq] +// CHECK:STDOUT: %Negate.ref.loc12: = name_ref Negate, file.%Negate [template = file.%Negate] +// CHECK:STDOUT: %.loc12_34: f64 = float_literal 1 [template = constants.%.12] +// CHECK:STDOUT: %float.negate.loc12: init f64 = call %Negate.ref.loc12(%.loc12_34) [template = constants.%.13] +// CHECK:STDOUT: %.loc12_40: f64 = float_literal 0 [template = constants.%.14] +// CHECK:STDOUT: %.loc12_26.1: f64 = value_of_initializer %float.negate.loc12 [template = constants.%.13] +// CHECK:STDOUT: %.loc12_26.2: f64 = converted %float.negate.loc12, %.loc12_26.1 [template = constants.%.13] +// CHECK:STDOUT: %float.greater_eq.loc12: init bool = call %GreaterEq.ref.loc12(%.loc12_26.2, %.loc12_40) [template = constants.%.6] +// CHECK:STDOUT: %.loc12_14.1: bool = value_of_initializer %float.greater_eq.loc12 [template = constants.%.6] +// CHECK:STDOUT: %.loc12_14.2: bool = converted %float.greater_eq.loc12, %.loc12_14.1 [template = constants.%.6] +// CHECK:STDOUT: if %.loc12_14.2 br !if.expr.then.loc12 else br !if.expr.else.loc12 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc12: +// CHECK:STDOUT: %True.ref.loc12: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc12(%True.ref.loc12) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc12: +// CHECK:STDOUT: %False.ref.loc12: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc12(%False.ref.loc12) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc12: +// CHECK:STDOUT: %.loc12_14.3: type = block_arg !if.expr.result.loc12 [template = constants.%False] +// CHECK:STDOUT: %true_.ref.loc13: True = name_ref true_, %true_ +// CHECK:STDOUT: %GreaterEq.ref.loc13: = name_ref GreaterEq, file.%GreaterEq [template = file.%GreaterEq] +// CHECK:STDOUT: %.loc13_26: f64 = float_literal 0 [template = constants.%.15] +// CHECK:STDOUT: %Negate.ref.loc13: = name_ref Negate, file.%Negate [template = file.%Negate] +// CHECK:STDOUT: %.loc13_38: f64 = float_literal 1 [template = constants.%.16] +// CHECK:STDOUT: %float.negate.loc13: init f64 = call %Negate.ref.loc13(%.loc13_38) [template = constants.%.17] +// CHECK:STDOUT: %.loc13_25.1: f64 = value_of_initializer %float.negate.loc13 [template = constants.%.17] +// CHECK:STDOUT: %.loc13_25.2: f64 = converted %float.negate.loc13, %.loc13_25.1 [template = constants.%.17] +// CHECK:STDOUT: %float.greater_eq.loc13: init bool = call %GreaterEq.ref.loc13(%.loc13_26, %.loc13_25.2) [template = constants.%.9] +// CHECK:STDOUT: %.loc13_13.1: bool = value_of_initializer %float.greater_eq.loc13 [template = constants.%.9] +// CHECK:STDOUT: %.loc13_13.2: bool = converted %float.greater_eq.loc13, %.loc13_13.1 [template = constants.%.9] +// CHECK:STDOUT: if %.loc13_13.2 br !if.expr.then.loc13 else br !if.expr.else.loc13 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc13: +// CHECK:STDOUT: %True.ref.loc13: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc13(%True.ref.loc13) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc13: +// CHECK:STDOUT: %False.ref.loc13: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc13(%False.ref.loc13) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc13: +// CHECK:STDOUT: %.loc13_13.3: type = block_arg !if.expr.result.loc13 [template = constants.%True] +// CHECK:STDOUT: return +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @RuntimeCall(%a: f64, %b: f64) -> bool { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %GreaterEq.ref: = name_ref GreaterEq, file.%GreaterEq [template = file.%GreaterEq] +// CHECK:STDOUT: %a.ref: f64 = name_ref a, %a +// CHECK:STDOUT: %b.ref: f64 = name_ref b, %b +// CHECK:STDOUT: %float.greater_eq: init bool = call %GreaterEq.ref(%a.ref, %b.ref) +// CHECK:STDOUT: %.loc17_25.1: bool = value_of_initializer %float.greater_eq +// CHECK:STDOUT: %.loc17_25.2: bool = converted %float.greater_eq, %.loc17_25.1 +// CHECK:STDOUT: return %.loc17_25.2 +// CHECK:STDOUT: } +// CHECK:STDOUT: diff --git a/toolchain/check/testdata/builtins/float/less.carbon b/toolchain/check/testdata/builtins/float/less.carbon new file mode 100644 index 0000000000000..14d6e2b8a38e8 --- /dev/null +++ b/toolchain/check/testdata/builtins/float/less.carbon @@ -0,0 +1,225 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// AUTOUPDATE + +// --- float_less.carbon + +fn Less(a: f64, b: f64) -> bool = "float.less"; +fn Negate(a: f64) -> f64 = "float.negate"; + +class True {} +class False {} + +fn F(true_: True, false_: False) { + true_ as (if Less(1.0, 2.0) then True else False); + false_ as (if Less(1.0, 1.0) then True else False); + false_ as (if Less(1.0, 0.0) then True else False); + true_ as (if Less(Negate(1.0), 0.0) then True else False); + false_ as (if Less(0.0, Negate(1.0)) then True else False); +} + +fn RuntimeCall(a: f64, b: f64) -> bool { + return Less(a, b); +} + +// CHECK:STDOUT: --- float_less.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %True: type = class_type @True [template] +// CHECK:STDOUT: %.1: type = struct_type {} [template] +// CHECK:STDOUT: %False: type = class_type @False [template] +// CHECK:STDOUT: %.2: type = tuple_type () [template] +// CHECK:STDOUT: %.3: type = ptr_type {} [template] +// CHECK:STDOUT: %.4: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.5: f64 = float_literal 2 [template] +// CHECK:STDOUT: %.6: bool = bool_literal true [template] +// CHECK:STDOUT: %.7: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.8: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.9: bool = bool_literal false [template] +// CHECK:STDOUT: %.10: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.11: f64 = float_literal 0 [template] +// CHECK:STDOUT: %.12: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.13: f64 = float_literal -1 [template] +// CHECK:STDOUT: %.14: f64 = float_literal 0 [template] +// CHECK:STDOUT: %.15: f64 = float_literal 0 [template] +// CHECK:STDOUT: %.16: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.17: f64 = float_literal -1 [template] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: package: = namespace [template] { +// CHECK:STDOUT: .Core = %Core +// CHECK:STDOUT: .Less = %Less +// CHECK:STDOUT: .Negate = %Negate +// CHECK:STDOUT: .True = %True.decl +// CHECK:STDOUT: .False = %False.decl +// CHECK:STDOUT: .F = %F +// CHECK:STDOUT: .RuntimeCall = %RuntimeCall +// CHECK:STDOUT: } +// CHECK:STDOUT: %Core: = namespace [template] {} +// CHECK:STDOUT: %Less: = fn_decl @Less [template] { +// CHECK:STDOUT: %a.loc2_9.1: f64 = param a +// CHECK:STDOUT: @Less.%a: f64 = bind_name a, %a.loc2_9.1 +// CHECK:STDOUT: %b.loc2_17.1: f64 = param b +// CHECK:STDOUT: @Less.%b: f64 = bind_name b, %b.loc2_17.1 +// CHECK:STDOUT: @Less.%return: ref bool = var +// CHECK:STDOUT: } +// CHECK:STDOUT: %Negate: = fn_decl @Negate [template] { +// CHECK:STDOUT: %a.loc3_11.1: f64 = param a +// CHECK:STDOUT: @Negate.%a: f64 = bind_name a, %a.loc3_11.1 +// CHECK:STDOUT: @Negate.%return: ref f64 = var +// CHECK:STDOUT: } +// CHECK:STDOUT: %True.decl: type = class_decl @True [template = constants.%True] {} +// CHECK:STDOUT: %False.decl: type = class_decl @False [template = constants.%False] {} +// CHECK:STDOUT: %F: = fn_decl @F [template] { +// CHECK:STDOUT: %True.ref: type = name_ref True, %True.decl [template = constants.%True] +// CHECK:STDOUT: %true_.loc8_6.1: True = param true_ +// CHECK:STDOUT: @F.%true_: True = bind_name true_, %true_.loc8_6.1 +// CHECK:STDOUT: %False.ref: type = name_ref False, %False.decl [template = constants.%False] +// CHECK:STDOUT: %false_.loc8_19.1: False = param false_ +// CHECK:STDOUT: @F.%false_: False = bind_name false_, %false_.loc8_19.1 +// CHECK:STDOUT: } +// CHECK:STDOUT: %RuntimeCall: = fn_decl @RuntimeCall [template] { +// CHECK:STDOUT: %a.loc16_16.1: f64 = param a +// CHECK:STDOUT: @RuntimeCall.%a: f64 = bind_name a, %a.loc16_16.1 +// CHECK:STDOUT: %b.loc16_24.1: f64 = param b +// CHECK:STDOUT: @RuntimeCall.%b: f64 = bind_name b, %b.loc16_24.1 +// CHECK:STDOUT: @RuntimeCall.%return: ref bool = var +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: class @True { +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = constants.%True +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: class @False { +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = constants.%False +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @Less(%a: f64, %b: f64) -> bool = "float.less"; +// CHECK:STDOUT: +// CHECK:STDOUT: fn @Negate(%a: f64) -> f64 = "float.negate"; +// CHECK:STDOUT: +// CHECK:STDOUT: fn @F(%true_: True, %false_: False) { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %true_.ref.loc9: True = name_ref true_, %true_ +// CHECK:STDOUT: %Less.ref.loc9: = name_ref Less, file.%Less [template = file.%Less] +// CHECK:STDOUT: %.loc9_21: f64 = float_literal 1 [template = constants.%.4] +// CHECK:STDOUT: %.loc9_26: f64 = float_literal 2 [template = constants.%.5] +// CHECK:STDOUT: %float.less.loc9: init bool = call %Less.ref.loc9(%.loc9_21, %.loc9_26) [template = constants.%.6] +// CHECK:STDOUT: %.loc9_13.1: bool = value_of_initializer %float.less.loc9 [template = constants.%.6] +// CHECK:STDOUT: %.loc9_13.2: bool = converted %float.less.loc9, %.loc9_13.1 [template = constants.%.6] +// CHECK:STDOUT: if %.loc9_13.2 br !if.expr.then.loc9 else br !if.expr.else.loc9 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc9: +// CHECK:STDOUT: %True.ref.loc9: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc9(%True.ref.loc9) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc9: +// CHECK:STDOUT: %False.ref.loc9: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc9(%False.ref.loc9) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc9: +// CHECK:STDOUT: %.loc9_13.3: type = block_arg !if.expr.result.loc9 [template = constants.%True] +// CHECK:STDOUT: %false_.ref.loc10: False = name_ref false_, %false_ +// CHECK:STDOUT: %Less.ref.loc10: = name_ref Less, file.%Less [template = file.%Less] +// CHECK:STDOUT: %.loc10_22: f64 = float_literal 1 [template = constants.%.7] +// CHECK:STDOUT: %.loc10_27: f64 = float_literal 1 [template = constants.%.8] +// CHECK:STDOUT: %float.less.loc10: init bool = call %Less.ref.loc10(%.loc10_22, %.loc10_27) [template = constants.%.9] +// CHECK:STDOUT: %.loc10_14.1: bool = value_of_initializer %float.less.loc10 [template = constants.%.9] +// CHECK:STDOUT: %.loc10_14.2: bool = converted %float.less.loc10, %.loc10_14.1 [template = constants.%.9] +// CHECK:STDOUT: if %.loc10_14.2 br !if.expr.then.loc10 else br !if.expr.else.loc10 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc10: +// CHECK:STDOUT: %True.ref.loc10: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc10(%True.ref.loc10) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc10: +// CHECK:STDOUT: %False.ref.loc10: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc10(%False.ref.loc10) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc10: +// CHECK:STDOUT: %.loc10_14.3: type = block_arg !if.expr.result.loc10 [template = constants.%False] +// CHECK:STDOUT: %false_.ref.loc11: False = name_ref false_, %false_ +// CHECK:STDOUT: %Less.ref.loc11: = name_ref Less, file.%Less [template = file.%Less] +// CHECK:STDOUT: %.loc11_22: f64 = float_literal 1 [template = constants.%.10] +// CHECK:STDOUT: %.loc11_27: f64 = float_literal 0 [template = constants.%.11] +// CHECK:STDOUT: %float.less.loc11: init bool = call %Less.ref.loc11(%.loc11_22, %.loc11_27) [template = constants.%.9] +// CHECK:STDOUT: %.loc11_14.1: bool = value_of_initializer %float.less.loc11 [template = constants.%.9] +// CHECK:STDOUT: %.loc11_14.2: bool = converted %float.less.loc11, %.loc11_14.1 [template = constants.%.9] +// CHECK:STDOUT: if %.loc11_14.2 br !if.expr.then.loc11 else br !if.expr.else.loc11 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc11: +// CHECK:STDOUT: %True.ref.loc11: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc11(%True.ref.loc11) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc11: +// CHECK:STDOUT: %False.ref.loc11: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc11(%False.ref.loc11) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc11: +// CHECK:STDOUT: %.loc11_14.3: type = block_arg !if.expr.result.loc11 [template = constants.%False] +// CHECK:STDOUT: %true_.ref.loc12: True = name_ref true_, %true_ +// CHECK:STDOUT: %Less.ref.loc12: = name_ref Less, file.%Less [template = file.%Less] +// CHECK:STDOUT: %Negate.ref.loc12: = name_ref Negate, file.%Negate [template = file.%Negate] +// CHECK:STDOUT: %.loc12_28: f64 = float_literal 1 [template = constants.%.12] +// CHECK:STDOUT: %float.negate.loc12: init f64 = call %Negate.ref.loc12(%.loc12_28) [template = constants.%.13] +// CHECK:STDOUT: %.loc12_34: f64 = float_literal 0 [template = constants.%.14] +// CHECK:STDOUT: %.loc12_20.1: f64 = value_of_initializer %float.negate.loc12 [template = constants.%.13] +// CHECK:STDOUT: %.loc12_20.2: f64 = converted %float.negate.loc12, %.loc12_20.1 [template = constants.%.13] +// CHECK:STDOUT: %float.less.loc12: init bool = call %Less.ref.loc12(%.loc12_20.2, %.loc12_34) [template = constants.%.6] +// CHECK:STDOUT: %.loc12_13.1: bool = value_of_initializer %float.less.loc12 [template = constants.%.6] +// CHECK:STDOUT: %.loc12_13.2: bool = converted %float.less.loc12, %.loc12_13.1 [template = constants.%.6] +// CHECK:STDOUT: if %.loc12_13.2 br !if.expr.then.loc12 else br !if.expr.else.loc12 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc12: +// CHECK:STDOUT: %True.ref.loc12: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc12(%True.ref.loc12) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc12: +// CHECK:STDOUT: %False.ref.loc12: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc12(%False.ref.loc12) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc12: +// CHECK:STDOUT: %.loc12_13.3: type = block_arg !if.expr.result.loc12 [template = constants.%True] +// CHECK:STDOUT: %false_.ref.loc13: False = name_ref false_, %false_ +// CHECK:STDOUT: %Less.ref.loc13: = name_ref Less, file.%Less [template = file.%Less] +// CHECK:STDOUT: %.loc13_22: f64 = float_literal 0 [template = constants.%.15] +// CHECK:STDOUT: %Negate.ref.loc13: = name_ref Negate, file.%Negate [template = file.%Negate] +// CHECK:STDOUT: %.loc13_34: f64 = float_literal 1 [template = constants.%.16] +// CHECK:STDOUT: %float.negate.loc13: init f64 = call %Negate.ref.loc13(%.loc13_34) [template = constants.%.17] +// CHECK:STDOUT: %.loc13_21.1: f64 = value_of_initializer %float.negate.loc13 [template = constants.%.17] +// CHECK:STDOUT: %.loc13_21.2: f64 = converted %float.negate.loc13, %.loc13_21.1 [template = constants.%.17] +// CHECK:STDOUT: %float.less.loc13: init bool = call %Less.ref.loc13(%.loc13_22, %.loc13_21.2) [template = constants.%.9] +// CHECK:STDOUT: %.loc13_14.1: bool = value_of_initializer %float.less.loc13 [template = constants.%.9] +// CHECK:STDOUT: %.loc13_14.2: bool = converted %float.less.loc13, %.loc13_14.1 [template = constants.%.9] +// CHECK:STDOUT: if %.loc13_14.2 br !if.expr.then.loc13 else br !if.expr.else.loc13 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc13: +// CHECK:STDOUT: %True.ref.loc13: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc13(%True.ref.loc13) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc13: +// CHECK:STDOUT: %False.ref.loc13: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc13(%False.ref.loc13) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc13: +// CHECK:STDOUT: %.loc13_14.3: type = block_arg !if.expr.result.loc13 [template = constants.%False] +// CHECK:STDOUT: return +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @RuntimeCall(%a: f64, %b: f64) -> bool { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %Less.ref: = name_ref Less, file.%Less [template = file.%Less] +// CHECK:STDOUT: %a.ref: f64 = name_ref a, %a +// CHECK:STDOUT: %b.ref: f64 = name_ref b, %b +// CHECK:STDOUT: %float.less: init bool = call %Less.ref(%a.ref, %b.ref) +// CHECK:STDOUT: %.loc17_20.1: bool = value_of_initializer %float.less +// CHECK:STDOUT: %.loc17_20.2: bool = converted %float.less, %.loc17_20.1 +// CHECK:STDOUT: return %.loc17_20.2 +// CHECK:STDOUT: } +// CHECK:STDOUT: diff --git a/toolchain/check/testdata/builtins/float/less_eq.carbon b/toolchain/check/testdata/builtins/float/less_eq.carbon new file mode 100644 index 0000000000000..d270f85497b9e --- /dev/null +++ b/toolchain/check/testdata/builtins/float/less_eq.carbon @@ -0,0 +1,225 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// AUTOUPDATE + +// --- float_less_eq.carbon + +fn LessEq(a: f64, b: f64) -> bool = "float.less_eq"; +fn Negate(a: f64) -> f64 = "float.negate"; + +class True {} +class False {} + +fn F(true_: True, false_: False) { + true_ as (if LessEq(1.0, 2.0) then True else False); + true_ as (if LessEq(1.0, 1.0) then True else False); + false_ as (if LessEq(1.0, 0.0) then True else False); + true_ as (if LessEq(Negate(1.0), 0.0) then True else False); + false_ as (if LessEq(0.0, Negate(1.0)) then True else False); +} + +fn RuntimeCall(a: f64, b: f64) -> bool { + return LessEq(a, b); +} + +// CHECK:STDOUT: --- float_less_eq.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %True: type = class_type @True [template] +// CHECK:STDOUT: %.1: type = struct_type {} [template] +// CHECK:STDOUT: %False: type = class_type @False [template] +// CHECK:STDOUT: %.2: type = tuple_type () [template] +// CHECK:STDOUT: %.3: type = ptr_type {} [template] +// CHECK:STDOUT: %.4: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.5: f64 = float_literal 2 [template] +// CHECK:STDOUT: %.6: bool = bool_literal true [template] +// CHECK:STDOUT: %.7: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.8: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.9: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.10: f64 = float_literal 0 [template] +// CHECK:STDOUT: %.11: bool = bool_literal false [template] +// CHECK:STDOUT: %.12: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.13: f64 = float_literal -1 [template] +// CHECK:STDOUT: %.14: f64 = float_literal 0 [template] +// CHECK:STDOUT: %.15: f64 = float_literal 0 [template] +// CHECK:STDOUT: %.16: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.17: f64 = float_literal -1 [template] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: package: = namespace [template] { +// CHECK:STDOUT: .Core = %Core +// CHECK:STDOUT: .LessEq = %LessEq +// CHECK:STDOUT: .Negate = %Negate +// CHECK:STDOUT: .True = %True.decl +// CHECK:STDOUT: .False = %False.decl +// CHECK:STDOUT: .F = %F +// CHECK:STDOUT: .RuntimeCall = %RuntimeCall +// CHECK:STDOUT: } +// CHECK:STDOUT: %Core: = namespace [template] {} +// CHECK:STDOUT: %LessEq: = fn_decl @LessEq [template] { +// CHECK:STDOUT: %a.loc2_11.1: f64 = param a +// CHECK:STDOUT: @LessEq.%a: f64 = bind_name a, %a.loc2_11.1 +// CHECK:STDOUT: %b.loc2_19.1: f64 = param b +// CHECK:STDOUT: @LessEq.%b: f64 = bind_name b, %b.loc2_19.1 +// CHECK:STDOUT: @LessEq.%return: ref bool = var +// CHECK:STDOUT: } +// CHECK:STDOUT: %Negate: = fn_decl @Negate [template] { +// CHECK:STDOUT: %a.loc3_11.1: f64 = param a +// CHECK:STDOUT: @Negate.%a: f64 = bind_name a, %a.loc3_11.1 +// CHECK:STDOUT: @Negate.%return: ref f64 = var +// CHECK:STDOUT: } +// CHECK:STDOUT: %True.decl: type = class_decl @True [template = constants.%True] {} +// CHECK:STDOUT: %False.decl: type = class_decl @False [template = constants.%False] {} +// CHECK:STDOUT: %F: = fn_decl @F [template] { +// CHECK:STDOUT: %True.ref: type = name_ref True, %True.decl [template = constants.%True] +// CHECK:STDOUT: %true_.loc8_6.1: True = param true_ +// CHECK:STDOUT: @F.%true_: True = bind_name true_, %true_.loc8_6.1 +// CHECK:STDOUT: %False.ref: type = name_ref False, %False.decl [template = constants.%False] +// CHECK:STDOUT: %false_.loc8_19.1: False = param false_ +// CHECK:STDOUT: @F.%false_: False = bind_name false_, %false_.loc8_19.1 +// CHECK:STDOUT: } +// CHECK:STDOUT: %RuntimeCall: = fn_decl @RuntimeCall [template] { +// CHECK:STDOUT: %a.loc16_16.1: f64 = param a +// CHECK:STDOUT: @RuntimeCall.%a: f64 = bind_name a, %a.loc16_16.1 +// CHECK:STDOUT: %b.loc16_24.1: f64 = param b +// CHECK:STDOUT: @RuntimeCall.%b: f64 = bind_name b, %b.loc16_24.1 +// CHECK:STDOUT: @RuntimeCall.%return: ref bool = var +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: class @True { +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = constants.%True +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: class @False { +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = constants.%False +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @LessEq(%a: f64, %b: f64) -> bool = "float.less_eq"; +// CHECK:STDOUT: +// CHECK:STDOUT: fn @Negate(%a: f64) -> f64 = "float.negate"; +// CHECK:STDOUT: +// CHECK:STDOUT: fn @F(%true_: True, %false_: False) { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %true_.ref.loc9: True = name_ref true_, %true_ +// CHECK:STDOUT: %LessEq.ref.loc9: = name_ref LessEq, file.%LessEq [template = file.%LessEq] +// CHECK:STDOUT: %.loc9_23: f64 = float_literal 1 [template = constants.%.4] +// CHECK:STDOUT: %.loc9_28: f64 = float_literal 2 [template = constants.%.5] +// CHECK:STDOUT: %float.less_eq.loc9: init bool = call %LessEq.ref.loc9(%.loc9_23, %.loc9_28) [template = constants.%.6] +// CHECK:STDOUT: %.loc9_13.1: bool = value_of_initializer %float.less_eq.loc9 [template = constants.%.6] +// CHECK:STDOUT: %.loc9_13.2: bool = converted %float.less_eq.loc9, %.loc9_13.1 [template = constants.%.6] +// CHECK:STDOUT: if %.loc9_13.2 br !if.expr.then.loc9 else br !if.expr.else.loc9 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc9: +// CHECK:STDOUT: %True.ref.loc9: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc9(%True.ref.loc9) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc9: +// CHECK:STDOUT: %False.ref.loc9: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc9(%False.ref.loc9) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc9: +// CHECK:STDOUT: %.loc9_13.3: type = block_arg !if.expr.result.loc9 [template = constants.%True] +// CHECK:STDOUT: %true_.ref.loc10: True = name_ref true_, %true_ +// CHECK:STDOUT: %LessEq.ref.loc10: = name_ref LessEq, file.%LessEq [template = file.%LessEq] +// CHECK:STDOUT: %.loc10_23: f64 = float_literal 1 [template = constants.%.7] +// CHECK:STDOUT: %.loc10_28: f64 = float_literal 1 [template = constants.%.8] +// CHECK:STDOUT: %float.less_eq.loc10: init bool = call %LessEq.ref.loc10(%.loc10_23, %.loc10_28) [template = constants.%.6] +// CHECK:STDOUT: %.loc10_13.1: bool = value_of_initializer %float.less_eq.loc10 [template = constants.%.6] +// CHECK:STDOUT: %.loc10_13.2: bool = converted %float.less_eq.loc10, %.loc10_13.1 [template = constants.%.6] +// CHECK:STDOUT: if %.loc10_13.2 br !if.expr.then.loc10 else br !if.expr.else.loc10 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc10: +// CHECK:STDOUT: %True.ref.loc10: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc10(%True.ref.loc10) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc10: +// CHECK:STDOUT: %False.ref.loc10: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc10(%False.ref.loc10) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc10: +// CHECK:STDOUT: %.loc10_13.3: type = block_arg !if.expr.result.loc10 [template = constants.%True] +// CHECK:STDOUT: %false_.ref.loc11: False = name_ref false_, %false_ +// CHECK:STDOUT: %LessEq.ref.loc11: = name_ref LessEq, file.%LessEq [template = file.%LessEq] +// CHECK:STDOUT: %.loc11_24: f64 = float_literal 1 [template = constants.%.9] +// CHECK:STDOUT: %.loc11_29: f64 = float_literal 0 [template = constants.%.10] +// CHECK:STDOUT: %float.less_eq.loc11: init bool = call %LessEq.ref.loc11(%.loc11_24, %.loc11_29) [template = constants.%.11] +// CHECK:STDOUT: %.loc11_14.1: bool = value_of_initializer %float.less_eq.loc11 [template = constants.%.11] +// CHECK:STDOUT: %.loc11_14.2: bool = converted %float.less_eq.loc11, %.loc11_14.1 [template = constants.%.11] +// CHECK:STDOUT: if %.loc11_14.2 br !if.expr.then.loc11 else br !if.expr.else.loc11 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc11: +// CHECK:STDOUT: %True.ref.loc11: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc11(%True.ref.loc11) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc11: +// CHECK:STDOUT: %False.ref.loc11: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc11(%False.ref.loc11) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc11: +// CHECK:STDOUT: %.loc11_14.3: type = block_arg !if.expr.result.loc11 [template = constants.%False] +// CHECK:STDOUT: %true_.ref.loc12: True = name_ref true_, %true_ +// CHECK:STDOUT: %LessEq.ref.loc12: = name_ref LessEq, file.%LessEq [template = file.%LessEq] +// CHECK:STDOUT: %Negate.ref.loc12: = name_ref Negate, file.%Negate [template = file.%Negate] +// CHECK:STDOUT: %.loc12_30: f64 = float_literal 1 [template = constants.%.12] +// CHECK:STDOUT: %float.negate.loc12: init f64 = call %Negate.ref.loc12(%.loc12_30) [template = constants.%.13] +// CHECK:STDOUT: %.loc12_36: f64 = float_literal 0 [template = constants.%.14] +// CHECK:STDOUT: %.loc12_22.1: f64 = value_of_initializer %float.negate.loc12 [template = constants.%.13] +// CHECK:STDOUT: %.loc12_22.2: f64 = converted %float.negate.loc12, %.loc12_22.1 [template = constants.%.13] +// CHECK:STDOUT: %float.less_eq.loc12: init bool = call %LessEq.ref.loc12(%.loc12_22.2, %.loc12_36) [template = constants.%.6] +// CHECK:STDOUT: %.loc12_13.1: bool = value_of_initializer %float.less_eq.loc12 [template = constants.%.6] +// CHECK:STDOUT: %.loc12_13.2: bool = converted %float.less_eq.loc12, %.loc12_13.1 [template = constants.%.6] +// CHECK:STDOUT: if %.loc12_13.2 br !if.expr.then.loc12 else br !if.expr.else.loc12 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc12: +// CHECK:STDOUT: %True.ref.loc12: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc12(%True.ref.loc12) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc12: +// CHECK:STDOUT: %False.ref.loc12: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc12(%False.ref.loc12) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc12: +// CHECK:STDOUT: %.loc12_13.3: type = block_arg !if.expr.result.loc12 [template = constants.%True] +// CHECK:STDOUT: %false_.ref.loc13: False = name_ref false_, %false_ +// CHECK:STDOUT: %LessEq.ref.loc13: = name_ref LessEq, file.%LessEq [template = file.%LessEq] +// CHECK:STDOUT: %.loc13_24: f64 = float_literal 0 [template = constants.%.15] +// CHECK:STDOUT: %Negate.ref.loc13: = name_ref Negate, file.%Negate [template = file.%Negate] +// CHECK:STDOUT: %.loc13_36: f64 = float_literal 1 [template = constants.%.16] +// CHECK:STDOUT: %float.negate.loc13: init f64 = call %Negate.ref.loc13(%.loc13_36) [template = constants.%.17] +// CHECK:STDOUT: %.loc13_23.1: f64 = value_of_initializer %float.negate.loc13 [template = constants.%.17] +// CHECK:STDOUT: %.loc13_23.2: f64 = converted %float.negate.loc13, %.loc13_23.1 [template = constants.%.17] +// CHECK:STDOUT: %float.less_eq.loc13: init bool = call %LessEq.ref.loc13(%.loc13_24, %.loc13_23.2) [template = constants.%.11] +// CHECK:STDOUT: %.loc13_14.1: bool = value_of_initializer %float.less_eq.loc13 [template = constants.%.11] +// CHECK:STDOUT: %.loc13_14.2: bool = converted %float.less_eq.loc13, %.loc13_14.1 [template = constants.%.11] +// CHECK:STDOUT: if %.loc13_14.2 br !if.expr.then.loc13 else br !if.expr.else.loc13 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc13: +// CHECK:STDOUT: %True.ref.loc13: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc13(%True.ref.loc13) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc13: +// CHECK:STDOUT: %False.ref.loc13: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc13(%False.ref.loc13) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc13: +// CHECK:STDOUT: %.loc13_14.3: type = block_arg !if.expr.result.loc13 [template = constants.%False] +// CHECK:STDOUT: return +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @RuntimeCall(%a: f64, %b: f64) -> bool { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %LessEq.ref: = name_ref LessEq, file.%LessEq [template = file.%LessEq] +// CHECK:STDOUT: %a.ref: f64 = name_ref a, %a +// CHECK:STDOUT: %b.ref: f64 = name_ref b, %b +// CHECK:STDOUT: %float.less_eq: init bool = call %LessEq.ref(%a.ref, %b.ref) +// CHECK:STDOUT: %.loc17_22.1: bool = value_of_initializer %float.less_eq +// CHECK:STDOUT: %.loc17_22.2: bool = converted %float.less_eq, %.loc17_22.1 +// CHECK:STDOUT: return %.loc17_22.2 +// CHECK:STDOUT: } +// CHECK:STDOUT: diff --git a/toolchain/check/testdata/builtins/float/neq.carbon b/toolchain/check/testdata/builtins/float/neq.carbon new file mode 100644 index 0000000000000..b4aff815cbf6f --- /dev/null +++ b/toolchain/check/testdata/builtins/float/neq.carbon @@ -0,0 +1,168 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// AUTOUPDATE + +// --- float_neq.carbon + +fn Neq(a: f64, b: f64) -> bool = "float.neq"; + +class True {} +class False {} + +fn F(true_: True, false_: False) { + true_ as (if Neq(1.0, 2.0) then True else False); + false_ as (if Neq(1.0, 1.0) then True else False); +} + +fn RuntimeCall(a: f64, b: f64) -> bool { + return Neq(a, b); +} + +// --- fail_bad_decl.carbon + +package FailBadDecl api; + +// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+3]]:1: ERROR: Invalid signature for builtin function "float.neq". +// CHECK:STDERR: fn WrongResult(a: f64, b: f64) -> f64 = "float.neq"; +// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +fn WrongResult(a: f64, b: f64) -> f64 = "float.neq"; + +// CHECK:STDOUT: --- float_neq.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %True: type = class_type @True [template] +// CHECK:STDOUT: %.1: type = struct_type {} [template] +// CHECK:STDOUT: %False: type = class_type @False [template] +// CHECK:STDOUT: %.2: type = tuple_type () [template] +// CHECK:STDOUT: %.3: type = ptr_type {} [template] +// CHECK:STDOUT: %.4: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.5: f64 = float_literal 2 [template] +// CHECK:STDOUT: %.6: bool = bool_literal true [template] +// CHECK:STDOUT: %.7: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.8: f64 = float_literal 1 [template] +// CHECK:STDOUT: %.9: bool = bool_literal false [template] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: package: = namespace [template] { +// CHECK:STDOUT: .Core = %Core +// CHECK:STDOUT: .Neq = %Neq +// CHECK:STDOUT: .True = %True.decl +// CHECK:STDOUT: .False = %False.decl +// CHECK:STDOUT: .F = %F +// CHECK:STDOUT: .RuntimeCall = %RuntimeCall +// CHECK:STDOUT: } +// CHECK:STDOUT: %Core: = namespace [template] {} +// CHECK:STDOUT: %Neq: = fn_decl @Neq [template] { +// CHECK:STDOUT: %a.loc2_8.1: f64 = param a +// CHECK:STDOUT: @Neq.%a: f64 = bind_name a, %a.loc2_8.1 +// CHECK:STDOUT: %b.loc2_16.1: f64 = param b +// CHECK:STDOUT: @Neq.%b: f64 = bind_name b, %b.loc2_16.1 +// CHECK:STDOUT: @Neq.%return: ref bool = var +// CHECK:STDOUT: } +// CHECK:STDOUT: %True.decl: type = class_decl @True [template = constants.%True] {} +// CHECK:STDOUT: %False.decl: type = class_decl @False [template = constants.%False] {} +// CHECK:STDOUT: %F: = fn_decl @F [template] { +// CHECK:STDOUT: %True.ref: type = name_ref True, %True.decl [template = constants.%True] +// CHECK:STDOUT: %true_.loc7_6.1: True = param true_ +// CHECK:STDOUT: @F.%true_: True = bind_name true_, %true_.loc7_6.1 +// CHECK:STDOUT: %False.ref: type = name_ref False, %False.decl [template = constants.%False] +// CHECK:STDOUT: %false_.loc7_19.1: False = param false_ +// CHECK:STDOUT: @F.%false_: False = bind_name false_, %false_.loc7_19.1 +// CHECK:STDOUT: } +// CHECK:STDOUT: %RuntimeCall: = fn_decl @RuntimeCall [template] { +// CHECK:STDOUT: %a.loc12_16.1: f64 = param a +// CHECK:STDOUT: @RuntimeCall.%a: f64 = bind_name a, %a.loc12_16.1 +// CHECK:STDOUT: %b.loc12_24.1: f64 = param b +// CHECK:STDOUT: @RuntimeCall.%b: f64 = bind_name b, %b.loc12_24.1 +// CHECK:STDOUT: @RuntimeCall.%return: ref bool = var +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: class @True { +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = constants.%True +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: class @False { +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = constants.%False +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @Neq(%a: f64, %b: f64) -> bool = "float.neq"; +// CHECK:STDOUT: +// CHECK:STDOUT: fn @F(%true_: True, %false_: False) { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %true_.ref: True = name_ref true_, %true_ +// CHECK:STDOUT: %Neq.ref.loc8: = name_ref Neq, file.%Neq [template = file.%Neq] +// CHECK:STDOUT: %.loc8_20: f64 = float_literal 1 [template = constants.%.4] +// CHECK:STDOUT: %.loc8_25: f64 = float_literal 2 [template = constants.%.5] +// CHECK:STDOUT: %float.neq.loc8: init bool = call %Neq.ref.loc8(%.loc8_20, %.loc8_25) [template = constants.%.6] +// CHECK:STDOUT: %.loc8_13.1: bool = value_of_initializer %float.neq.loc8 [template = constants.%.6] +// CHECK:STDOUT: %.loc8_13.2: bool = converted %float.neq.loc8, %.loc8_13.1 [template = constants.%.6] +// CHECK:STDOUT: if %.loc8_13.2 br !if.expr.then.loc8 else br !if.expr.else.loc8 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc8: +// CHECK:STDOUT: %True.ref.loc8: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc8(%True.ref.loc8) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc8: +// CHECK:STDOUT: %False.ref.loc8: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc8(%False.ref.loc8) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc8: +// CHECK:STDOUT: %.loc8_13.3: type = block_arg !if.expr.result.loc8 [template = constants.%True] +// CHECK:STDOUT: %false_.ref: False = name_ref false_, %false_ +// CHECK:STDOUT: %Neq.ref.loc9: = name_ref Neq, file.%Neq [template = file.%Neq] +// CHECK:STDOUT: %.loc9_21: f64 = float_literal 1 [template = constants.%.7] +// CHECK:STDOUT: %.loc9_26: f64 = float_literal 1 [template = constants.%.8] +// CHECK:STDOUT: %float.neq.loc9: init bool = call %Neq.ref.loc9(%.loc9_21, %.loc9_26) [template = constants.%.9] +// CHECK:STDOUT: %.loc9_14.1: bool = value_of_initializer %float.neq.loc9 [template = constants.%.9] +// CHECK:STDOUT: %.loc9_14.2: bool = converted %float.neq.loc9, %.loc9_14.1 [template = constants.%.9] +// CHECK:STDOUT: if %.loc9_14.2 br !if.expr.then.loc9 else br !if.expr.else.loc9 +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.then.loc9: +// CHECK:STDOUT: %True.ref.loc9: type = name_ref True, file.%True.decl [template = constants.%True] +// CHECK:STDOUT: br !if.expr.result.loc9(%True.ref.loc9) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.else.loc9: +// CHECK:STDOUT: %False.ref.loc9: type = name_ref False, file.%False.decl [template = constants.%False] +// CHECK:STDOUT: br !if.expr.result.loc9(%False.ref.loc9) +// CHECK:STDOUT: +// CHECK:STDOUT: !if.expr.result.loc9: +// CHECK:STDOUT: %.loc9_14.3: type = block_arg !if.expr.result.loc9 [template = constants.%False] +// CHECK:STDOUT: return +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @RuntimeCall(%a: f64, %b: f64) -> bool { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %Neq.ref: = name_ref Neq, file.%Neq [template = file.%Neq] +// CHECK:STDOUT: %a.ref: f64 = name_ref a, %a +// CHECK:STDOUT: %b.ref: f64 = name_ref b, %b +// CHECK:STDOUT: %float.neq: init bool = call %Neq.ref(%a.ref, %b.ref) +// CHECK:STDOUT: %.loc13_19.1: bool = value_of_initializer %float.neq +// CHECK:STDOUT: %.loc13_19.2: bool = converted %float.neq, %.loc13_19.1 +// CHECK:STDOUT: return %.loc13_19.2 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: --- fail_bad_decl.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: package: = namespace [template] { +// CHECK:STDOUT: .Core = %Core +// CHECK:STDOUT: .WrongResult = %WrongResult +// CHECK:STDOUT: } +// CHECK:STDOUT: %Core: = namespace [template] {} +// CHECK:STDOUT: %WrongResult: = fn_decl @WrongResult [template] { +// CHECK:STDOUT: %a.loc7_16.1: f64 = param a +// CHECK:STDOUT: @WrongResult.%a: f64 = bind_name a, %a.loc7_16.1 +// CHECK:STDOUT: %b.loc7_24.1: f64 = param b +// CHECK:STDOUT: @WrongResult.%b: f64 = bind_name b, %b.loc7_24.1 +// CHECK:STDOUT: @WrongResult.%return: ref f64 = var +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @WrongResult(%a: f64, %b: f64) -> f64; +// CHECK:STDOUT: diff --git a/toolchain/lower/handle.cpp b/toolchain/lower/handle.cpp index 0088094d6f48f..1dd22640a65a6 100644 --- a/toolchain/lower/handle.cpp +++ b/toolchain/lower/handle.cpp @@ -200,6 +200,28 @@ static auto GetBuiltinICmpPredicate(SemIR::BuiltinFunctionKind builtin_kind, } } +// Get the predicate to use for an `fcmp` instruction generated for the +// specified builtin. +static auto GetBuiltinFCmpPredicate(SemIR::BuiltinFunctionKind builtin_kind) + -> llvm::CmpInst::Predicate { + switch (builtin_kind) { + case SemIR::BuiltinFunctionKind::FloatEq: + return llvm::CmpInst::FCMP_OEQ; + case SemIR::BuiltinFunctionKind::FloatNeq: + return llvm::CmpInst::FCMP_ONE; + case SemIR::BuiltinFunctionKind::FloatLess: + return llvm::CmpInst::FCMP_OLT; + case SemIR::BuiltinFunctionKind::FloatLessEq: + return llvm::CmpInst::FCMP_OLE; + case SemIR::BuiltinFunctionKind::FloatGreater: + return llvm::CmpInst::FCMP_OGT; + case SemIR::BuiltinFunctionKind::FloatGreaterEq: + return llvm::CmpInst::FCMP_OGE; + default: + CARBON_FATAL() << "Unexpected builtin kind " << builtin_kind; + } +} + // Returns whether the specified instruction has a signed integer type. static auto IsSignedInt(FunctionContext& context, SemIR::InstId int_id) -> bool { @@ -400,6 +422,18 @@ static auto HandleBuiltinCall(FunctionContext& context, SemIR::InstId inst_id, context.GetValue(arg_ids[1]), "fdiv")); return; } + case SemIR::BuiltinFunctionKind::FloatEq: + case SemIR::BuiltinFunctionKind::FloatNeq: + case SemIR::BuiltinFunctionKind::FloatLess: + case SemIR::BuiltinFunctionKind::FloatLessEq: + case SemIR::BuiltinFunctionKind::FloatGreater: + case SemIR::BuiltinFunctionKind::FloatGreaterEq: { + context.SetLocal(inst_id, context.builder().CreateFCmp( + GetBuiltinFCmpPredicate(builtin_kind), + context.GetValue(arg_ids[0]), + context.GetValue(arg_ids[1]))); + return; + } } CARBON_FATAL() << "Unsupported builtin call."; diff --git a/toolchain/lower/testdata/builtins/float.carbon b/toolchain/lower/testdata/builtins/float.carbon index 6f55065a5a394..1aea049e271ba 100644 --- a/toolchain/lower/testdata/builtins/float.carbon +++ b/toolchain/lower/testdata/builtins/float.carbon @@ -19,6 +19,24 @@ fn TestMul(a: f64, b: f64) -> f64 { return Mul(a, b); } fn Div(a: f64, b: f64) -> f64 = "float.div"; fn TestDiv(a: f64, b: f64) -> f64 { return Div(a, b); } +fn Eq(a: f64, b: f64) -> bool = "float.eq"; +fn TestEq(a: f64, b: f64) -> bool { return Eq(a, b); } + +fn Neq(a: f64, b: f64) -> bool = "float.neq"; +fn TestNeq(a: f64, b: f64) -> bool { return Neq(a, b); } + +fn Less(a: f64, b: f64) -> bool = "float.less"; +fn TestLess(a: f64, b: f64) -> bool { return Less(a, b); } + +fn LessEq(a: f64, b: f64) -> bool = "float.less_eq"; +fn TestLessEq(a: f64, b: f64) -> bool { return LessEq(a, b); } + +fn Greater(a: f64, b: f64) -> bool = "float.greater"; +fn TestGreater(a: f64, b: f64) -> bool { return Greater(a, b); } + +fn GreaterEq(a: f64, b: f64) -> bool = "float.greater_eq"; +fn TestGreaterEq(a: f64, b: f64) -> bool { return GreaterEq(a, b); } + // CHECK:STDOUT: ; ModuleID = 'float.carbon' // CHECK:STDOUT: source_filename = "float.carbon" // CHECK:STDOUT: @@ -51,3 +69,39 @@ fn TestDiv(a: f64, b: f64) -> f64 { return Div(a, b); } // CHECK:STDOUT: %float.div.fdiv = fdiv double %a, %b // CHECK:STDOUT: ret double %float.div.fdiv // CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: define i1 @TestEq(double %a, double %b) { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: %float.eq = fcmp oeq double %a, %b +// CHECK:STDOUT: ret i1 %float.eq +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: define i1 @TestNeq(double %a, double %b) { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: %float.neq = fcmp one double %a, %b +// CHECK:STDOUT: ret i1 %float.neq +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: define i1 @TestLess(double %a, double %b) { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: %float.less = fcmp olt double %a, %b +// CHECK:STDOUT: ret i1 %float.less +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: define i1 @TestLessEq(double %a, double %b) { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: %float.less_eq = fcmp ole double %a, %b +// CHECK:STDOUT: ret i1 %float.less_eq +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: define i1 @TestGreater(double %a, double %b) { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: %float.greater = fcmp ogt double %a, %b +// CHECK:STDOUT: ret i1 %float.greater +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: define i1 @TestGreaterEq(double %a, double %b) { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: %float.greater_eq = fcmp oge double %a, %b +// CHECK:STDOUT: ret i1 %float.greater_eq +// CHECK:STDOUT: } diff --git a/toolchain/sem_ir/builtin_function_kind.cpp b/toolchain/sem_ir/builtin_function_kind.cpp index 39e3bd1a951a3..7bf3c3209f107 100644 --- a/toolchain/sem_ir/builtin_function_kind.cpp +++ b/toolchain/sem_ir/builtin_function_kind.cpp @@ -295,6 +295,30 @@ constexpr BuiltinInfo FloatMul = { constexpr BuiltinInfo FloatDiv = { "float.div", ValidateSignatureFloatT>}; +// "float.eq": float equality comparison. +constexpr BuiltinInfo FloatEq = {"float.eq", + ValidateSignatureBool>}; + +// "float.neq": float non-equality comparison. +constexpr BuiltinInfo FloatNeq = { + "float.neq", ValidateSignatureBool>}; + +// "float.less": float less than comparison. +constexpr BuiltinInfo FloatLess = { + "float.less", ValidateSignatureBool>}; + +// "float.less_eq": float less than or equal comparison. +constexpr BuiltinInfo FloatLessEq = { + "float.less_eq", ValidateSignatureBool>}; + +// "float.greater": float greater than comparison. +constexpr BuiltinInfo FloatGreater = { + "float.greater", ValidateSignatureBool>}; + +// "float.greater_eq": float greater than or equal comparison. +constexpr BuiltinInfo FloatGreaterEq = { + "float.greater_eq", ValidateSignatureBool>}; + } // namespace BuiltinFunctionInfo CARBON_DEFINE_ENUM_CLASS_NAMES(BuiltinFunctionKind) = { diff --git a/toolchain/sem_ir/builtin_function_kind.def b/toolchain/sem_ir/builtin_function_kind.def index a538fb8f3f8d8..7e04f2c72cdc5 100644 --- a/toolchain/sem_ir/builtin_function_kind.def +++ b/toolchain/sem_ir/builtin_function_kind.def @@ -62,4 +62,12 @@ CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatSub) CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatMul) CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatDiv) +// Float comparison. +CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatEq) +CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatNeq) +CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatLess) +CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatLessEq) +CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatGreater) +CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(FloatGreaterEq) + #undef CARBON_SEM_IR_BUILTIN_FUNCTION_KIND