github.com/primecitizens/pcz/std@v0.2.1/encoding/binary/types.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 4 package binary 5 6 import ( 7 "github.com/primecitizens/pcz/std/core/arch" 8 "github.com/primecitizens/pcz/std/core/bits" 9 ) 10 11 type ( 12 U16le[T ~uint16] uint16 13 U32le[T ~uint32] uint32 14 U64le[T ~uint64] uint64 15 16 I16le[T ~uint16] uint16 17 I32le[T ~uint32] uint32 18 I64le[T ~uint64] uint64 19 ) 20 21 func (x U16le[T]) Get() T { 22 if arch.BigEndian { 23 return T(bits.ReverseBytes16(uint16(x))) 24 } 25 26 return T(x) 27 } 28 29 func (x *U16le[T]) Set(v T) { 30 if arch.BigEndian { 31 *x = U16le[T](bits.ReverseBytes16(uint16(v))) 32 } else { 33 *x = U16le[T](v) 34 } 35 } 36 37 func (x I16le[T]) Get() T { 38 if arch.BigEndian { 39 return T(bits.ReverseBytes16(uint16(x))) 40 } 41 42 return T(x) 43 } 44 45 func (x *I16le[T]) Set(v T) { 46 if arch.BigEndian { 47 *x = I16le[T](bits.ReverseBytes16(uint16(v))) 48 } else { 49 *x = I16le[T](v) 50 } 51 } 52 53 func (x U32le[T]) Get() T { 54 if arch.BigEndian { 55 return T(bits.ReverseBytes32(uint32(x))) 56 } 57 58 return T(x) 59 } 60 61 func (x *U32le[T]) Set(v T) { 62 if arch.BigEndian { 63 *x = U32le[T](bits.ReverseBytes32(uint32(v))) 64 } else { 65 *x = U32le[T](v) 66 } 67 } 68 69 func (x I32le[T]) Get() T { 70 if arch.BigEndian { 71 return T(bits.ReverseBytes32(uint32(x))) 72 } 73 74 return T(x) 75 } 76 77 func (x *I32le[T]) Set(v T) { 78 if arch.BigEndian { 79 *x = I32le[T](bits.ReverseBytes32(uint32(v))) 80 } else { 81 *x = I32le[T](v) 82 } 83 } 84 85 func (x U64le[T]) Get() T { 86 if arch.BigEndian { 87 return T(bits.ReverseBytes64(uint64(x))) 88 } 89 90 return T(x) 91 } 92 93 func (x *U64le[T]) Set(v T) { 94 if arch.BigEndian { 95 *x = U64le[T](bits.ReverseBytes64(uint64(v))) 96 } else { 97 *x = U64le[T](v) 98 } 99 } 100 101 func (x I64le[T]) Get() T { 102 if arch.BigEndian { 103 return T(bits.ReverseBytes64(uint64(x))) 104 } 105 106 return T(x) 107 } 108 109 func (x *I64le[T]) Set(v T) { 110 if arch.BigEndian { 111 *x = I64le[T](bits.ReverseBytes64(uint64(v))) 112 } else { 113 *x = I64le[T](v) 114 } 115 } 116 117 type ( 118 U16be[T ~uint16] uint16 119 U32be[T ~uint32] uint32 120 U64be[T ~uint64] uint64 121 122 I16be[T ~uint16] uint16 123 I32be[T ~uint32] uint32 124 I64be[T ~uint64] uint64 125 ) 126 127 func (x U16be[T]) Get() T { 128 if arch.LittleEndian { 129 return T(bits.ReverseBytes16(uint16(x))) 130 } 131 132 return T(x) 133 } 134 135 func (x *U16be[T]) Set(v T) { 136 if arch.LittleEndian { 137 *x = U16be[T](bits.ReverseBytes16(uint16(v))) 138 } else { 139 *x = U16be[T](v) 140 } 141 } 142 143 func (x I16be[T]) Get() T { 144 if arch.LittleEndian { 145 return T(bits.ReverseBytes16(uint16(x))) 146 } 147 148 return T(x) 149 } 150 151 func (x *I16be[T]) Set(v T) { 152 if arch.LittleEndian { 153 *x = I16be[T](bits.ReverseBytes16(uint16(v))) 154 } else { 155 *x = I16be[T](v) 156 } 157 } 158 159 func (x U32be[T]) Get() T { 160 if arch.LittleEndian { 161 return T(bits.ReverseBytes32(uint32(x))) 162 } 163 164 return T(x) 165 } 166 167 func (x *U32be[T]) Set(v T) { 168 if arch.LittleEndian { 169 *x = U32be[T](bits.ReverseBytes32(uint32(v))) 170 } else { 171 *x = U32be[T](v) 172 } 173 } 174 175 func (x I32be[T]) Get() T { 176 if arch.LittleEndian { 177 return T(bits.ReverseBytes32(uint32(x))) 178 } 179 180 return T(x) 181 } 182 183 func (x *I32be[T]) Set(v T) { 184 if arch.LittleEndian { 185 *x = I32be[T](bits.ReverseBytes32(uint32(v))) 186 } else { 187 *x = I32be[T](v) 188 } 189 } 190 191 func (x U64be[T]) Get() T { 192 if arch.LittleEndian { 193 return T(bits.ReverseBytes64(uint64(x))) 194 } 195 196 return T(x) 197 } 198 199 func (x *U64be[T]) Set(v T) { 200 if arch.LittleEndian { 201 *x = U64be[T](bits.ReverseBytes64(uint64(v))) 202 } else { 203 *x = U64be[T](v) 204 } 205 } 206 207 func (x I64be[T]) Get() T { 208 if arch.LittleEndian { 209 return T(bits.ReverseBytes64(uint64(x))) 210 } 211 212 return T(x) 213 } 214 215 func (x *I64be[T]) Set(v T) { 216 if arch.LittleEndian { 217 *x = I64be[T](bits.ReverseBytes64(uint64(v))) 218 } else { 219 *x = I64be[T](v) 220 } 221 }