Added generic types to sizeof
This commit is contained in:
parent
01342f5318
commit
91bafc0867
@ -16,7 +16,7 @@ restart:
|
||||
for i, pointer := range c.codePointers {
|
||||
address := pointer.Resolve()
|
||||
|
||||
if sizeof.Signed(int64(address)) > int(pointer.Size) {
|
||||
if sizeof.Signed(address) > int(pointer.Size) {
|
||||
left := c.code[:pointer.Position-Address(pointer.OpSize)]
|
||||
right := c.code[pointer.Position+Address(pointer.Size):]
|
||||
size := pointer.Size + pointer.OpSize
|
||||
|
@ -16,7 +16,7 @@ func (f *Machine) RegisterNumber(mnemonic asm.Mnemonic, a cpu.Register, b int) {
|
||||
}
|
||||
|
||||
// If the number only needs 32 bits, we can encode the instruction.
|
||||
if sizeof.Signed(int64(b)) <= 4 {
|
||||
if sizeof.Signed(b) <= 4 {
|
||||
f.Assembler.RegisterNumber(mnemonic, a, b)
|
||||
f.postInstruction()
|
||||
return
|
||||
|
@ -3,15 +3,17 @@ package sizeof
|
||||
import "math"
|
||||
|
||||
// Signed tells you how many bytes are needed to encode this signed number.
|
||||
func Signed(number int64) int {
|
||||
func Signed[T int | int8 | int16 | int32 | int64](number T) int {
|
||||
x := int64(number)
|
||||
|
||||
switch {
|
||||
case number >= math.MinInt8 && number <= math.MaxInt8:
|
||||
case x >= math.MinInt8 && x <= math.MaxInt8:
|
||||
return 1
|
||||
|
||||
case number >= math.MinInt16 && number <= math.MaxInt16:
|
||||
case x >= math.MinInt16 && x <= math.MaxInt16:
|
||||
return 2
|
||||
|
||||
case number >= math.MinInt32 && number <= math.MaxInt32:
|
||||
case x >= math.MinInt32 && x <= math.MaxInt32:
|
||||
return 4
|
||||
|
||||
default:
|
||||
|
Loading…
x
Reference in New Issue
Block a user