mirror of
https://github.com/openhwgroup/cvw.git
synced 2025-04-22 12:57:23 -04:00
Created softfloat_demo showcasing how to do math with SoftFloat
This commit is contained in:
parent
046259cff8
commit
bb14dba9be
8 changed files with 78 additions and 99 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -52,6 +52,8 @@ examples/asm/sumtest/sumtest
|
|||
examples/asm/example/example
|
||||
examples/C/sum/sum
|
||||
examples/C/fir/fir
|
||||
examples/fp/softfloat_demo/softfloat_demo
|
||||
pipelined/src/fma/fma16_testgen
|
||||
linux/devicetree/debug/*
|
||||
!linux/devicetree/debug/dump-dts.sh
|
||||
linux/testvector-generation/genCheckpoint.gdb
|
||||
|
|
23
examples/fp/softfloat_demo/Makefile
Normal file
23
examples/fp/softfloat_demo/Makefile
Normal file
|
@ -0,0 +1,23 @@
|
|||
# Makefile
|
||||
|
||||
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
|
||||
SRCS = $(wildcard *.c)
|
||||
|
||||
PROGS = $(patsubst %.c,%,$(SRCS))
|
||||
|
||||
all: $(PROGS)
|
||||
|
||||
%: %.c
|
||||
$(CC) $(CFLAGS) $(IFLAGS) $(LFLAGS) -o $@ $< $(LIBS)
|
||||
|
||||
clean:
|
||||
rm -f $(PROGS)
|
53
examples/fp/softfloat_demo/softfloat_demo.c
Normal file
53
examples/fp/softfloat_demo/softfloat_demo.c
Normal file
|
@ -0,0 +1,53 @@
|
|||
// softfloat_demo.c
|
||||
// David_Harris@hmc.edu 27 February 2022
|
||||
//
|
||||
// Demonstrate using SoftFloat do compute a floating-point, then print results
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include "softfloat.h"
|
||||
#include "softfloat_types.h"
|
||||
typedef union sp {
|
||||
uint32_t v;
|
||||
float f;
|
||||
} sp;
|
||||
|
||||
void printF32(char *msg, float32_t f) {
|
||||
sp conv;
|
||||
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);
|
||||
}
|
||||
|
||||
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 ("exceptions: 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_beforeRounding; // RISC-V behavior for tininess
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
float32_t x, y, z, r;
|
||||
|
||||
x.v = 0x3fc00000;
|
||||
y.v = 0x3fc00000;
|
||||
z.v = 0x00000001;
|
||||
|
||||
softfloatInit();
|
||||
r = f32_mulAdd(x, y, z);
|
||||
printF32("X", x); printF32("Y", y); printF32("Z", z);
|
||||
printF32("result = X*Y+Z", r); printFlags();
|
||||
}
|
Binary file not shown.
|
@ -1,52 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include "softfloat.h"
|
||||
#include "softfloat_types.h"
|
||||
|
||||
int float_rounding_mode = 0;
|
||||
|
||||
union dp {
|
||||
unsigned short x[4];
|
||||
double y;
|
||||
} X;
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
uint8_t rounding_mode;
|
||||
uint8_t exceptions;
|
||||
|
||||
uint64_t n, d, result;
|
||||
float64_t d_n, d_d, d_result;
|
||||
|
||||
n = 0x3feffffffefffff6;
|
||||
d = 0xffeffffffffffffe;
|
||||
//n = 0x00000000400001ff;
|
||||
//d = 0x3ffffdfffffffbfe;
|
||||
|
||||
d_n.v = n;
|
||||
d_d.v = d;
|
||||
|
||||
softfloat_roundingMode = rounding_mode;
|
||||
softfloat_exceptionFlags = 0;
|
||||
softfloat_detectTininess = softfloat_tininess_beforeRounding;
|
||||
|
||||
d_result = f64_div(d_n, d_d);
|
||||
|
||||
//result = d_result.v;
|
||||
//exceptions = softfloat_exceptionFlags & 0x1f;
|
||||
|
||||
X.x[3] = (d_result.v & 0xffff000000000000) >> 48;
|
||||
X.x[2] = (d_result.v & 0x0000ffff00000000) >> 32;
|
||||
X.x[1] = (d_result.v & 0x00000000ffff0000) >> 16;
|
||||
X.x[0] = (d_result.v & 0x000000000000ffff);
|
||||
|
||||
printf("Number = %.4x\n", X.x[3]);
|
||||
printf("Number = %.4x\n", X.x[2]);
|
||||
printf("Number = %.4x\n", X.x[1]);
|
||||
printf("Number = %.4x\n", X.x[0]);
|
||||
printf("Number = %1.25lg\n", X.y);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
Binary file not shown.
Binary file not shown.
|
@ -1,47 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include "softfloat.h"
|
||||
#include "softfloat_types.h"
|
||||
|
||||
int float_rounding_mode = 0;
|
||||
|
||||
union sp {
|
||||
unsigned short x[2];
|
||||
float y;
|
||||
} X;
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
uint8_t rounding_mode;
|
||||
uint8_t exceptions;
|
||||
|
||||
uint32_t multiplier, multiplicand, addend, result;
|
||||
float32_t f_multiplier, f_multiplicand, f_addend, f_result;
|
||||
|
||||
multiplier = 0xbf800000;
|
||||
multiplicand = 0xbf800000;
|
||||
addend = 0xffaaaaaa;
|
||||
|
||||
f_multiplier.v = multiplier;
|
||||
f_multiplicand.v = multiplicand;
|
||||
f_addend.v = addend;
|
||||
|
||||
softfloat_roundingMode = rounding_mode;
|
||||
softfloat_exceptionFlags = 0;
|
||||
softfloat_detectTininess = softfloat_tininess_beforeRounding;
|
||||
|
||||
f_result = f32_mulAdd(f_multiplier, f_multiplicand, f_addend);
|
||||
|
||||
result = f_result.v;
|
||||
exceptions = softfloat_exceptionFlags & 0x1f;
|
||||
|
||||
printf("%x\n", f_result.v);
|
||||
|
||||
// Print out SP number
|
||||
X.x[1] = (f_result.v & 0xffff0000) >> 16;
|
||||
X.x[0] = (f_result.v & 0x0000ffff);
|
||||
printf("Number = %f\n", X.y);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue