Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat(gnovm): add software floating point package #3185

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Next Next commit
feat: add software floating point package
thehowl authored and omarsy committed Nov 23, 2024
commit b4cfed3b0a2a61720d6241c6636e82599ab4b598
32 changes: 32 additions & 0 deletions gnovm/pkg/gnolang/internal/softfloat/copy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/sh

# softfloat64.go:
# - add header
# - change package name
cat > runtime_softfloat64.go << EOF
// Code generated by copy.sh. DO NOT EDIT.
// This file is copied from \$GOROOT/src/runtime/softfloat64.go.
// It is the software floating point implementation used by the Go runtime.

EOF
cat "$GOROOT/src/runtime/softfloat64.go" >> ./runtime_softfloat64.go
sed -i 's/^package runtime$/package softfloat/' runtime_softfloat64.go

# softfloat64_test.go:
# - add header
# - change package name
# - change import to right package
# - change GOARCH to runtime.GOARCH, and import the "runtime" package
cat > runtime_softfloat64_test.go << EOF
// Code generated by copy.sh. DO NOT EDIT.
// This file is copied from \$GOROOT/src/runtime/softfloat64_test.go.
// It is the tests for the software floating point implementation
// used by the Go runtime.

EOF
cat "$GOROOT/src/runtime/softfloat64_test.go" >> ./runtime_softfloat64_test.go
sed -i 's/^package runtime_test$/package softfloat_test/
s#^\t\. "runtime"$#\t. "github.com/gnolang/gno/gnovm/pkg/gnolang/internal/softfloat"#
s/GOARCH/runtime.GOARCH/g
16a\
"runtime"' runtime_softfloat64_test.go
631 changes: 631 additions & 0 deletions gnovm/pkg/gnolang/internal/softfloat/runtime_softfloat64.go
204 changes: 204 additions & 0 deletions gnovm/pkg/gnolang/internal/softfloat/runtime_softfloat64_test.go
61 changes: 61 additions & 0 deletions gnovm/pkg/gnolang/internal/softfloat/softfloat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Package softfloat is a copy of the Go runtime's softfloat64.go file.
// It is a pure software floating point implementation. It can be used to
// perform determinstic, hardware-independent floating point computations.
//
// This package uses shortnames to refer to its different operations. Here is a
// quick reference:
//
// add f + g
// sub f - g
// mul f * g
// div f / g
// neg (- f)
// eq f == g
// gt f > g
// ge f >= g
package softfloat

// This file mostly exports the functions from runtime_softfloat64.go

//go:generate sh copy.sh

func Fadd64(f, g uint64) uint64 { return fadd64(f, g) }
func Fsub64(f, g uint64) uint64 { return fsub64(f, g) }
func Fmul64(f, g uint64) uint64 { return fmul64(f, g) }
func Fdiv64(f, g uint64) uint64 { return fdiv64(f, g) }
func Fneg64(f uint64) uint64 { return fneg64(f) }
func Feq64(f, g uint64) bool { return feq64(f, g) }
func Fgt64(f, g uint64) bool { return fgt64(f, g) }
func Fge64(f, g uint64) bool { return fge64(f, g) }

func Fadd32(f, g uint32) uint32 { return fadd32(f, g) }
func Fsub32(f, g uint32) uint32 { return fadd32(f, Fneg32(g)) }
func Fmul32(f, g uint32) uint32 { return fmul32(f, g) }
func Fdiv32(f, g uint32) uint32 { return fdiv32(f, g) }
func Feq32(f, g uint32) bool { return feq32(f, g) }
func Fgt32(f, g uint32) bool { return fgt32(f, g) }
func Fge32(f, g uint32) bool { return fge32(f, g) }

func Fneg32(f uint32) uint32 {
// Not defined in runtime - this is a copy similar to fneg64.
return f ^ (1 << (mantbits32 + expbits32))
}

// Conversions

func Fintto64(val int64) (f uint64) { return fintto64(val) }
func Fintto32(val int64) (f uint32) { return fintto32(val) }

func F32to64(f uint32) uint64 { return f32to64(f) }
func F32toint32(x uint32) int32 { return f32toint32(x) }
func F32toint64(x uint32) int64 { return f32toint64(x) }
func F32touint64(x uint32) uint64 { return f32touint64(x) }
func F64to32(f uint64) uint32 { return f64to32(f) }
func F64toint(f uint64) (val int64, ok bool) { return f64toint(f) }
func F64toint32(x uint64) int32 { return f64toint32(x) }
func F64toint64(x uint64) int64 { return f64toint64(x) }
func F64touint64(x uint64) uint64 { return f64touint64(x) }
func Fint32to32(x int32) uint32 { return fint32to32(x) }
func Fint32to64(x int32) uint64 { return fint32to64(x) }
func Fint64to32(x int64) uint32 { return fint64to32(x) }
func Fint64to64(x int64) uint64 { return fint64to64(x) }