| 1 |
package x86 |
| 2 |
|
| 3 |
import ( |
| 4 |
. "github.com/mmcloughlin/avo/build" |
| 5 |
. "github.com/mmcloughlin/avo/operand" |
| 6 |
|
| 7 |
"math" |
| 8 |
"math/bits" |
| 9 |
|
| 10 |
"github.com/segmentio/asm/cpu" |
| 11 |
) |
| 12 |
|
| 13 |
// JumpIfFeature constructs a jump sequence that tests for one or more feature flags. |
| 14 |
// If all flags are matched, jump to the target label. |
| 15 |
func JumpIfFeature(jmp string, f cpu.X86Feature) { |
| 16 |
jump(LabelRef(jmp), f, false) |
| 17 |
} |
| 18 |
|
| 19 |
// JumpUnlessFeature constructs a jump sequence that tests for one or more feature flags. |
| 20 |
// Unless all flags are matched, jump to the target label. |
| 21 |
func JumpUnlessFeature(jmp string, f cpu.X86Feature) { |
| 22 |
jump(LabelRef(jmp), f, true) |
| 23 |
} |
| 24 |
|
| 25 |
// cpuAddr is a Mem operand containing the global symbolic reference to the |
| 26 |
// X86 cpu feature flags. |
| 27 |
var cpuAddr = NewDataAddr(Symbol{Name: "github·com∕segmentio∕asm∕cpu·X86"}, 0) |
| 28 |
|
| 29 |
func jump(jmp Op, f cpu.X86Feature, invert bool) { |
| 30 |
if bits.OnesCount64(uint64(f)) == 1 { |
| 31 |
// If the feature test is for a single flag, optimize the test using BTQ |
| 32 |
jumpSingleFlag(jmp, f, invert) |
| 33 |
} else { |
| 34 |
jumpMultiFlag(jmp, f, invert) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
func jumpSingleFlag(jmp Op, f cpu.X86Feature, invert bool) { |
| 39 |
bit := U8(bits.TrailingZeros64(uint64(f))) |
| 40 |
|
| 41 |
// Likely only need lower 4 bytes |
| 42 |
if bit < 32 { |
| 43 |
BTL(bit, cpuAddr) |
| 44 |
} else { |
| 45 |
BTQ(bit, cpuAddr) |
| 46 |
} |
| 47 |
|
| 48 |
if invert { |
| 49 |
JCC(jmp) |
| 50 |
} else { |
| 51 |
JCS(jmp) |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func jumpMultiFlag(jmp Op, f cpu.X86Feature, invert bool) { |
| 56 |
r := GP64() |
| 57 |
MOVQ(cpuAddr, r) |
| 58 |
|
| 59 |
var op Op |
| 60 |
if f <= math.MaxUint32 { |
| 61 |
op = U32(f) |
| 62 |
} else { |
| 63 |
op = GP64() |
| 64 |
MOVQ(U64(f), op) |
| 65 |
} |
| 66 |
|
| 67 |
ANDQ(op, r) |
| 68 |
CMPQ(r, op) |
| 69 |
|
| 70 |
if invert { |
| 71 |
JNE(jmp) |
| 72 |
} else { |
| 73 |
JEQ(jmp) |
| 74 |
} |
| 75 |
} |