Skip to content
New issue

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

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

Already on GitHub? # to your account

Configurable floating-point precision #137

Open
yansendao opened this issue May 27, 2021 · 0 comments
Open

Configurable floating-point precision #137

yansendao opened this issue May 27, 2021 · 0 comments

Comments

@yansendao
Copy link

Background

Some compilation optimization options, such as the -Ofast option, cause a range of floating-point calculation errors that are permissible. However, the running result of the current csmith calculation program compares all bits and does not allow floating-point calculation errors.

Therefore, the source code of the csmith tool needs to be modified to allow certain errors in the calculation results of certain compilation optimization options.

Scheme

$ git diff runtime/csmith.h                
diff --git a/runtime/csmith.h b/runtime/csmith.h
index 6ed44ff..0be747e 100644
--- a/runtime/csmith.h
+++ b/runtime/csmith.h
@@ -117,12 +117,15 @@ transparent_crc (uint64_t val, char* vname, int flag)
 
 #endif
 
+#ifndef FLOAT_MASK
+#define FLOAT_MASK 0xFFFFFFFF
+#endif
 static void 
 transparent_crc_bytes (char *ptr, int nbytes, char* vname, int flag)
 {
     int i;
     for (i=0; i<nbytes; i++) {
-        crc32_byte(ptr[i]);
+        crc32_byte(ptr[i] & FLOAT_MASK);
     }
        if (flag) {
                printf("...checksum after hashing %s : %lX\n", vname, crc32_context ^ 0xFFFFFFFFUL);

result

yansendao@jvmtest-129:dir-1$ clang -lm -O0 -I ~/software/csmith/include -w test.c && ./a.out
checksum = 8802805D
yansendao@jvmtest-129:dir-1$ clang -lm -mcpu=tsv110 -Ofast --rtlib=compiler-rt -mrecip -lunwind -I ~/software/csmith/include -w test.c && ./a.out
checksum = 2F4608A
yansendao@jvmtest-129:dir-1$ clang -lm -mcpu=tsv110 -Ofast --rtlib=compiler-rt -mrecip -lunwind -ffp-model=precise -I ~/software/csmith/include -w test.c && ./a.out
checksum = 8802805D
yansendao@jvmtest-129:dir-1$ 
yansendao@jvmtest-129:dir-1$ 
yansendao@jvmtest-129:dir-1$ clang -DFLOAT_MASK=0xFFFFFF00 -lm -O0 -I ~/software/csmith/include -w test.c && ./a.out    
checksum = C782D06E
yansendao@jvmtest-129:dir-1$ clang -DFLOAT_MASK=0xFFFFFF00 -lm -mcpu=tsv110 -Ofast --rtlib=compiler-rt -mrecip -lunwind -I ~/software/csmith/include -w test.c && ./a.out 
checksum = C782D06E
yansendao@jvmtest-129:dir-1$ clang -DFLOAT_MASK=0xFFFFFF00 -lm -mcpu=tsv110 -Ofast --rtlib=compiler-rt -mrecip -lunwind -ffp-model=precise -I ~/software/csmith/include -w test.c && ./a.out
checksum = C782D06E
yansendao@jvmtest-129:dir-1$ head test.c 
/*
 * This is a RANDOMLY GENERATED PROGRAM.
 *
 * Generator: csmith 2.4.0
 * Git version: deddca6
 * Options:   --float --builtins --builtin-function-prob 100 -s 2745091934184403458 -o test.c
 * Seed:      2745091934184403458
 */

#include <float.h>

image

Floating-point error situation

image

unsigned int float2hexRepr(float* a){
  unsigned int c;  
  c= ((unsigned int*)a)[0];   
  return c;
}
int printf(const char *, ...);
int a;
char b;
float c, d, e;
short f, j;
static int g = 234335540;
static int *h[][7] = {{}, {}, &g};
int *i;
float *k = &c;
void l(char *m) {
  a = m[0];
  printf("%lX\n", a ^ 5UL);
}
int *n();
void o() {}
static unsigned char fn2(unsigned short m) {
  for (;;) {
    if (m)
      break;
    h[0][f] = n;
  }
  return j;
}
int *n(short *m, unsigned q) {
  e = 0x0p-1 + q;
  d = e / q;
  *k = d;
  return h[2][3];
}
int main() {
  fn2(8);
  i = n(o, g);
  printf("%x\t%f\n", float2hexRepr(&c), c);
  //l(&c);
}
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant