Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
neoppod
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Levin Zimmermann
neoppod
Commits
d67b644a
Commit
d67b644a
authored
Dec 09, 2016
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
d5fb4ac4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
65 additions
and
13 deletions
+65
-13
t/neo/packed.go
t/neo/packed.go
+65
-13
No files found.
t/neo/
byteorder
.go
→
t/neo/
packed
.go
View file @
d67b644a
...
...
@@ -10,7 +10,7 @@
//
// See COPYING file for full licensing terms.
//
NEO | Bigendian/native conversion
//
Types to use in packed structures
package
neo
...
...
@@ -19,42 +19,85 @@ import (
"unsafe"
)
type
be16
uint16
type
be32
uint32
type
be64
uint64
// XXX temp
type
xbe16
uint16
type
xbe32
uint32
type
xbe64
uint64
// uintX has alignment requirement =X; [X]byte has alignment requirement 1.
// That's why we can use [X]byte and this way keep a struct packed, even if Go
// does not support packed structs in general.
type
be16
[
2
]
byte
type
be32
[
4
]
byte
type
be64
[
8
]
byte
// XXX this way compiler does not preclears return
// (on [2]byte it preclears)
// https://github.com/golang/go/issues/15925
type
zbe16
struct
{
b0
,
b1
byte
}
// XXX naming ntoh{s,l,q} ?
func
ntoh16
(
v
be16
)
uint16
{
//b := (*[2]byte)(unsafe.Pointer(&v))
return
binary
.
BigEndian
.
Uint16
(
v
[
:
])
}
func
ntoh16_z
(
v
zbe16
)
uint16
{
b
:=
(
*
[
2
]
byte
)(
unsafe
.
Pointer
(
&
v
))
return
binary
.
BigEndian
.
Uint16
(
b
[
:
])
}
func
hton16_z
(
v
uint16
)
zbe16
{
return
zbe16
{
byte
(
v
>>
8
),
byte
(
v
)}
}
/* NOTE ^^^ is as efficient
func ntoh16_2(v be16) uint16 {
b := (*[2]byte)(unsafe.Pointer(&v))
return uint16(
b[1]) | uint16(b
[0])<<8
//
b := (*[2]byte)(unsafe.Pointer(&v))
return uint16(
v[1]) | uint16(v
[0])<<8
}
*/
/* FIXME compiler emits instruction to pre-clear r, probably because of &r
func
hton16_1
(
v
uint16
)
(
r
be16
)
{
r
[
0
]
=
byte
(
v
>>
8
)
r
[
1
]
=
byte
(
v
)
return
r
//return [2]byte{byte(v >> 8), byte(v)}
}
/* FIXME compiler emits instruction to pre-clear r, probably because of &r */
func
hton16_2
(
v
uint16
)
(
r
be16
)
{
b := (*[2]byte)(unsafe.Pointer(&r))
binary.BigEndian.PutUint16(
b
[:], v)
//
b := (*[2]byte)(unsafe.Pointer(&r))
binary
.
BigEndian
.
PutUint16
(
r
[
:
],
v
)
return
r
}
*/
// NOTE here we are leveraging BigEndian.Uint16**2 = identity
func
hton16
(
v
uint16
)
be16
{
func
hton16_3x
(
v
uint16
)
xbe16
{
return
*
(
*
xbe16
)(
unsafe
.
Pointer
(
&
v
))
//b := (*be16)(unsafe.Pointer(&v))
//return *b
}
func
hton16_3
(
v
uint16
)
be16
{
return
be16
{
4
,
5
}
//return *(*be16)(unsafe.Pointer(&v))
//b := (*be16)(unsafe.Pointer(&v))
//return *b
}
// NOTE here we are leveraging BigEndian.Uint16^2 = identity
func
hton16
(
v
uint16
)
xbe16
{
// FIXME just doing
// return be16(ntoh16(be16(v)))
// emits more prologue/epilogue
b
:=
(
*
[
2
]
byte
)(
unsafe
.
Pointer
(
&
v
))
return
be16
(
binary
.
BigEndian
.
Uint16
(
b
[
:
])
)
return
x
be16
(
binary
.
BigEndian
.
Uint16
(
b
[
:
])
)
}
/*
func ntoh32(v be32) uint32 {
b := (*[4]byte)(unsafe.Pointer(&v))
return binary.BigEndian.Uint32(b[:])
...
...
@@ -74,3 +117,12 @@ func hton64(v uint64) be64 {
b := (*[8]byte)(unsafe.Pointer(&v))
return be64( binary.BigEndian.Uint64(b[:]) )
}
*/
type
A
struct
{
z
be16
}
func
zzz
(
a
*
A
,
v
uint16
)
{
a
.
z
=
hton16_1
(
v
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment