detached 63c6fec

purego: fix float returns (#177)

The bits for a float32 return value were being converted into a float64. This causes the returned value to be incorrect. This fix was to separate the float32 case and use math.Float32frombits. The test just makes sure that float32 and float64 return values are returned from C functions correctly.
commit
63c6fec935c5d014be719ef339e930658eb39213
author
TotallyGamerJet · · 2023-11-18 17:14 UTC
committer
GitHub · · 2023-11-18 17:14 UTC
parent
cb05b2a
signature
GPG Unverified4AEE18F83AFDEB23 The key is unknown
2 files · +44 -5
M func.go +5 -1
@@ -272,7 +272,11 @@ func RegisterFunc(fptr interface{}, cfn uintptr) {
272272 RegisterFunc(v.Interface(), r1)
273273 case reflect.String:
274274 v.SetString(strings.GoString(r1))
275- case reflect.Float32, reflect.Float64:
275+ case reflect.Float32:
276+ // NOTE: r2 is only the floating return value on 64bit platforms.
277+ // On 32bit platforms r2 is the upper part of a 64bit return.
278+ v.SetFloat(float64(math.Float32frombits(uint32(r2))))
279+ case reflect.Float64:
276280 // NOTE: r2 is only the floating return value on 64bit platforms.
277281 // On 32bit platforms r2 is the upper part of a 64bit return.
278282 v.SetFloat(math.Float64frombits(uint64(r2)))
@@ -272,7 +272,11 @@ func RegisterFunc(fptr interface{}, cfn uintptr) {
272 RegisterFunc(v.Interface(), r1)272 RegisterFunc(v.Interface(), r1)
273 case reflect.String:273 case reflect.String:
274 v.SetString(strings.GoString(r1))274 v.SetString(strings.GoString(r1))
275- case reflect.Float32, reflect.Float64:275+ case reflect.Float32:
276+ // NOTE: r2 is only the floating return value on 64bit platforms.
277+ // On 32bit platforms r2 is the upper part of a 64bit return.
278+ v.SetFloat(float64(math.Float32frombits(uint32(r2))))
279+ case reflect.Float64:
276 // NOTE: r2 is only the floating return value on 64bit platforms.280 // NOTE: r2 is only the floating return value on 64bit platforms.
277 // On 32bit platforms r2 is the upper part of a 64bit return.281 // On 32bit platforms r2 is the upper part of a 64bit return.
278 v.SetFloat(math.Float64frombits(uint64(r2)))282 v.SetFloat(math.Float64frombits(uint64(r2)))
M func_test.go +39 -4
@@ -35,11 +35,11 @@ func getSystemLibrary() (string, error) {
3535 func TestRegisterFunc(t *testing.T) {
3636 library, err := getSystemLibrary()
3737 if err != nil {
38- t.Errorf("couldn't get system library: %s", err)
38+ t.Fatalf("couldn't get system library: %s", err)
3939 }
4040 libc, err := openLibrary(library)
4141 if err != nil {
42- t.Errorf("failed to dlopen: %s", err)
42+ t.Fatalf("failed to dlopen: %s", err)
4343 }
4444 var puts func(string)
4545 purego.RegisterLibFunc(&puts, libc, "puts")
@@ -65,11 +65,11 @@ func ExampleNewCallback() {
6565 func Test_qsort(t *testing.T) {
6666 library, err := getSystemLibrary()
6767 if err != nil {
68- t.Errorf("couldn't get system library: %s", err)
68+ t.Fatalf("couldn't get system library: %s", err)
6969 }
7070 libc, err := openLibrary(library)
7171 if err != nil {
72- t.Errorf("failed to dlopen: %s", err)
72+ t.Fatalf("failed to dlopen: %s", err)
7373 }
7474
7575 data := []int{88, 56, 100, 2, 25}
@@ -86,3 +86,38 @@ func Test_qsort(t *testing.T) {
8686 }
8787 }
8888 }
89+
90+func TestRegisterFunc_Floats(t *testing.T) {
91+ library, err := getSystemLibrary()
92+ if err != nil {
93+ t.Fatalf("couldn't get system library: %s", err)
94+ }
95+ libc, err := openLibrary(library)
96+ if err != nil {
97+ t.Fatalf("failed to dlopen: %s", err)
98+ }
99+ {
100+ var strtof func(arg string) float32
101+ purego.RegisterLibFunc(&strtof, libc, "strtof")
102+ const (
103+ arg = "2"
104+ )
105+ got := strtof(arg)
106+ expected := float32(2)
107+ if got != expected {
108+ t.Errorf("strtof failed. got %f but wanted %f", got, expected)
109+ }
110+ }
111+ {
112+ var strtod func(arg string, ptr **byte) float64
113+ purego.RegisterLibFunc(&strtod, libc, "strtod")
114+ const (
115+ arg = "1"
116+ )
117+ got := strtod(arg, nil)
118+ expected := float64(1)
119+ if got != expected {
120+ t.Errorf("strtod failed. got %f but wanted %f", got, expected)
121+ }
122+ }
123+}
@@ -35,11 +35,11 @@ func getSystemLibrary() (string, error) {
35 func TestRegisterFunc(t *testing.T) {35 func TestRegisterFunc(t *testing.T) {
36 library, err := getSystemLibrary()36 library, err := getSystemLibrary()
37 if err != nil {37 if err != nil {
38- t.Errorf("couldn't get system library: %s", err)38+ t.Fatalf("couldn't get system library: %s", err)
39 }39 }
40 libc, err := openLibrary(library)40 libc, err := openLibrary(library)
41 if err != nil {41 if err != nil {
42- t.Errorf("failed to dlopen: %s", err)42+ t.Fatalf("failed to dlopen: %s", err)
43 }43 }
44 var puts func(string)44 var puts func(string)
45 purego.RegisterLibFunc(&puts, libc, "puts")45 purego.RegisterLibFunc(&puts, libc, "puts")
@@ -65,11 +65,11 @@ func ExampleNewCallback() {
65 func Test_qsort(t *testing.T) {65 func Test_qsort(t *testing.T) {
66 library, err := getSystemLibrary()66 library, err := getSystemLibrary()
67 if err != nil {67 if err != nil {
68- t.Errorf("couldn't get system library: %s", err)68+ t.Fatalf("couldn't get system library: %s", err)
69 }69 }
70 libc, err := openLibrary(library)70 libc, err := openLibrary(library)
71 if err != nil {71 if err != nil {
72- t.Errorf("failed to dlopen: %s", err)72+ t.Fatalf("failed to dlopen: %s", err)
73 }73 }
74 74
75 data := []int{88, 56, 100, 2, 25}75 data := []int{88, 56, 100, 2, 25}
@@ -86,3 +86,38 @@ func Test_qsort(t *testing.T) {
86 }86 }
87 }87 }
88 }88 }
89+
90+func TestRegisterFunc_Floats(t *testing.T) {
91+ library, err := getSystemLibrary()
92+ if err != nil {
93+ t.Fatalf("couldn't get system library: %s", err)
94+ }
95+ libc, err := openLibrary(library)
96+ if err != nil {
97+ t.Fatalf("failed to dlopen: %s", err)
98+ }
99+ {
100+ var strtof func(arg string) float32
101+ purego.RegisterLibFunc(&strtof, libc, "strtof")
102+ const (
103+ arg = "2"
104+ )
105+ got := strtof(arg)
106+ expected := float32(2)
107+ if got != expected {
108+ t.Errorf("strtof failed. got %f but wanted %f", got, expected)
109+ }
110+ }
111+ {
112+ var strtod func(arg string, ptr **byte) float64
113+ purego.RegisterLibFunc(&strtod, libc, "strtod")
114+ const (
115+ arg = "1"
116+ )
117+ got := strtod(arg, nil)
118+ expected := float64(1)
119+ if got != expected {
120+ t.Errorf("strtod failed. got %f but wanted %f", got, expected)
121+ }
122+ }
123+}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 102

↑↓ moveenter openesc close