Skip to content

Commit

Permalink
Update isnan implementation in WebGL backend to follow IEEE 754-1985 (#…
Browse files Browse the repository at this point in the history
…6107)

* Update isnan implementation in WebGL backend to follow IEEE 754-1985

Previous implementation of isNaN in WebGL relies on the platform behaviour.
And it might generate incorrect results(See #5800).
This PR implements isnan based on the rules in IEEE 754-1985 to restrict
the rules.

Issue:#5800

* Only apply bit version isnan for WebGL2

* Fix 80-line exceeed issue
  • Loading branch information
shaoboyan authored Feb 17, 2022
1 parent 4d3ef72 commit a51929e
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions tfjs-backend-webgl/src/glsl_version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,19 @@ export function getGlslDifferences(): GLSL {
// Use custom isnan definition to work across differences between
// implementations on various platforms. While this should happen in ANGLE
// we still see differences between android and windows (on chrome) when
// using isnan directly.
// using isnan directly. Since WebGL2 supports uint type and
// floatBitsToUinT built-in function, we could implment isnan following
// IEEE 754 rules.
// NaN defination in IEEE 754-1985 is :
// - sign = either 0 or 1.
// - biased exponent = all 1 bits.
// - fraction = anything except all 0 bits (since all 0 bits represents
// infinity).
// https://en.wikipedia.org/wiki/IEEE_754-1985#Representation_of_non-numbers
defineSpecialNaN = `
bool isnan_custom(float val) {
return (val > 0.0 || val < 0.0) ? false : val != 0.0;
uint floatToUint = floatBitsToUint(val);
return (floatToUint & 0x7fffffffu) > 0x7f800000u;
}
bvec4 isnan_custom(vec4 val) {
Expand Down

0 comments on commit a51929e

Please sign in to comment.