You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
WGSL currently has x % x described as OpFMod.
SPIR-V semantics for OpFMod this is x - y * floor(x / y)
Metal semantics for fmod is x - y * trunc(x / y), which matches HLSL semantics for %, and also SPIR-V semantics for OpFRem.
Given the mostly consensus here among the APIs, I suggest we change the semantics of % to match. Thus, it should be described as OpFRem in SPIR-V.
Good point. Looking closer, both MSL and GLSL have % only operating on integer scalars and vectors.
My instinct is to remove % for floating point, and replace it with frem and fmod.
(I haven't worked it out again, but I recall the difference is how to handle mixed signedness of the operands.)
Well there you go:
OpFRem "The floating-point remainder whose sign matches the sign of Operand 1."
OpFMod "The floating-point remainder whose sign matches the sign of Operand 2."
Note: Until C++11, if one or both operands to binary operator % were negative, the sign of the remainder was implementation-defined, as it depends on the rounding direction of integer division. The function std::div provided well-defined behavior in that case.
To follow up up on OpSRem and OpSMod, the spec currently says it is OpSMod, but probably shouldn't. Worse than that is that Vulkan specifically says OpSRem and OpSMod with any negative input is undefined.
For the OpSRem and OpSMod instructions, if either operand is negative the result is undefined.
@dneto0 why would we need to expose OpFMod?
What I see is - this is an operation that's only available on 1/3 backends. Unless it's actually used/desired by ISVs (1) and it has any performance benefit versus the polyfill (2), it seems straightforward to me that we'd just skip it.
For (1) - since people don't write SPIR-V, and instead write GLSL/HLSL, which do not have an analogue to OpFMod, their programs aren't expected to use this instruction. I did a quick check on the shader playground, and % translates to OpFRem, as expected.
For (2) we don't have any data.
We are working on converting glsl to wgsl in TFJS. In order to convert glsl mod to wgsl, we first use %, but it not work. Then we implement mod as below:
// From https://github.com/tensorflow/tfjs/pull/5589/files
fn mod(a : f32, b : f32) -> f32 {
return a - b * floor(a/b);
}
fn modVec4(a : vec4<f32>, b : vec4<f32>) -> vec4<f32> {
return a - b * floor(a/b);
}
Based on our experience, it's straightforward to use % as mod. So we strongly agree % should have its conventional OpFMod behavior
@axinging What was the original GLSL using - mod(), I assume? It seems GLSL doesn't define % for floats.
Also can you give some info about why you rely on the OpFMod behavior specifically? Is it just generally because you want to port code more directly from GLSL?
We are working on converting glsl to wgsl in TFJS. In order to convert glsl mod to wgsl, we first use %, but it not work. Then we implement mod as below:
// From https://github.com/tensorflow/tfjs/pull/5589/files
fn mod(a : f32, b : f32) -> f32 {
return a - b * floor(a/b);
}
fn modVec4(a : vec4<f32>, b : vec4<f32>) -> vec4<f32> {
return a - b * floor(a/b);
}
Based on our experience, it's straightforward to use % as mod. So we strongly agree % should have its conventional OpFMod behavior
@axinging What was the original GLSL using - mod(), I assume? It seems GLSL doesn't define % for floats.
Also can you give some info about why you rely on the OpFMod behavior specifically? Is it just generally because you want to port code more directly from GLSL?
Yes, if mod and % behave the same, we can port code from GLSL to WGSL more directly.
Below is the use case (Note: the mod in WGSL is implemented by us, to make it the same as GLSL mod):
const POW = `
if(a < 0.0 && floor(b) < b) {
return NAN;
}
if (b == 0.0) {
return 1.0;
}
// For GLSL, use mod.
return (round(mod(b, 2.0)) != 1) ?
pow(abs(a), b) : sign(a) * pow(abs(a), b);
`;
const POW_WGSL = `
if(a < 0.0 && floor(b) < b) {
return f32(uniforms.NAN);
}
if (b == 0.0) {
return 1.0;
}
// For WGSL, use mod implemented by us
if (i32(round(mod(b, 2.0))) != 1) {
return pow(abs(a), b);
}
return sign(a) * pow(abs(a), b);
`;
How this functions disassembles in MSL and HLSL? Should we be restricted by OpFRem semantic because this function exist on hardware level? Seems current specification mixing different conventions for int's and float's.
See the top of this issue and #1913 (comment).
HLSL and MSL have intrinsics that map to OpFRem, which is why we went with it.
GLSL is the odd one out here.
Current spec:
T is i32 or vecN -> OpSMod -> sign matches the sign of Operand 2
T is u32 or vecN -> OpUMod -> sign matches the sign of Operand 2
T is f32 or vecN -> OpFRem -> sign matches the sign of Operand 1
Result sign matches to another operand for floats. Better to create fmod() for consistency.
Also I checked HLSL output in DXBC, seems that fmod just a syntax sugar there and gives 3 vectorised FLOP. Metal IR have such thing as fast_fmod, but I can imagine world when this IR become 3 FLOP eventually.
OpFMod is periodic function which is a good property, also expression let uv = (xy % vec2<f32>(10.)); gives you tiled texture if you using OpFMod. So in the wild even HLSL/MSL programmers creating #define mod(x, y) (x - y * floor(x / y)) helper sometimes.
Activity
dneto0 commentedon Apr 29, 2021
Good point. Looking closer, both MSL and GLSL have % only operating on integer scalars and vectors.
My instinct is to remove % for floating point, and replace it with frem and fmod.
(I haven't worked it out again, but I recall the difference is how to handle mixed signedness of the operands.)
dneto0 commentedon Apr 29, 2021
Well there you go:
OpFRem "The floating-point remainder whose sign matches the sign of Operand 1."
OpFMod "The floating-point remainder whose sign matches the sign of Operand 2."
dneto0 commentedon Apr 29, 2021
I found this clarifying:
dneto0 commentedon Apr 29, 2021
And what we're left with is a naming mess.
dneto0 commentedon Apr 29, 2021
Looks like fmod is in the standard C99 library. https://en.cppreference.com/w/c/numeric/math/fmod
And MSL matches that. For fmod(x,y) "The returned value has the same sign as x and is less or equal to y in magnitude."
dneto0 commentedon Apr 29, 2021
Using different numbers (so it's more clear what's going on)
RobinMorisset commentedon May 4, 2021
btw, JS seems to match HLSL/MSL/C++ here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder (it is FRem).
I would also be ok with David's suggestion to not have '%' and just have two functions with explicit names.
litherum commentedon May 4, 2021
So I thiiiiiiink where we ended up in the call today is:
fmod()that has the same semantics asOpFRemin SPIR-V. It accepts scalar floats and vectors of floats. Nothing else.%operator which only operates on signed and unsigned ints, which has the same behavior regarding negative numbers asfmod()does.kvark commentedon May 4, 2021
@litherum MSL, SPIRV (and I think HLSL?) allow
%/OpSremon vectors of integers. Any reason we don't want this?dneto0 commentedon May 4, 2021
https://gpuweb.github.io/gpuweb/wgsl/#arithmetic-expr already has % for scalars i32, u32, f32, and vectors over i32, u32, f32. I think the change here is to remove the f32 and vec-of-f32 versions of %, and to add fmod instead.
Separately, @alan-baker pointed out we should check the semantics of % over signed integral.
Make sure we pick the "right" one.
In the meeting @pow2clk said (if I recall correctly), that the C standard pins down the behaviour for signs.
I found this note at https://en.cppreference.com/w/cpp/language/operator_arithmetic#Multiplicative_operators
dneto0 commentedon May 4, 2021
Also from cppreference. Since C++11, for integer division
/:The quotient is truncated towards zero (fractional part is discarded)."
kdashg commentedon May 5, 2021
WGSL meeting minutes 2021-05-04
%and the builtin, which allows us to add both later maybealan-baker commentedon May 10, 2021
To follow up up on OpSRem and OpSMod, the spec currently says it is OpSMod, but probably shouldn't. Worse than that is that Vulkan specifically says OpSRem and OpSMod with any negative input is undefined.
13 remaining items
kvark commentedon Jul 21, 2021
@dneto0 why would we need to expose
OpFMod?What I see is - this is an operation that's only available on 1/3 backends. Unless it's actually used/desired by ISVs (1) and it has any performance benefit versus the polyfill (2), it seems straightforward to me that we'd just skip it.
For (1) - since people don't write SPIR-V, and instead write GLSL/HLSL, which do not have an analogue to
OpFMod, their programs aren't expected to use this instruction. I did a quick check on the shader playground, and%translates toOpFRem, as expected.For (2) we don't have any data.
kainino0x commentedon Sep 10, 2021
munrocket:
axinging:
@axinging What was the original GLSL using -
mod(), I assume? It seems GLSL doesn't define%for floats.Also can you give some info about why you rely on the OpFMod behavior specifically? Is it just generally because you want to port code more directly from GLSL?
kainino0x commentedon Sep 10, 2021
Reopening for discussion
axinging commentedon Sep 10, 2021
Yes, if mod and % behave the same, we can port code from GLSL to WGSL more directly.
Below is the use case (Note: the mod in WGSL is implemented by us, to make it the same as GLSL mod):
munrocket commentedon Sep 10, 2021
@axinging I believe it can be fixed like this
Also I will share my fixes
munrocket commentedon Sep 10, 2021
How this functions disassembles in MSL and HLSL? Should we be restricted by OpFRem semantic because this function exist on hardware level? Seems current specification mixing different conventions for int's and float's.
ben-clayton commentedon Sep 10, 2021
See the top of this issue and #1913 (comment).
HLSL and MSL have intrinsics that map to
OpFRem, which is why we went with it.GLSL is the odd one out here.
munrocket commentedon Sep 10, 2021
Nice to hear it, thanks! So we can count each FLOP just by using current spec. GLSL mod polyfill in WGSL will not give that possibility.
kvark commentedon Sep 10, 2021
I specifically wanted us to have the same behavior for ints and floats. Both should be
OpFRemtoday. What is different?munrocket commentedon Sep 10, 2021
Current spec:
T is i32 or vecN -> OpSMod -> sign matches the sign of Operand 2
T is u32 or vecN -> OpUMod -> sign matches the sign of Operand 2
T is f32 or vecN -> OpFRem -> sign matches the sign of Operand 1
Result sign matches to another operand for floats. Better to create fmod() for consistency.
Also I checked HLSL output in DXBC, seems that fmod just a syntax sugar there and gives 3 vectorised FLOP. Metal IR have such thing as fast_fmod, but I can imagine world when this IR become 3 FLOP eventually.
OpFMod is periodic function which is a good property, also expression
let uv = (xy % vec2<f32>(10.));gives you tiled texture if you using OpFMod. So in the wild even HLSL/MSL programmers creating#define mod(x, y) (x - y * floor(x / y))helper sometimes.kvark commentedon Sep 10, 2021
@munrocket the current spec doesn't reflect the group consensus on integer division. #1830 does, instead.
@dneto0 I think we should just land it.
munrocket commentedon Sep 10, 2021
I didn't know that, I am simple man that fixing my WGSL according to spec.
wgsl: Change `operator%(f32,32)` to use `OpFRem`