Skip to content

Commit 8530d5a

Browse files
committed
Redirect bitwise ops to logical ops in case the arguments are bool.
1 parent 813920f commit 8530d5a

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/IROperator.cpp

+28-1
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ Expr halide_log(const Expr &x_full) {
781781

782782
Expr use_nan = x_full < 0.0f; // log of a negative returns nan
783783
Expr use_neg_inf = x_full == 0.0f; // log of zero is -inf
784-
Expr exceptional = use_nan | use_neg_inf;
784+
Expr exceptional = use_nan || use_neg_inf;
785785

786786
// Avoid producing nans or infs by generating ln(1.0f) instead and
787787
// then fixing it later.
@@ -2419,54 +2419,81 @@ Expr reinterpret(Type t, Expr e) {
24192419
Expr operator&(Expr x, Expr y) {
24202420
match_types_bitwise(x, y, "bitwise and");
24212421
Type t = x.type();
2422+
if (t.is_bool()) {
2423+
return std::move(x) && std::move(y);
2424+
}
24222425
return Call::make(t, Call::bitwise_and, {std::move(x), std::move(y)}, Call::PureIntrinsic);
24232426
}
24242427

24252428
Expr operator&(Expr x, int y) {
24262429
Type t = x.type();
24272430
check_representable(t, y);
2431+
if (t.is_bool()) {
2432+
return std::move(x) && make_const(t, y);
2433+
}
24282434
return Call::make(t, Call::bitwise_and, {std::move(x), make_const(t, y)}, Call::PureIntrinsic);
24292435
}
24302436

24312437
Expr operator&(int x, Expr y) {
24322438
Type t = y.type();
24332439
check_representable(t, x);
2440+
if (t.is_bool()) {
2441+
return make_const(t, x) && std::move(y);
2442+
}
24342443
return Call::make(t, Call::bitwise_and, {make_const(t, x), std::move(y)}, Call::PureIntrinsic);
24352444
}
24362445

24372446
Expr operator|(Expr x, Expr y) {
24382447
match_types_bitwise(x, y, "bitwise or");
24392448
Type t = x.type();
2449+
if (t.is_bool()) {
2450+
return std::move(x) || std::move(y);
2451+
}
24402452
return Call::make(t, Call::bitwise_or, {std::move(x), std::move(y)}, Call::PureIntrinsic);
24412453
}
24422454

24432455
Expr operator|(Expr x, int y) {
24442456
Type t = x.type();
24452457
check_representable(t, y);
2458+
if (t.is_bool()) {
2459+
return std::move(x) || make_const(t, y);
2460+
}
24462461
return Call::make(t, Call::bitwise_or, {std::move(x), make_const(t, y)}, Call::PureIntrinsic);
24472462
}
24482463

24492464
Expr operator|(int x, Expr y) {
24502465
Type t = y.type();
24512466
check_representable(t, x);
2467+
if (t.is_bool()) {
2468+
return make_const(t, x) || std::move(y);
2469+
}
24522470
return Call::make(t, Call::bitwise_or, {make_const(t, x), std::move(y)}, Call::PureIntrinsic);
24532471
}
24542472

24552473
Expr operator^(Expr x, Expr y) {
24562474
match_types_bitwise(x, y, "bitwise xor");
24572475
Type t = x.type();
2476+
if (t.is_bool()) {
2477+
return std::move(x) != std::move(y);
2478+
}
24582479
return Call::make(t, Call::bitwise_xor, {std::move(x), std::move(y)}, Call::PureIntrinsic);
24592480
}
24602481

24612482
Expr operator^(Expr x, int y) {
24622483
Type t = x.type();
24632484
check_representable(t, y);
2485+
if (t.is_bool()) {
2486+
return std::move(x) != make_const(t, y);
2487+
}
24642488
return Call::make(t, Call::bitwise_xor, {std::move(x), make_const(t, y)}, Call::PureIntrinsic);
24652489
}
24662490

24672491
Expr operator^(int x, Expr y) {
24682492
Type t = y.type();
24692493
check_representable(t, x);
2494+
if (t.is_bool()) {
2495+
return make_const(t, x) != std::move(y);
2496+
}
24702497
return Call::make(t, Call::bitwise_xor, {make_const(t, x), std::move(y)}, Call::PureIntrinsic);
24712498
}
24722499

0 commit comments

Comments
 (0)