Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hermes does not canonicalize NaNs stored in typed array #1639

Open
JimWongM opened this issue Mar 6, 2025 · 4 comments
Open

Hermes does not canonicalize NaNs stored in typed array #1639

JimWongM opened this issue Mar 6, 2025 · 4 comments
Labels
need more info Awating additional info before proceeding

Comments

@JimWongM
Copy link

JimWongM commented Mar 6, 2025

Bug Description

Hermes return different results in O0 and Og.

Hermes git revision (if applicable):01d96f4198789446b0e14226ec646b38f0ec3fd0 in branch static_h
OS: Linux
Platform: x86_64

Steps To Reproduce

  1. Compile hermes in Debug mode
  2. Run: ./hermes -O0 test.js
  3. Run: ./hermes -Og test.js

code example:

let buffer = new ArrayBuffer(96);
var arr64 = new Float64Array(buffer);
var arr32 = new Float32Array(buffer);
arr32[4] = -NaN;
print(arr64[2]);

Result of Step 2:
1.058925634e-314

Result of Step 3:
2.1199235295e-314

The Expected Behavior

The results of Step 2 and Step 3 should be the same.

@JimWongM JimWongM added the bug Something isn't working label Mar 6, 2025
@tmikov
Copy link
Contributor

tmikov commented Mar 6, 2025

Thank you. A better repro:

let buffer = new ArrayBuffer(4);
var arrInt = new Int32Array(buffer);
var arr32 = new Float32Array(buffer);
var a = NaN;
arr32[0] = -a;
print(arr32[0]);
print(arrInt[0].toString(16));

v8: -400000
sh-hermes: 7fc00000

@tmikov
Copy link
Contributor

tmikov commented Mar 6, 2025

Is this actually a bug? These seem to be different representations of NaN.

@tmikov
Copy link
Contributor

tmikov commented Mar 6, 2025

According to the spec:

    1. If type is float32, then
      a. a. Let rawBytes be a List whose elements are the 4 bytes that are the result of converting value to IEEE 754-2019 binary32 format using roundTiesToEven mode. The bytes are arranged in little endian order. If value is NaN, rawBytes may be set to any implementation chosen IEEE 754-2019 binary32 format Not-a-Number encoding. An implementation must always choose the same encoding for each implementation distinguishable NaN value.

Not 100% sure what it means.

@tmikov tmikov changed the title Hermes incorrectly handle -NaN Hermes does not canonicalize NaNs stored in typed array Mar 6, 2025
@tmikov tmikov added need more info Awating additional info before proceeding and removed bug Something isn't working labels Mar 6, 2025
@tmikov
Copy link
Contributor

tmikov commented Mar 6, 2025

So far my interpretation of the spec and various discussions is that this is fine. Canonicalizing every NaN on writes to a floating point typed array might get pretty expensive in some scenarios.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need more info Awating additional info before proceeding
Projects
None yet
Development

No branches or pull requests

2 participants