diff --git a/.gitignore b/.gitignore index 71d4771a..1f2f31d3 100644 --- a/.gitignore +++ b/.gitignore @@ -53,7 +53,7 @@ examples/asm/example/example examples/C/sum/sum examples/C/fir/fir examples/fp/softfloat_demo/softfloat_demo -examples/fp/softfloat_calc/softfloat_calc +examples/fp/fpcalc/fpcalc pipelined/src/fma/fma16_testgen linux/devicetree/debug/* !linux/devicetree/debug/dump-dts.sh diff --git a/examples/fp/softfloat_calc/softfloat_calc.c b/examples/fp/fpcalc/fpcalc.c similarity index 69% rename from examples/fp/softfloat_calc/softfloat_calc.c rename to examples/fp/fpcalc/fpcalc.c index a3396cf2..6b9dc535 100644 --- a/examples/fp/softfloat_calc/softfloat_calc.c +++ b/examples/fp/fpcalc/fpcalc.c @@ -29,26 +29,97 @@ typedef union dp { int opSize = 0; +void long2binstr(long val, char *str, int bits) { + int i; + long masked; + + if (val == 0) { // just return zero + str[0] = '0'; + str[1] = 0; + } else { + for (i=0; i> 10) & ((1<<5) -1); + sign = f.v >> 15 ? '-' : '+'; + //printf("%c %d %d ", sign, exp, fract); + if (exp == 0 && fract == 0) sprintf(sci, "%czero", sign); + else if (exp == 0 && fract != 0) sprintf(sci, "Denorm: %c0.%s x 2^-14", sign, fractstr); + else if (exp == 31 && fract == 0) sprintf(sci, "%cinf", sign); + else if (exp == 31 && fract != 0) sprintf(sci, "NaN Payload: %c%s", sign, fractstr); + else sprintf(sci, "%c1.%s x 2^%d", sign, fractstr, exp-15); + + printf ("%s: 0x%04x = %g = %s: Biased Exp %d Fract 0x%lx\n", + msg, convh.v, convf.f, sci, exp, fract); // no easy way to print half prec. } void printF32(char *msg, float32_t f) { sp conv; + long exp, fract; + char sign; + char sci[80], fractstr[80]; + conv.v = f.v; // use union to convert between hexadecimal and floating-point views - printf ("%s: 0x%08x = %g\n", msg, conv.v, conv.f); + + fract = f.v & ((1<<23) - 1); long2binstr(fract, fractstr, 23); + exp = (f.v >> 23) & ((1<<8) -1); + sign = f.v >> 31 ? '-' : '+'; + //printf("%c %d %d ", sign, exp, fract); + if (exp == 0 && fract == 0) sprintf(sci, "%czero", sign); + else if (exp == 0 && fract != 0) sprintf(sci, "Denorm: %c0.%s x 2^-126", sign, fractstr); + else if (exp == 255 && fract == 0) sprintf(sci, "%cinf", sign); + else if (exp == 255 && fract != 0) sprintf(sci, "NaN Payload: %c%s", sign, fractstr); + else sprintf(sci, "%c1.%s x 2^%d", sign, fractstr, exp-127); + + //printf ("%s: 0x%08x = %g\n", msg, conv.v, conv.f); + printf ("%s: 0x%08x = %g = %s: Biased Exp %d Fract 0x%lx\n", + msg, conv.v, conv.f, sci, exp, fract); } void printF64(char *msg, float64_t f) { dp conv; + long exp, fract; + long mask; + char sign; + char sci[80], fractstr[80]; + conv.v = f.v; // use union to convert between hexadecimal and floating-point views - printf ("%s: 0x%016lx = %lg\n", msg, conv.v, conv.d); + + mask = 1; mask = (mask << 52) - 1; + fract = f.v & mask; long2binstr(fract, fractstr, 52); + exp = (f.v >> 52) & ((1<<11) -1); + sign = f.v >> 63 ? '-' : '+'; + //printf("%c %d %d ", sign, exp, fract); + if (exp == 0 && fract == 0) sprintf(sci, "%czero", sign); + else if (exp == 0 && fract != 0) sprintf(sci, "Denorm: %c0.%s x 2^-1022", sign, fractstr); + else if (exp == 2047 && fract == 0) sprintf(sci, "%cinf", sign); + else if (exp == 2047 && fract != 0) sprintf(sci, "NaN Payload: %c%s", sign, fractstr); + else sprintf(sci, "%c1.%s x 2^%d", sign, fractstr, exp-1023); + + //printf ("%s: 0x%016lx = %lg\n", msg, conv.v, conv.d); + printf ("%s: 0x%016x = %g = %s: Biased Exp %d Fract 0x%lx\n", + msg, conv.v, conv.d, sci, exp, fract); } void printFlags(void) {