MCPcopy Create free account
hub / github.com/dolthub/doltgresql / LeftShiftAny

Method LeftShiftAny

postgres/parser/utils/bitarray.go:226–274  ·  view source on GitHub ↗

LeftShiftAny performs a logical left shift, with a possible negative count. The number of bits to shift can be arbitrarily large (i.e. possibly larger than 64 in absolute value).

(n int64)

Source from the content-addressed store, hash-verified

224// The number of bits to shift can be arbitrarily large (i.e. possibly
225// larger than 64 in absolute value).
226func (d BitArray) LeftShiftAny(n int64) BitArray {
227 bitlen := d.BitLen()
228 if n == 0 || bitlen == 0 {
229 // Fast path.
230 return d
231 }
232
233 r := MakeZeroBitArray(bitlen)
234 if (n > 0 && n > int64(bitlen)) || (n < 0 && -n > int64(bitlen)) {
235 // Fast path.
236 return r
237 }
238
239 if n > 0 {
240 // This is a left shift.
241 dstWord := uint(0)
242 srcWord := uint(uint64(n) / numBitsPerWord)
243 srcShift := uint(uint64(n) % numBitsPerWord)
244 for i, j := srcWord, dstWord; i < uint(len(d.words)); i++ {
245 r.words[j] = d.words[i] << srcShift
246 j++
247 }
248 for i, j := srcWord+1, dstWord; i < uint(len(d.words)); i++ {
249 r.words[j] |= d.words[i] >> (numBitsPerWord - srcShift)
250 j++
251 }
252 } else {
253 // A right shift.
254 n = -n
255 srcWord := uint(0)
256 dstWord := uint(uint64(n) / numBitsPerWord)
257 srcShift := uint(uint64(n) % numBitsPerWord)
258 for i, j := srcWord, dstWord; j < uint(len(r.words)); i++ {
259 r.words[j] = d.words[i] >> srcShift
260 j++
261 }
262 for i, j := srcWord, dstWord+1; j < uint(len(r.words)); i++ {
263 r.words[j] |= d.words[i] << (numBitsPerWord - srcShift)
264 j++
265 }
266 // Erase the trailing bits that are not used any more.
267 // See #36606.
268 if len(r.words) > 0 {
269 r.words[len(r.words)-1] &= ^word(0) << (numBitsPerWord - r.lastBitsUsed)
270 }
271 }
272
273 return r
274}
275
276// byteReprs contains the bit representation of the 256 possible
277// groups of 8 bits.

Callers

nothing calls this directly

Calls 2

BitLenMethod · 0.95
MakeZeroBitArrayFunction · 0.85

Tested by

no test coverage detected