Skip to content

Instantly share code, notes, and snippets.

@brendandahl
Created August 27, 2024 00:46
Show Gist options
  • Save brendandahl/10b3ab8d4ac8abc7212e862833322c26 to your computer and use it in GitHub Desktop.
Save brendandahl/10b3ab8d4ac8abc7212e862833322c26 to your computer and use it in GitHub Desktop.
Compare FP16 results of Soft Float's sqrt to Converting FP16 to F32 and using std::sqrt
/*
* Using https://www.jhauser.us/arithmetic/SoftFloat.html
* and https://github.com/Maratyszcza/FP16
* compiling with:
* clang++ -std=c++20 -o sf -I ../../source/include/ -I ~/projects/binaryen/third_party/FP16/include/ main.cpp softfloat.a && ./sf
*/
extern "C" {
#include "softfloat.h"
}
#include <cmath>
#include <stdio.h>
#include <fp16.h>
bool IsNaN(uint16_t f) {
return ((f & 0x7c00) == 0x7c00) && ((f & 0x03ff) != 0);
}
int main() {
float16_t a_f16;
for (int i = 0; i < 65536; i++) {
a_f16.v = i;
float a_f = fp16_ieee_to_fp32_value(i);
uint16_t soft_float_result = f16_sqrt(a_f16).v;
uint16_t fp16_result = fp16_ieee_from_fp32_value(std::sqrt(a_f));
// Print out the results if they are different, but ignore NaN value differences.
if (soft_float_result != fp16_result && IsNaN(soft_float_result) != IsNaN(fp16_result)) {
printf("%hx %hx\n", soft_float_result, fp16_result);
}
}
printf("done\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment