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 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b4cfed3
feat: add software floating point package
thehowl Sep 27, 2024
5c6ce77
add Fcmp64
thehowl Oct 2, 2024
4fabee4
feat: use soft float
omarsy Nov 23, 2024
7b9b429
Merge branch 'master' into feat/312
omarsy Nov 28, 2024
853a9d3
feat: small refactoring
omarsy Nov 28, 2024
8b46e01
feat: refactor again
omarsy Dec 1, 2024
c8445ff
Merge branch 'master' into feat/312
omarsy Dec 2, 2024
004d5d2
fix: test
omarsy Dec 3, 2024
97dee5f
Merge branch 'master' into feat/312
omarsy Dec 3, 2024
30f55c3
Merge branch 'master' into feat/312
thehowl Dec 6, 2024
5d40f7c
Merge branch 'master' into feat/312
omarsy Dec 7, 2024
c0808ea
Merge branch 'master' into feat/312
omarsy Dec 13, 2024
08dba29
Merge branch 'master' into feat/312
omarsy Dec 17, 2024
7f7b888
feat: use softfloat as is it
omarsy Dec 20, 2024
77cf58f
Merge branch 'master' into feat/312
omarsy Dec 21, 2024
4f60124
Merge branch 'master' into feat/312
omarsy Dec 21, 2024
01e32ce
Merge branch 'master' into feat/312
omarsy Dec 26, 2024
24deeb8
Merge branch 'master' into feat/312
omarsy Dec 27, 2024
051148f
Merge branch 'master' into feat/312
omarsy Dec 28, 2024
f224d1a
Merge branch 'master' into feat/312
omarsy Jan 7, 2025
13e7ae2
refactor: change variables to constants in float8.gno
omarsy Jan 8, 2025
3919e17
Merge branch 'master' into feat/312
omarsy Jan 8, 2025
def11a8
Merge remote-tracking branch 'upstream/master' into feat/312
omarsy Jan 9, 2025
58bd8ee
fix: update float constants and add minimum values for better precisi…
omarsy Jan 9, 2025
a48f8ae
Merge branch 'master' into feat/312
omarsy Jan 9, 2025
9a3f5a9
update float8 to test both var and const
thehowl Jan 10, 2025
ef75f16
Merge branch 'feat/312' of github.com:omarsy/gno into feat/312
thehowl Jan 10, 2025
b7411cc
Merge branch 'master' of github.com:gnolang/gno into feat/312
thehowl Jan 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions gno.land/pkg/sdk/vm/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vm
import (
"encoding/base64"
"fmt"
"math"
"strconv"
"strings"

Expand Down Expand Up @@ -143,11 +144,11 @@ func convertArgToGno(arg string, argT gno.Type) (tv gno.TypedValue) {
return
case gno.Float32Type:
value := convertFloat(arg, 32)
tv.SetFloat32(float32(value))
tv.SetFloat32(math.Float32bits(float32(value)))
return
case gno.Float64Type:
value := convertFloat(arg, 64)
tv.SetFloat64(value)
tv.SetFloat64(math.Float64bits(value))
return
default:
panic(fmt.Sprintf("unexpected primitive type %s", bt.String()))
Expand Down
5 changes: 3 additions & 2 deletions gnovm/pkg/gnolang/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gnolang

import (
"fmt"
"math"
"strings"
)

Expand Down Expand Up @@ -207,9 +208,9 @@ func toConstExpTrace(cte *ConstExpr) string {
case Uint64Type:
return fmt.Sprintf("%d", tv.GetUint64())
case Float32Type:
return fmt.Sprintf("%v", tv.GetFloat32())
return fmt.Sprintf("%v", math.Float32frombits(tv.GetFloat32()))
case Float64Type:
return fmt.Sprintf("%v", tv.GetFloat64())
return fmt.Sprintf("%v", math.Float64frombits(tv.GetFloat64()))
}
}

Expand Down
19 changes: 11 additions & 8 deletions gnovm/pkg/gnolang/gonative.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import (
"fmt"
"math"
"reflect"

"github.com/gnolang/gno/gnovm/pkg/gnolang/internal/softfloat"
)

// NOTE
Expand Down Expand Up @@ -329,9 +332,9 @@
case reflect.Uint64:
tv.SetUint64(rv.Uint())
case reflect.Float32:
tv.SetFloat32(float32(rv.Float()))
tv.SetFloat32(softfloat.F64to32(math.Float64bits(rv.Float())))

Check warning on line 335 in gnovm/pkg/gnolang/gonative.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/gonative.go#L335

Added line #L335 was not covered by tests
case reflect.Float64:
tv.SetFloat64(rv.Float())
tv.SetFloat64(math.Float64bits(rv.Float()))

Check warning on line 337 in gnovm/pkg/gnolang/gonative.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/gonative.go#L337

Added line #L337 was not covered by tests
case reflect.Array:
tv.V = alloc.NewNative(rv)
case reflect.Slice:
Expand Down Expand Up @@ -428,11 +431,11 @@
}
case Float32Kind:
if lvl != 0 {
tv.SetFloat32(float32(rv.Float()))
tv.SetFloat32(softfloat.F64to32(math.Float64bits(rv.Float())))

Check warning on line 434 in gnovm/pkg/gnolang/gonative.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/gonative.go#L434

Added line #L434 was not covered by tests
}
case Float64Kind:
if lvl != 0 {
tv.SetFloat64(rv.Float())
tv.SetFloat64(math.Float64bits(rv.Float()))

Check warning on line 438 in gnovm/pkg/gnolang/gonative.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/gonative.go#L438

Added line #L438 was not covered by tests
}
case BigintKind:
panic("not yet implemented")
Expand Down Expand Up @@ -644,9 +647,9 @@
case reflect.Uint64:
tv.SetUint64(rv.Uint())
case reflect.Float32:
tv.SetFloat32(float32(rv.Float()))
tv.SetFloat32(softfloat.F64to32(math.Float64bits(rv.Float())))
case reflect.Float64:
tv.SetFloat64(rv.Float())
tv.SetFloat64(math.Float64bits(rv.Float()))
case reflect.Array:
rvl := rv.Len()
if rv.Type().Elem().Kind() == reflect.Uint8 {
Expand Down Expand Up @@ -1049,9 +1052,9 @@
case Uint64Type:
rv.SetUint(tv.GetUint64())
case Float32Type:
rv.SetFloat(float64(tv.GetFloat32()))
rv.SetFloat(math.Float64frombits(softfloat.F32to64(tv.GetFloat32())))
case Float64Type:
rv.SetFloat(tv.GetFloat64())
rv.SetFloat(math.Float64frombits(tv.GetFloat64()))
default:
panic(fmt.Sprintf(
"unexpected type %s",
Expand Down
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
Loading
Loading