Added generic types to sizeof

This commit is contained in:
Eduard Urbach 2025-02-08 14:44:13 +01:00
parent 01342f5318
commit 91bafc0867
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
3 changed files with 8 additions and 6 deletions

View File

@ -16,7 +16,7 @@ restart:
for i, pointer := range c.codePointers { for i, pointer := range c.codePointers {
address := pointer.Resolve() 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)] left := c.code[:pointer.Position-Address(pointer.OpSize)]
right := c.code[pointer.Position+Address(pointer.Size):] right := c.code[pointer.Position+Address(pointer.Size):]
size := pointer.Size + pointer.OpSize size := pointer.Size + pointer.OpSize

View File

@ -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 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.Assembler.RegisterNumber(mnemonic, a, b)
f.postInstruction() f.postInstruction()
return return

View File

@ -3,15 +3,17 @@ package sizeof
import "math" import "math"
// Signed tells you how many bytes are needed to encode this signed number. // 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 { switch {
case number >= math.MinInt8 && number <= math.MaxInt8: case x >= math.MinInt8 && x <= math.MaxInt8:
return 1 return 1
case number >= math.MinInt16 && number <= math.MaxInt16: case x >= math.MinInt16 && x <= math.MaxInt16:
return 2 return 2
case number >= math.MinInt32 && number <= math.MaxInt32: case x >= math.MinInt32 && x <= math.MaxInt32:
return 4 return 4
default: default: