Replicate D3D's div by 0 behavior for RCP/RSQ
authorCaleb Cornett <caleb.cornett@outlook.com>
Mon, 25 Nov 2019 20:21:41 -0500
changeset 1219 c239540656e7
parent 1218 063ee7ba5eea
child 1220 048797e6aae1
Replicate D3D's div by 0 behavior for RCP/RSQ
profiles/mojoshader_profile_metal.c
--- a/profiles/mojoshader_profile_metal.c	Sat Nov 23 21:54:01 2019 -0500
+++ b/profiles/mojoshader_profile_metal.c	Mon Nov 25 20:21:41 2019 -0500
@@ -1122,7 +1122,8 @@
 {
     char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
     char code[128];
-    make_METAL_destarg_assign(ctx, code, sizeof (code), "1.0 / %s", src0);
+    ctx->metal_need_header_math = 1;
+    make_METAL_destarg_assign(ctx, code, sizeof (code), "(%s == 0.0) ? FLT_MAX : 1.0 / %s", src0, src0);
     output_line(ctx, "%s", code);
 } // emit_METAL_RCP
 
@@ -1131,7 +1132,7 @@
     char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
     char code[128];
     ctx->metal_need_header_math = 1;
-    make_METAL_destarg_assign(ctx, code, sizeof (code), "rsqrt(%s)", src0);
+    make_METAL_destarg_assign(ctx, code, sizeof (code), "(%s == 0.0) ? FLT_MAX : rsqrt(abs(%s))", src0, src0);
     output_line(ctx, "%s", code);
 } // emit_METAL_RSQ