detached f719fc5

add Int support to amd64 and fix issue with structs not being copied (#288)

Closes #289
commit
f719fc513de52b2bcaf7ff33af86858fb7d8756e
author
TotallyGamerJet · · 2024-10-19 14:18 UTC
committer
Hajime Hoshi · · 2024-10-19 14:18 UTC
parent
ed3ae3d
signature
unsigned
refs
tag: v0.8.1
3 files · +50 -16
M struct_amd64.go +4 -16
@@ -111,7 +111,7 @@ func addStruct(v reflect.Value, numInts, numFloats, numStack *int, addInt, addFl
111111 return keepAlive
112112 }
113113
114-func postMerger(t reflect.Type) bool {
114+func postMerger(t reflect.Type) (passInMemory bool) {
115115 // (c) If the size of the aggregate exceeds two eightbytes and the first eight- byte isn’t SSE or any other
116116 // eightbyte isn’t SSEUP, the whole argument is passed in memory.
117117 if t.Kind() != reflect.Struct {
@@ -120,19 +120,7 @@ func postMerger(t reflect.Type) bool {
120120 if t.Size() <= 2*8 {
121121 return false
122122 }
123- first := getFirst(t).Kind()
124- if first != reflect.Float32 && first != reflect.Float64 {
125- return false
126- }
127- return true
128-}
129-
130-func getFirst(t reflect.Type) reflect.Type {
131- first := t.Field(0).Type
132- if first.Kind() == reflect.Struct {
133- return getFirst(first)
134- }
135- return first
123+ return true // Go does not have an SSE/SEEUP type so this is always true
136124 }
137125
138126 func tryPlaceRegister(v reflect.Value, addFloat func(uintptr), addInt func(uintptr)) (ok bool) {
@@ -196,7 +184,7 @@ func tryPlaceRegister(v reflect.Value, addFloat func(uintptr), addInt func(uintp
196184 val |= uint64(f.Int()&0xFFFF_FFFF) << shift
197185 shift += 32
198186 class |= _INTEGER
199- case reflect.Int64:
187+ case reflect.Int64, reflect.Int:
200188 val = uint64(f.Int())
201189 shift = 64
202190 class = _INTEGER
@@ -212,7 +200,7 @@ func tryPlaceRegister(v reflect.Value, addFloat func(uintptr), addInt func(uintp
212200 val |= f.Uint() << shift
213201 shift += 32
214202 class |= _INTEGER
215- case reflect.Uint64:
203+ case reflect.Uint64, reflect.Uint:
216204 val = f.Uint()
217205 shift = 64
218206 class = _INTEGER
@@ -111,7 +111,7 @@ func addStruct(v reflect.Value, numInts, numFloats, numStack *int, addInt, addFl
111 return keepAlive111 return keepAlive
112 }112 }
113 113
114-func postMerger(t reflect.Type) bool {114+func postMerger(t reflect.Type) (passInMemory bool) {
115 // (c) If the size of the aggregate exceeds two eightbytes and the first eight- byte isn’t SSE or any other115 // (c) If the size of the aggregate exceeds two eightbytes and the first eight- byte isn’t SSE or any other
116 // eightbyte isn’t SSEUP, the whole argument is passed in memory.116 // eightbyte isn’t SSEUP, the whole argument is passed in memory.
117 if t.Kind() != reflect.Struct {117 if t.Kind() != reflect.Struct {
@@ -120,19 +120,7 @@ func postMerger(t reflect.Type) bool {
120 if t.Size() <= 2*8 {120 if t.Size() <= 2*8 {
121 return false121 return false
122 }122 }
123- first := getFirst(t).Kind()123+ return true // Go does not have an SSE/SEEUP type so this is always true
124- if first != reflect.Float32 && first != reflect.Float64 {
125- return false
126- }
127- return true
128-}
129-
130-func getFirst(t reflect.Type) reflect.Type {
131- first := t.Field(0).Type
132- if first.Kind() == reflect.Struct {
133- return getFirst(first)
134- }
135- return first
136 }124 }
137 125
138 func tryPlaceRegister(v reflect.Value, addFloat func(uintptr), addInt func(uintptr)) (ok bool) {126 func tryPlaceRegister(v reflect.Value, addFloat func(uintptr), addInt func(uintptr)) (ok bool) {
@@ -196,7 +184,7 @@ func tryPlaceRegister(v reflect.Value, addFloat func(uintptr), addInt func(uintp
196 val |= uint64(f.Int()&0xFFFF_FFFF) << shift184 val |= uint64(f.Int()&0xFFFF_FFFF) << shift
197 shift += 32185 shift += 32
198 class |= _INTEGER186 class |= _INTEGER
199- case reflect.Int64:187+ case reflect.Int64, reflect.Int:
200 val = uint64(f.Int())188 val = uint64(f.Int())
201 shift = 64189 shift = 64
202 class = _INTEGER190 class = _INTEGER
@@ -212,7 +200,7 @@ func tryPlaceRegister(v reflect.Value, addFloat func(uintptr), addInt func(uintp
212 val |= f.Uint() << shift200 val |= f.Uint() << shift
213 shift += 32201 shift += 32
214 class |= _INTEGER202 class |= _INTEGER
215- case reflect.Uint64:203+ case reflect.Uint64, reflect.Uint:
216 val = f.Uint()204 val = f.Uint()
217 shift = 64205 shift = 64
218 class = _INTEGER206 class = _INTEGER
M struct_test.go +23 -0
@@ -6,6 +6,7 @@
66 package purego_test
77
88 import (
9+ "math"
910 "os"
1011 "path/filepath"
1112 "runtime"
@@ -440,6 +441,28 @@ func TestRegisterFunc_structArgs(t *testing.T) {
440441 t.Fatalf("InitWithContentRect returned %d wanted %#x", ret, expectedUnsigned)
441442 }
442443 }
444+ {
445+ type GoInt4 struct {
446+ A, B, C, D int
447+ }
448+ var GoInt4Fn func(GoInt4) int
449+ purego.RegisterLibFunc(&GoInt4Fn, lib, "GoInt4")
450+ const expected = math.MaxInt - 52 - 3 + 4
451+ if ret := GoInt4Fn(GoInt4{math.MaxInt, -52, 3, 4}); ret != expected {
452+ t.Fatalf("GoInt4Fn returned %d wanted %#x", ret, expected)
453+ }
454+ }
455+ {
456+ type GoUint4 struct {
457+ A, B, C, D uint
458+ }
459+ var GoUint4Fn func(GoUint4) uint
460+ purego.RegisterLibFunc(&GoUint4Fn, lib, "GoUint4")
461+ const expected = 1_000_000 + 53 + 71 + 8
462+ if ret := GoUint4Fn(GoUint4{1_000_000, 53, 71, 8}); ret != expected {
463+ t.Fatalf("GoUint4Fn returned %d wanted %#x", ret, expected)
464+ }
465+ }
443466 }
444467
445468 func TestRegisterFunc_structReturns(t *testing.T) {
@@ -6,6 +6,7 @@
6 package purego_test6 package purego_test
7 7
8 import (8 import (
9+ "math"
9 "os"10 "os"
10 "path/filepath"11 "path/filepath"
11 "runtime"12 "runtime"
@@ -440,6 +441,28 @@ func TestRegisterFunc_structArgs(t *testing.T) {
440 t.Fatalf("InitWithContentRect returned %d wanted %#x", ret, expectedUnsigned)441 t.Fatalf("InitWithContentRect returned %d wanted %#x", ret, expectedUnsigned)
441 }442 }
442 }443 }
444+ {
445+ type GoInt4 struct {
446+ A, B, C, D int
447+ }
448+ var GoInt4Fn func(GoInt4) int
449+ purego.RegisterLibFunc(&GoInt4Fn, lib, "GoInt4")
450+ const expected = math.MaxInt - 52 - 3 + 4
451+ if ret := GoInt4Fn(GoInt4{math.MaxInt, -52, 3, 4}); ret != expected {
452+ t.Fatalf("GoInt4Fn returned %d wanted %#x", ret, expected)
453+ }
454+ }
455+ {
456+ type GoUint4 struct {
457+ A, B, C, D uint
458+ }
459+ var GoUint4Fn func(GoUint4) uint
460+ purego.RegisterLibFunc(&GoUint4Fn, lib, "GoUint4")
461+ const expected = 1_000_000 + 53 + 71 + 8
462+ if ret := GoUint4Fn(GoUint4{1_000_000, 53, 71, 8}); ret != expected {
463+ t.Fatalf("GoUint4Fn returned %d wanted %#x", ret, expected)
464+ }
465+ }
443 }466 }
444 467
445 func TestRegisterFunc_structReturns(t *testing.T) {468 func TestRegisterFunc_structReturns(t *testing.T) {
M testdata/structtest/struct_test.c +23 -0
@@ -1,6 +1,13 @@
11 // SPDX-License-Identifier: Apache-2.0
22 // SPDX-FileCopyrightText: 2024 The Ebitengine Authors
33
4+#include "stdint.h"
5+
6+#if defined(__x86_64__) || defined(__aarch64__)
7+typedef int64_t GoInt;
8+typedef uint64_t GoUint;
9+#endif
10+
411 // Empty is empty
512 struct Empty {};
613
@@ -325,3 +332,19 @@ unsigned long InitWithContentRect(int *win, struct Content c, int style, int bac
325332 return 0xF1A6; // FLAG
326333 return (unsigned long)(c.point.x + c.point.y + c.size.width + c.size.height) / (style - backing);
327334 }
335+
336+struct GoInt4 {
337+ GoInt a, b, c, d;
338+};
339+
340+GoInt GoInt4(struct GoInt4 g) {
341+ return g.a + g.b - g.c + g.d;
342+}
343+
344+struct GoUint4 {
345+ GoUint a, b, c, d;
346+};
347+
348+GoUint GoUint4(struct GoUint4 g) {
349+ return g.a + g.b + g.c + g.d;
350+}
@@ -1,6 +1,13 @@
1 // SPDX-License-Identifier: Apache-2.01 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: 2024 The Ebitengine Authors2 // SPDX-FileCopyrightText: 2024 The Ebitengine Authors
3 3
4+#include "stdint.h"
5+
6+#if defined(__x86_64__) || defined(__aarch64__)
7+typedef int64_t GoInt;
8+typedef uint64_t GoUint;
9+#endif
10+
4 // Empty is empty11 // Empty is empty
5 struct Empty {};12 struct Empty {};
6 13
@@ -325,3 +332,19 @@ unsigned long InitWithContentRect(int *win, struct Content c, int style, int bac
325 return 0xF1A6; // FLAG332 return 0xF1A6; // FLAG
326 return (unsigned long)(c.point.x + c.point.y + c.size.width + c.size.height) / (style - backing);333 return (unsigned long)(c.point.x + c.point.y + c.size.width + c.size.height) / (style - backing);
327 }334 }
335+
336+struct GoInt4 {
337+ GoInt a, b, c, d;
338+};
339+
340+GoInt GoInt4(struct GoInt4 g) {
341+ return g.a + g.b - g.c + g.d;
342+}
343+
344+struct GoUint4 {
345+ GoUint a, b, c, d;
346+};
347+
348+GoUint GoUint4(struct GoUint4 g) {
349+ return g.a + g.b + g.c + g.d;
350+}

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