| 1 |
package asm |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/binary" |
| 5 |
|
| 6 |
. "github.com/mmcloughlin/avo/build" |
| 7 |
"github.com/mmcloughlin/avo/operand" |
| 8 |
) |
| 9 |
|
| 10 |
func ConstBytes(name string, data []byte) operand.Mem { |
| 11 |
m := GLOBL(name, RODATA|NOPTR) |
| 12 |
|
| 13 |
switch { |
| 14 |
case len(data)%8 == 0: |
| 15 |
constBytes8(0, data) |
| 16 |
|
| 17 |
case len(data)%4 == 0: |
| 18 |
constBytes4(0, data) |
| 19 |
|
| 20 |
default: |
| 21 |
i := (len(data) / 8) * 8 |
| 22 |
constBytes8(0, data[:i]) |
| 23 |
constBytes1(i, data[i:]) |
| 24 |
} |
| 25 |
|
| 26 |
return m |
| 27 |
} |
| 28 |
|
| 29 |
func ConstArray16(name string, elems ...uint16) operand.Mem { |
| 30 |
data := make([]byte, 2*len(elems)) |
| 31 |
for i, elem := range elems { |
| 32 |
binary.LittleEndian.PutUint16(data[i*2:], elem) |
| 33 |
} |
| 34 |
return ConstBytes(name, data) |
| 35 |
} |
| 36 |
|
| 37 |
func ConstArray32(name string, elems ...uint32) operand.Mem { |
| 38 |
data := make([]byte, 4*len(elems)) |
| 39 |
for i, elem := range elems { |
| 40 |
binary.LittleEndian.PutUint32(data[i*4:], elem) |
| 41 |
} |
| 42 |
return ConstBytes(name, data) |
| 43 |
} |
| 44 |
|
| 45 |
func ConstArray64(name string, elems ...uint64) operand.Mem { |
| 46 |
data := make([]byte, 8*len(elems)) |
| 47 |
for i, elem := range elems { |
| 48 |
binary.LittleEndian.PutUint64(data[i*8:], elem) |
| 49 |
} |
| 50 |
return ConstBytes(name, data) |
| 51 |
} |
| 52 |
|
| 53 |
func ConstShuffleMask32(name string, indices ...uint32) operand.Mem { |
| 54 |
data := make([]byte, 4*len(indices)) |
| 55 |
for i, index := range indices { |
| 56 |
for j := 0; j < 4; j++ { |
| 57 |
data[i*4+j] = byte(index*4 + uint32(j)) |
| 58 |
} |
| 59 |
} |
| 60 |
return ConstBytes(name, data) |
| 61 |
} |
| 62 |
|
| 63 |
func ConstShuffleMask64(name string, indices ...uint64) operand.Mem { |
| 64 |
data := make([]byte, 8*len(indices)) |
| 65 |
for i, index := range indices { |
| 66 |
for j := 0; j < 8; j++ { |
| 67 |
data[i*8+j] = byte(index*8 + uint64(j)) |
| 68 |
} |
| 69 |
} |
| 70 |
return ConstBytes(name, data) |
| 71 |
} |
| 72 |
|
| 73 |
func ConstLoadMask32(name string, indices ...uint32) operand.Mem { |
| 74 |
data := make([]uint32, len(indices)) |
| 75 |
for i, index := range indices { |
| 76 |
data[i] = index << 31 |
| 77 |
} |
| 78 |
return ConstArray32(name, data...) |
| 79 |
} |
| 80 |
|
| 81 |
func ConstLoadMask64(name string, indices ...uint64) operand.Mem { |
| 82 |
data := make([]uint64, len(indices)) |
| 83 |
for i, index := range indices { |
| 84 |
data[i] = index << 63 |
| 85 |
} |
| 86 |
return ConstArray64(name, data...) |
| 87 |
} |
| 88 |
|
| 89 |
func constBytes8(offset int, data []byte) { |
| 90 |
for i := 0; i < len(data); i += 8 { |
| 91 |
DATA(offset+i, operand.U64(binary.LittleEndian.Uint64(data[i:i+8]))) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
func constBytes4(offset int, data []byte) { |
| 96 |
for i := 0; i < len(data); i += 4 { |
| 97 |
DATA(offset+i, operand.U32(binary.LittleEndian.Uint32(data[i:i+4]))) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func constBytes1(offset int, data []byte) { |
| 102 |
for i, b := range data { |
| 103 |
DATA(offset+i, operand.U8(b)) |
| 104 |
} |
| 105 |
} |