1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include "Halide.h"
#include <stdio.h>
using namespace Halide;
int bits_diff(float fa, float fb) {
uint32_t a = Halide::Internal::reinterpret_bits<uint32_t>(fa);
uint32_t b = Halide::Internal::reinterpret_bits<uint32_t>(fb);
uint32_t a_exp = a >> 23;
uint32_t b_exp = b >> 23;
if (a_exp != b_exp) return -100;
uint32_t diff = a > b ? a - b : b - a;
int count = 0;
while (diff) {
count++;
diff /= 2;
}
return count;
}
int main(int argc, char **argv) {
Func f;
Var x;
f(x) = erf((x - 50000) / 10000.0f);
f.vectorize(x, 8);
Buffer<float> im = f.realize({100000});
int max_err = 0;
float max_err_x = 0;
for (int i = 0; i < 100000; i++) {
float x = (i - 50000) / 10000.0f;
float correct = erff(x);
float approx = im(i);
int err = bits_diff(correct, approx);
if (err > max_err) {
max_err = err;
max_err_x = x;
}
}
printf("Maximum number of incorrect mantissa bits: %d @ %g\n", max_err, max_err_x);
if (max_err <= 4) {
printf("Success!\n");
return 0;
} else {
return 1;
}
}
|