webmachinelearning / webnn-polyfill

🧠⚙️ Web Neural Network API polyfill based on TensorFlow.js
https://www.npmjs.com/package/@webmachinelearning/webnn-polyfill
Apache License 2.0
102 stars 18 forks source link

Use ULP based comparison. #139

Closed BruceDai closed 2 years ago

BruceDai commented 3 years ago
int64_t GetBitwise(float value) {
    int64_t bitwiseValue = (value < T(0)) ? ~int64_t(0) : 0; // Extend sign.
    *std::launder(reinterpret_cast<T*>(&bitwiseValue)) = value;
    return bitwiseValue;
}

bool CompareUlp(float a, float b, uint64_t ulp) {
    return static_cast<uint64_t>(abs(GetBitwise(a) - GetBitwise(b))) > ulp;
}

@huningxin I implemented JavaScript compareUlp() function to align Chai's comment on #2, PTAL, thanks.

BruceDai commented 3 years ago

Thanks @huningxin .

I updated PR following your suggestion, now new getBitwise() function is only for float32 data

function getBitwise(value) {
  const buffer = new ArrayBuffer(4);
  const view = new DataView(buffer);
  view.setFloat32(0, value);
  return view.getInt32(0);
}

About bitwise of float32 data, I compared the return value of this JS function with one of native C++ GetBitwise() function, they are same.

@huningxin Please take a look again, thanks.

BruceDai commented 3 years ago

Thanks much @huningxin. I've updated commit to fix your review comments, PTAL, Thanks.

BruceDai commented 3 years ago

@huningxin I updated PR, PTAL, thanks.

BruceDai commented 2 years ago

I rebased this PR to only add JavaScript's ULP based comparison implementation.

@huningxin PTAL, thanks.