github.com/moreal/bencodex-go@v0.0.0-20231021172012-18277a477d15/internal/encoder/int.go (about)

     1  package encoder
     2  
     3  import "unsafe"
     4  
     5  const intMask uint64 = 1<<(32<<(^uint(0)>>63)) - 1
     6  
     7  var intLookup [100]uint16
     8  
     9  // "00010203...96979899" cast to []uint16
    10  var intLELookup = [100]uint16{
    11  	0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530, 0x3630, 0x3730, 0x3830, 0x3930,
    12  	0x3031, 0x3131, 0x3231, 0x3331, 0x3431, 0x3531, 0x3631, 0x3731, 0x3831, 0x3931,
    13  	0x3032, 0x3132, 0x3232, 0x3332, 0x3432, 0x3532, 0x3632, 0x3732, 0x3832, 0x3932,
    14  	0x3033, 0x3133, 0x3233, 0x3333, 0x3433, 0x3533, 0x3633, 0x3733, 0x3833, 0x3933,
    15  	0x3034, 0x3134, 0x3234, 0x3334, 0x3434, 0x3534, 0x3634, 0x3734, 0x3834, 0x3934,
    16  	0x3035, 0x3135, 0x3235, 0x3335, 0x3435, 0x3535, 0x3635, 0x3735, 0x3835, 0x3935,
    17  	0x3036, 0x3136, 0x3236, 0x3336, 0x3436, 0x3536, 0x3636, 0x3736, 0x3836, 0x3936,
    18  	0x3037, 0x3137, 0x3237, 0x3337, 0x3437, 0x3537, 0x3637, 0x3737, 0x3837, 0x3937,
    19  	0x3038, 0x3138, 0x3238, 0x3338, 0x3438, 0x3538, 0x3638, 0x3738, 0x3838, 0x3938,
    20  	0x3039, 0x3139, 0x3239, 0x3339, 0x3439, 0x3539, 0x3639, 0x3739, 0x3839, 0x3939,
    21  }
    22  
    23  func init() {
    24  	var b [2]byte
    25  	*(*uint16)(unsafe.Pointer(&b)) = uint16(0xABCD)
    26  	switch b[0] {
    27  	case 0xCD:
    28  		intLookup = intLELookup
    29  	case 0xAB:
    30  		intLookup = [100]uint16{
    31  			0x3030, 0x3031, 0x3032, 0x3033, 0x3034, 0x3035, 0x3036, 0x3037, 0x3038, 0x3039,
    32  			0x3130, 0x3131, 0x3132, 0x3133, 0x3134, 0x3135, 0x3136, 0x3137, 0x3138, 0x3139,
    33  			0x3230, 0x3231, 0x3232, 0x3233, 0x3234, 0x3235, 0x3236, 0x3237, 0x3238, 0x3239,
    34  			0x3330, 0x3331, 0x3332, 0x3333, 0x3334, 0x3335, 0x3336, 0x3337, 0x3338, 0x3339,
    35  			0x3430, 0x3431, 0x3432, 0x3433, 0x3434, 0x3435, 0x3436, 0x3437, 0x3438, 0x3439,
    36  			0x3530, 0x3531, 0x3532, 0x3533, 0x3534, 0x3535, 0x3536, 0x3537, 0x3538, 0x3539,
    37  			0x3630, 0x3631, 0x3632, 0x3633, 0x3634, 0x3635, 0x3636, 0x3637, 0x3638, 0x3639,
    38  			0x3730, 0x3731, 0x3732, 0x3733, 0x3734, 0x3735, 0x3736, 0x3737, 0x3738, 0x3739,
    39  			0x3830, 0x3831, 0x3832, 0x3833, 0x3834, 0x3835, 0x3836, 0x3837, 0x3838, 0x3839,
    40  			0x3930, 0x3931, 0x3932, 0x3933, 0x3934, 0x3935, 0x3936, 0x3937, 0x3938, 0x3939,
    41  		}
    42  	default:
    43  		panic("could not determine endianness")
    44  	}
    45  }
    46  
    47  //go:nosplit
    48  func (e *Encoder) writeInt(data int64) {
    49  	u64 := uint64(data)
    50  	n := u64 & intMask
    51  	negative := data < 0
    52  	if !negative {
    53  		if n < 10 {
    54  			e.writeByte(byte(n + '0'))
    55  			return
    56  		} else if n < 100 {
    57  			memmove(
    58  				unsafe.Pointer(uintptr((*sliceHeader)(unsafe.Pointer(&e.buffer)).data)+uintptr(e.offset)),
    59  				unsafe.Pointer(&intLELookup[n]),
    60  				2,
    61  			)
    62  			e.offset += 2
    63  			return
    64  		}
    65  	} else {
    66  		n = -n & intMask
    67  	}
    68  	var b [22]byte
    69  	u := (*[11]uint16)(unsafe.Pointer(&b))
    70  	i := 11
    71  	for n >= 100 {
    72  		j := n % 100
    73  		n /= 100
    74  		i--
    75  		u[i] = intLookup[j]
    76  	}
    77  	i--
    78  	u[i] = intLookup[n]
    79  	i *= 2 // convert to byte index
    80  	if n < 10 {
    81  		i++ // remove leading zero
    82  	}
    83  	if negative {
    84  		i--
    85  		b[i] = '-'
    86  	}
    87  	e.write(b[i:])
    88  }
    89  
    90  //go:nosplit
    91  func (e *Encoder) encodeInt(data int64) {
    92  	e.grow(24)
    93  	e.writeByte('i')
    94  	e.writeInt(data)
    95  	e.writeByte('e')
    96  }