From 077ab4838e4eacd001b2c30210a73c3fb538c5e4 Mon Sep 17 00:00:00 2001 From: "James E. Stine" Date: Tue, 3 Oct 2023 08:02:39 -0500 Subject: [PATCH] Modify Makefile + software example for SP/DP/QP --- examples/fp/softfloat_demo/Makefile | 11 +-- examples/fp/softfloat_demo/softfloat_demo2.c | 77 ---------------- examples/fp/softfloat_demo/softfloat_demoDP.c | 88 ++++++++++++++++++ examples/fp/softfloat_demo/softfloat_demoQP.c | 91 +++++++++++++++++++ examples/fp/softfloat_demo/softfloat_demoSP.c | 88 ++++++++++++++++++ 5 files changed, 272 insertions(+), 83 deletions(-) delete mode 100644 examples/fp/softfloat_demo/softfloat_demo2.c create mode 100644 examples/fp/softfloat_demo/softfloat_demoDP.c create mode 100644 examples/fp/softfloat_demo/softfloat_demoQP.c create mode 100644 examples/fp/softfloat_demo/softfloat_demoSP.c diff --git a/examples/fp/softfloat_demo/Makefile b/examples/fp/softfloat_demo/Makefile index 4d0efe20e..cc98d0cfb 100644 --- a/examples/fp/softfloat_demo/Makefile +++ b/examples/fp/softfloat_demo/Makefile @@ -2,14 +2,13 @@ CC = gcc CFLAGS = -O3 -LIBS = -lm LFLAGS = -L. # Link against the riscv-isa-sim version of SoftFloat rather than # the regular version to get RISC-V NaN behavior -IFLAGS = -I$(RISCV)/riscv-isa-sim/softfloat -LIBS = $(RISCV)/riscv-isa-sim/build/libsoftfloat.a -#IFLAGS = -I../../../addins/SoftFloat-3e/source/include/ -#LIBS = ../../../addins/SoftFloat-3e/build/Linux-x86_64-GCC/softfloat.a +#IFLAGS = -I$(RISCV)/riscv-isa-sim/softfloat +#LIBS = $(RISCV)/riscv-isa-sim/build/libsoftfloat.a +IFLAGS = -I../../../addins/SoftFloat-3e/source/include/ +LIBS = ../../../addins/SoftFloat-3e/build/Linux-x86_64-GCC/softfloat.a -lm -lquadmath SRCS = $(wildcard *.c) PROGS = $(patsubst %.c,%,$(SRCS)) @@ -17,7 +16,7 @@ PROGS = $(patsubst %.c,%,$(SRCS)) all: $(PROGS) %: %.c - $(CC) $(CFLAGS) $(IFLAGS) $(LFLAGS) -o $@ $< $(LIBS) + $(CC) $(CFLAGS) -DSOFTFLOAT_FAST_INT64 $(IFLAGS) $(LFLAGS) -o $@ $< $(LIBS) clean: rm -f $(PROGS) diff --git a/examples/fp/softfloat_demo/softfloat_demo2.c b/examples/fp/softfloat_demo/softfloat_demo2.c deleted file mode 100644 index d0582724c..000000000 --- a/examples/fp/softfloat_demo/softfloat_demo2.c +++ /dev/null @@ -1,77 +0,0 @@ -// -// softfloat_div.c -// james.stine@okstate.edu 12 April 2023 -// -// Demonstrate using SoftFloat to compute 754 fp divide, then print results -// (adapted from original C built by David Harris) -// - -#include -#include -#include "softfloat.h" -#include "softfloat_types.h" -typedef union sp { - uint32_t v; - unsigned short x[2]; - float f; -} sp; - -void printF32 (char *msg, float32_t f) { - sp conv; - int i, j; - conv.v = f.v; // use union to convert between hexadecimal and floating-point views - printf("%s: ", msg); // print out nicely - printf("0x%04x_%04x = %1.15g\n", (conv.v >> 16),(conv.v & 0xFFFF), conv.f); -} - -void printFlags(void) { - int NX = softfloat_exceptionFlags % 2; - int UF = (softfloat_exceptionFlags >> 1) % 2; - int OF = (softfloat_exceptionFlags >> 2) % 2; - int DZ = (softfloat_exceptionFlags >> 3) % 2; - int NV = (softfloat_exceptionFlags >> 4) % 2; - printf ("Flags: Inexact %d Underflow %d Overflow %d DivideZero %d Invalid %d\n", - NX, UF, OF, DZ, NV); -} - -void softfloatInit(void) { - // RNE: softfloat_round_near_even - // RZ: softfloat_round_minMag - // RU: softfloat_round_max - // RD: softfloat_round_min - // RM: softfloat_round_near_maxMag - softfloat_roundingMode = softfloat_round_near_even; - softfloat_exceptionFlags = 0; // clear exceptions - softfloat_detectTininess = softfloat_tininess_afterRounding; // RISC-V behavior for tininess -} - -int main() { - - // float32_t is typedef in SoftFloat - float32_t x, y, r1, r2; - sp convx, convy; - - // Choose two random values - convx.f = 1.30308703073; - convy.f = 1.903038030370; - // Convert to SoftFloat format - x.v = (convx.x[1] << 16) + convx.x[0]; - y.v = (convy.x[1] << 16) + convy.x[0]; - - printf("Example using SoftFloat\n"); - - softfloatInit(); - r1 = f32_div(x, y); - printf("-------\n"); - printF32("X", x); - printF32("Y", y); - printF32("result = X/Y", r1); - printFlags(); - - r2 = f32_sqrt(x); - printf("-------\n"); - printF32("X", x); - printF32("result = sqrt(X)", r2); - printFlags(); - -} diff --git a/examples/fp/softfloat_demo/softfloat_demoDP.c b/examples/fp/softfloat_demo/softfloat_demoDP.c new file mode 100644 index 000000000..50b3d0b53 --- /dev/null +++ b/examples/fp/softfloat_demo/softfloat_demoDP.c @@ -0,0 +1,88 @@ +// softfloat_demo3.c +// james.stine@okstate.edu 15 August 2023 +// +// Demonstrate using SoftFloat do compute a floating-point for quad, then print results + +#include +#include +#include +#include // GCC Quad-Math Library +#include "softfloat.h" +#include "softfloat_types.h" +typedef union sp { + uint32_t v; + float f; +} sp; + +typedef union dp { + uint64_t v; + double d; +} dp; + +typedef union qp { + uint64_t v[2]; + __float128 q; +} qp; + + +void printF32 (char *msg, float32_t f) { + sp conv; + int i, j; + conv.v = f.v; // use union to convert between hexadecimal and floating-point views + printf("%s: ", msg); // print out nicely + printf("0x%04x_%04x = %g\n", (conv.v >> 16),(conv.v & 0xFFFF), conv.f); +} + +void printF64 (char *msg, float64_t d) { + dp conv; + int i, j; + conv.v = d.v; // use union to convert between hexadecimal and floating-point views + printf("%s: ", msg); // print out nicely + printf("0x%08x_%08x = %g\n", (conv.v >> 32),(conv.v & 0xFFFFFFFF), conv.d); +} + +void printF128 (char *msg, float128_t q) { + qp conv; + int i, j; + conv.v[0] = q.v[0]; // use union to convert between hexadecimal and floating-point views + conv.v[1] = q.v[1]; // use union to convert between hexadecimal and floating-point views + printf("%s: ", msg); // print out nicely + printf("0x%016" PRIx64 "_%016" PRIx64 " = %1.15Qe\n", q.v[1], q.v[0], conv.q); +} + +void printFlags(void) { + int NX = softfloat_exceptionFlags % 2; + int UF = (softfloat_exceptionFlags >> 1) % 2; + int OF = (softfloat_exceptionFlags >> 2) % 2; + int DZ = (softfloat_exceptionFlags >> 3) % 2; + int NV = (softfloat_exceptionFlags >> 4) % 2; + printf ("Flags: Inexact %d Underflow %d Overflow %d DivideZero %d Invalid %d\n", + NX, UF, OF, DZ, NV); +} + +void softfloatInit(void) { + // rounding modes: RNE: softfloat_round_near_even + // RZ: softfloat_round_minMag + // RP: softfloat_round_max + // RM: softfloat_round_min + softfloat_roundingMode = softfloat_round_near_even; + softfloat_exceptionFlags = 0; // clear exceptions + softfloat_detectTininess = softfloat_tininess_afterRounding; // RISC-V behavior for tininess +} + +int main() { + + float64_t x, y, z; + float64_t r; + + x.v = 0xBFFF988ECE97DFEB; + y.v = 0x3F8EFFFFFFFFFFFF; + z.v = 0x4001000000000000; + + softfloatInit(); + printF64("X", x); printF64("Y", y); printF64("Z", z); + r = f64_mulAdd(x, y, z); + printf("\n"); + printF64("r", r); + +} diff --git a/examples/fp/softfloat_demo/softfloat_demoQP.c b/examples/fp/softfloat_demo/softfloat_demoQP.c new file mode 100644 index 000000000..03f0e5edb --- /dev/null +++ b/examples/fp/softfloat_demo/softfloat_demoQP.c @@ -0,0 +1,91 @@ +// softfloat_demo3.c +// james.stine@okstate.edu 15 August 2023 +// +// Demonstrate using SoftFloat do compute a floating-point for quad, then print results + +#include +#include +#include +#include // GCC Quad-Math Library +#include "softfloat.h" +#include "softfloat_types.h" +typedef union sp { + uint32_t v; + float f; +} sp; + +typedef union dp { + uint64_t v; + double d; +} dp; + +typedef union qp { + uint64_t v[2]; + __float128 q; +} qp; + + +void printF32 (char *msg, float32_t f) { + sp conv; + int i, j; + conv.v = f.v; // use union to convert between hexadecimal and floating-point views + printf("%s: ", msg); // print out nicely + printf("0x%04x_%04x = %g\n", (conv.v >> 16),(conv.v & 0xFFFF), conv.f); +} + +void printF64 (char *msg, float64_t d) { + dp conv; + int i, j; + conv.v = d.v; // use union to convert between hexadecimal and floating-point views + printf("%s: ", msg); // print out nicely + printf("0x%08x_%08x = %g\n", (conv.v >> 32),(conv.v & 0xFFFFFFFF), conv.d); +} + +void printF128 (char *msg, float128_t q) { + qp conv; + int i, j; + conv.v[0] = q.v[0]; // use union to convert between hexadecimal and floating-point views + conv.v[1] = q.v[1]; // use union to convert between hexadecimal and floating-point views + printf("%s: ", msg); // print out nicely + printf("0x%016" PRIx64 "_%016" PRIx64 " = %1.15Qe\n", q.v[1], q.v[0], conv.q); +} + +void printFlags(void) { + int NX = softfloat_exceptionFlags % 2; + int UF = (softfloat_exceptionFlags >> 1) % 2; + int OF = (softfloat_exceptionFlags >> 2) % 2; + int DZ = (softfloat_exceptionFlags >> 3) % 2; + int NV = (softfloat_exceptionFlags >> 4) % 2; + printf ("Flags: Inexact %d Underflow %d Overflow %d DivideZero %d Invalid %d\n", + NX, UF, OF, DZ, NV); +} + +void softfloatInit(void) { + // rounding modes: RNE: softfloat_round_near_even + // RZ: softfloat_round_minMag + // RP: softfloat_round_max + // RM: softfloat_round_min + softfloat_roundingMode = softfloat_round_near_even; + softfloat_exceptionFlags = 0; // clear exceptions + softfloat_detectTininess = softfloat_tininess_afterRounding; // RISC-V behavior for tininess +} + +int main() { + + float128_t x, y, z; + float128_t r; + + x.v[1] = 0xBFFF988ECE97DFEB; + x.v[0] = 0xC3BBA082445B4836; + y.v[1] = 0x3F8EFFFFFFFFFFFF; + y.v[0] = 0xFFFFFFFFFFFFFFFF; + z.v[1] = 0x4001000000000000; + z.v[0] = 0x0000000000000000; + + softfloatInit(); + printF128("X", x); printF128("Y", y); printF128("Z", z); + r = f128_mulAdd(x, y, z); + printf("\n"); + printF128("r", r); + +} diff --git a/examples/fp/softfloat_demo/softfloat_demoSP.c b/examples/fp/softfloat_demo/softfloat_demoSP.c new file mode 100644 index 000000000..55c5ef991 --- /dev/null +++ b/examples/fp/softfloat_demo/softfloat_demoSP.c @@ -0,0 +1,88 @@ +// softfloat_demo3.c +// james.stine@okstate.edu 15 August 2023 +// +// Demonstrate using SoftFloat do compute a floating-point for quad, then print results + +#include +#include +#include +#include // GCC Quad-Math Library +#include "softfloat.h" +#include "softfloat_types.h" +typedef union sp { + uint32_t v; + float f; +} sp; + +typedef union dp { + uint64_t v; + double d; +} dp; + +typedef union qp { + uint64_t v[2]; + __float128 q; +} qp; + + +void printF32 (char *msg, float32_t f) { + sp conv; + int i, j; + conv.v = f.v; // use union to convert between hexadecimal and floating-point views + printf("%s: ", msg); // print out nicely + printf("0x%04x_%04x = %g\n", (conv.v >> 16),(conv.v & 0xFFFF), conv.f); +} + +void printF64 (char *msg, float64_t d) { + dp conv; + int i, j; + conv.v = d.v; // use union to convert between hexadecimal and floating-point views + printf("%s: ", msg); // print out nicely + printf("0x%08x_%08x = %g\n", (conv.v >> 32),(conv.v & 0xFFFFFFFF), conv.d); +} + +void printF128 (char *msg, float128_t q) { + qp conv; + int i, j; + conv.v[0] = q.v[0]; // use union to convert between hexadecimal and floating-point views + conv.v[1] = q.v[1]; // use union to convert between hexadecimal and floating-point views + printf("%s: ", msg); // print out nicely + printf("0x%016" PRIx64 "_%016" PRIx64 " = %1.15Qe\n", q.v[1], q.v[0], conv.q); +} + +void printFlags(void) { + int NX = softfloat_exceptionFlags % 2; + int UF = (softfloat_exceptionFlags >> 1) % 2; + int OF = (softfloat_exceptionFlags >> 2) % 2; + int DZ = (softfloat_exceptionFlags >> 3) % 2; + int NV = (softfloat_exceptionFlags >> 4) % 2; + printf ("Flags: Inexact %d Underflow %d Overflow %d DivideZero %d Invalid %d\n", + NX, UF, OF, DZ, NV); +} + +void softfloatInit(void) { + // rounding modes: RNE: softfloat_round_near_even + // RZ: softfloat_round_minMag + // RP: softfloat_round_max + // RM: softfloat_round_min + softfloat_roundingMode = softfloat_round_near_even; + softfloat_exceptionFlags = 0; // clear exceptions + softfloat_detectTininess = softfloat_tininess_afterRounding; // RISC-V behavior for tininess +} + +int main() { + + float32_t x, y, z; + float32_t r; + + x.v = 0xBFFF988E; + y.v = 0x3F8EFFFF; + z.v = 0x40010000; + + softfloatInit(); + printF32("X", x); printF32("Y", y); printF32("Z", z); + r = f32_mulAdd(x, y, z); + printf("\n"); + printF32("r", r); + +}