Skip to content

Commit e883584

Browse files
committed
Attempt to make pow() return NaN on WebGPU.
1 parent bdbecfc commit e883584

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/CodeGen_WebGPU_Dev.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,13 @@ void CodeGen_WebGPU_Dev::CodeGen_WGSL::visit(const Call *op) {
527527
Expr oy = op->args[1];
528528
Expr equiv = Call::make(op->type, "pow", {abs(ox), oy}, Call::PureExtern);
529529
Type int_type = Type(Type::Int, 32, oy.type().lanes());
530+
531+
// This is ill-defined. WGSL spec says that nan cannot occur.
532+
// Yet the WGSL built-in pow() function yields NaN for pow(-0.1, -0.1).
533+
// We'll try to get the NaN value in, by doing a value of 1.0 with
534+
// the correct number of lanes, multiplied by the result of our helper function
535+
// that tries to hack a NaN value in.
536+
Expr nan = make_const(oy.type(), 1.0f) * Call::make(Float(32), "nan_f32", {}, Call::PureExtern);
530537
equiv = select(ox > 0.0f,
531538
equiv,
532539
select(oy == 0.0f,
@@ -535,7 +542,7 @@ void CodeGen_WebGPU_Dev::CodeGen_WGSL::visit(const Call *op) {
535542
select(cast(int_type, oy) % 2 == 0,
536543
equiv,
537544
-equiv),
538-
make_const(oy.type(), std::nanf("")))));
545+
nan)));
539546
equiv.accept(this);
540547

541548
} else {

0 commit comments

Comments
 (0)