...

Source file src/math/bits/bits.go

Documentation: math/bits

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:generate go run make_tables.go
     6  
     7  // Package bits implements bit counting and manipulation
     8  // functions for the predeclared unsigned integer types.
     9  package bits
    10  
    11  import _ "unsafe" // for go:linkname
    12  
    13  const uintSize = 32 << (^uint(0) >> 32 & 1) // 32 or 64
    14  
    15  // UintSize is the size of a uint in bits.
    16  const UintSize = uintSize
    17  
    18  // --- LeadingZeros ---
    19  
    20  // LeadingZeros returns the number of leading zero bits in x; the result is UintSize for x == 0.
    21  func LeadingZeros(x uint) int { return UintSize - Len(x) }
    22  
    23  // LeadingZeros8 returns the number of leading zero bits in x; the result is 8 for x == 0.
    24  func LeadingZeros8(x uint8) int { return 8 - Len8(x) }
    25  
    26  // LeadingZeros16 returns the number of leading zero bits in x; the result is 16 for x == 0.
    27  func LeadingZeros16(x uint16) int { return 16 - Len16(x) }
    28  
    29  // LeadingZeros32 returns the number of leading zero bits in x; the result is 32 for x == 0.
    30  func LeadingZeros32(x uint32) int { return 32 - Len32(x) }
    31  
    32  // LeadingZeros64 returns the number of leading zero bits in x; the result is 64 for x == 0.
    33  func LeadingZeros64(x uint64) int { return 64 - Len64(x) }
    34  
    35  // --- TrailingZeros ---
    36  
    37  // See http://supertech.csail.mit.edu/papers/debruijn.pdf
    38  const deBruijn32 = 0x077CB531
    39  
    40  var deBruijn32tab = [32]byte{
    41  	0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
    42  	31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9,
    43  }
    44  
    45  const deBruijn64 = 0x03f79d71b4ca8b09
    46  
    47  var deBruijn64tab = [64]byte{
    48  	0, 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4,
    49  	62, 47, 59, 36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5,
    50  	63, 55, 48, 27, 60, 41, 37, 16, 46, 35, 44, 21, 52, 32, 23, 11,
    51  	54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6,
    52  }
    53  
    54  // TrailingZeros returns the number of trailing zero bits in x; the result is UintSize for x == 0.
    55  func TrailingZeros(x uint) int {
    56  	if UintSize == 32 {
    57  		return TrailingZeros32(uint32(x))
    58  	}
    59  	return TrailingZeros64(uint64(x))
    60  }
    61  
    62  // TrailingZeros8 returns the number of trailing zero bits in x; the result is 8 for x == 0.
    63  func TrailingZeros8(x uint8) int {
    64  	return int(ntz8tab[x])
    65  }
    66  
    67  // TrailingZeros16 returns the number of trailing zero bits in x; the result is 16 for x == 0.
    68  func TrailingZeros16(x uint16) int {
    69  	if x == 0 {
    70  		return 16
    71  	}
    72  	// see comment in TrailingZeros64
    73  	return int(deBruijn32tab[uint32(x&-x)*deBruijn32>>(32-5)])
    74  }
    75  
    76  // TrailingZeros32 returns the number of trailing zero bits in x; the result is 32 for x == 0.
    77  func TrailingZeros32(x uint32) int {
    78  	if x == 0 {
    79  		return 32
    80  	}
    81  	// see comment in TrailingZeros64
    82  	return int(deBruijn32tab[(x&-x)*deBruijn32>>(32-5)])
    83  }
    84  
    85  // TrailingZeros64 returns the number of trailing zero bits in x; the result is 64 for x == 0.
    86  func TrailingZeros64(x uint64) int {
    87  	if x == 0 {
    88  		return 64
    89  	}
    90  	// If popcount is fast, replace code below with return popcount(^x & (x - 1)).
    91  	//
    92  	// x & -x leaves only the right-most bit set in the word. Let k be the
    93  	// index of that bit. Since only a single bit is set, the value is two
    94  	// to the power of k. Multiplying by a power of two is equivalent to
    95  	// left shifting, in this case by k bits. The de Bruijn (64 bit) constant
    96  	// is such that all six bit, consecutive substrings are distinct.
    97  	// Therefore, if we have a left shifted version of this constant we can
    98  	// find by how many bits it was shifted by looking at which six bit
    99  	// substring ended up at the top of the word.
   100  	// (Knuth, volume 4, section 7.3.1)
   101  	return int(deBruijn64tab[(x&-x)*deBruijn64>>(64-6)])
   102  }
   103  
   104  // --- OnesCount ---
   105  
   106  const m0 = 0x5555555555555555 // 01010101 ...
   107  const m1 = 0x3333333333333333 // 00110011 ...
   108  const m2 = 0x0f0f0f0f0f0f0f0f // 00001111 ...
   109  const m3 = 0x00ff00ff00ff00ff // etc.
   110  const m4 = 0x0000ffff0000ffff
   111  
   112  // OnesCount returns the number of one bits ("population count") in x.
   113  func OnesCount(x uint) int {
   114  	if UintSize == 32 {
   115  		return OnesCount32(uint32(x))
   116  	}
   117  	return OnesCount64(uint64(x))
   118  }
   119  
   120  // OnesCount8 returns the number of one bits ("population count") in x.
   121  func OnesCount8(x uint8) int {
   122  	return int(pop8tab[x])
   123  }
   124  
   125  // OnesCount16 returns the number of one bits ("population count") in x.
   126  func OnesCount16(x uint16) int {
   127  	return int(pop8tab[x>>8] + pop8tab[x&0xff])
   128  }
   129  
   130  // OnesCount32 returns the number of one bits ("population count") in x.
   131  func OnesCount32(x uint32) int {
   132  	return int(pop8tab[x>>24] + pop8tab[x>>16&0xff] + pop8tab[x>>8&0xff] + pop8tab[x&0xff])
   133  }
   134  
   135  // OnesCount64 returns the number of one bits ("population count") in x.
   136  func OnesCount64(x uint64) int {
   137  	// Implementation: Parallel summing of adjacent bits.
   138  	// See "Hacker's Delight", Chap. 5: Counting Bits.
   139  	// The following pattern shows the general approach:
   140  	//
   141  	//   x = x>>1&(m0&m) + x&(m0&m)
   142  	//   x = x>>2&(m1&m) + x&(m1&m)
   143  	//   x = x>>4&(m2&m) + x&(m2&m)
   144  	//   x = x>>8&(m3&m) + x&(m3&m)
   145  	//   x = x>>16&(m4&m) + x&(m4&m)
   146  	//   x = x>>32&(m5&m) + x&(m5&m)
   147  	//   return int(x)
   148  	//
   149  	// Masking (& operations) can be left away when there's no
   150  	// danger that a field's sum will carry over into the next
   151  	// field: Since the result cannot be > 64, 8 bits is enough
   152  	// and we can ignore the masks for the shifts by 8 and up.
   153  	// Per "Hacker's Delight", the first line can be simplified
   154  	// more, but it saves at best one instruction, so we leave
   155  	// it alone for clarity.
   156  	const m = 1<<64 - 1
   157  	x = x>>1&(m0&m) + x&(m0&m)
   158  	x = x>>2&(m1&m) + x&(m1&m)
   159  	x = (x>>4 + x) & (m2 & m)
   160  	x += x >> 8
   161  	x += x >> 16
   162  	x += x >> 32
   163  	return int(x) & (1<<7 - 1)
   164  }
   165  
   166  // --- RotateLeft ---
   167  
   168  // RotateLeft returns the value of x rotated left by (k mod UintSize) bits.
   169  // To rotate x right by k bits, call RotateLeft(x, -k).
   170  func RotateLeft(x uint, k int) uint {
   171  	if UintSize == 32 {
   172  		return uint(RotateLeft32(uint32(x), k))
   173  	}
   174  	return uint(RotateLeft64(uint64(x), k))
   175  }
   176  
   177  // RotateLeft8 returns the value of x rotated left by (k mod 8) bits.
   178  // To rotate x right by k bits, call RotateLeft8(x, -k).
   179  func RotateLeft8(x uint8, k int) uint8 {
   180  	const n = 8
   181  	s := uint(k) & (n - 1)
   182  	return x<<s | x>>(n-s)
   183  }
   184  
   185  // RotateLeft16 returns the value of x rotated left by (k mod 16) bits.
   186  // To rotate x right by k bits, call RotateLeft16(x, -k).
   187  func RotateLeft16(x uint16, k int) uint16 {
   188  	const n = 16
   189  	s := uint(k) & (n - 1)
   190  	return x<<s | x>>(n-s)
   191  }
   192  
   193  // RotateLeft32 returns the value of x rotated left by (k mod 32) bits.
   194  // To rotate x right by k bits, call RotateLeft32(x, -k).
   195  func RotateLeft32(x uint32, k int) uint32 {
   196  	const n = 32
   197  	s := uint(k) & (n - 1)
   198  	return x<<s | x>>(n-s)
   199  }
   200  
   201  // RotateLeft64 returns the value of x rotated left by (k mod 64) bits.
   202  // To rotate x right by k bits, call RotateLeft64(x, -k).
   203  func RotateLeft64(x uint64, k int) uint64 {
   204  	const n = 64
   205  	s := uint(k) & (n - 1)
   206  	return x<<s | x>>(n-s)
   207  }
   208  
   209  // --- Reverse ---
   210  
   211  // Reverse returns the value of x with its bits in reversed order.
   212  func Reverse(x uint) uint {
   213  	if UintSize == 32 {
   214  		return uint(Reverse32(uint32(x)))
   215  	}
   216  	return uint(Reverse64(uint64(x)))
   217  }
   218  
   219  // Reverse8 returns the value of x with its bits in reversed order.
   220  func Reverse8(x uint8) uint8 {
   221  	return rev8tab[x]
   222  }
   223  
   224  // Reverse16 returns the value of x with its bits in reversed order.
   225  func Reverse16(x uint16) uint16 {
   226  	return uint16(rev8tab[x>>8]) | uint16(rev8tab[x&0xff])<<8
   227  }
   228  
   229  // Reverse32 returns the value of x with its bits in reversed order.
   230  func Reverse32(x uint32) uint32 {
   231  	const m = 1<<32 - 1
   232  	x = x>>1&(m0&m) | x&(m0&m)<<1
   233  	x = x>>2&(m1&m) | x&(m1&m)<<2
   234  	x = x>>4&(m2&m) | x&(m2&m)<<4
   235  	x = x>>8&(m3&m) | x&(m3&m)<<8
   236  	return x>>16 | x<<16
   237  }
   238  
   239  // Reverse64 returns the value of x with its bits in reversed order.
   240  func Reverse64(x uint64) uint64 {
   241  	const m = 1<<64 - 1
   242  	x = x>>1&(m0&m) | x&(m0&m)<<1
   243  	x = x>>2&(m1&m) | x&(m1&m)<<2
   244  	x = x>>4&(m2&m) | x&(m2&m)<<4
   245  	x = x>>8&(m3&m) | x&(m3&m)<<8
   246  	x = x>>16&(m4&m) | x&(m4&m)<<16
   247  	return x>>32 | x<<32
   248  }
   249  
   250  // --- ReverseBytes ---
   251  
   252  // ReverseBytes returns the value of x with its bytes in reversed order.
   253  func ReverseBytes(x uint) uint {
   254  	if UintSize == 32 {
   255  		return uint(ReverseBytes32(uint32(x)))
   256  	}
   257  	return uint(ReverseBytes64(uint64(x)))
   258  }
   259  
   260  // ReverseBytes16 returns the value of x with its bytes in reversed order.
   261  func ReverseBytes16(x uint16) uint16 {
   262  	return x>>8 | x<<8
   263  }
   264  
   265  // ReverseBytes32 returns the value of x with its bytes in reversed order.
   266  func ReverseBytes32(x uint32) uint32 {
   267  	const m = 1<<32 - 1
   268  	x = x>>8&(m3&m) | x&(m3&m)<<8
   269  	return x>>16 | x<<16
   270  }
   271  
   272  // ReverseBytes64 returns the value of x with its bytes in reversed order.
   273  func ReverseBytes64(x uint64) uint64 {
   274  	const m = 1<<64 - 1
   275  	x = x>>8&(m3&m) | x&(m3&m)<<8
   276  	x = x>>16&(m4&m) | x&(m4&m)<<16
   277  	return x>>32 | x<<32
   278  }
   279  
   280  // --- Len ---
   281  
   282  // Len returns the minimum number of bits required to represent x; the result is 0 for x == 0.
   283  func Len(x uint) int {
   284  	if UintSize == 32 {
   285  		return Len32(uint32(x))
   286  	}
   287  	return Len64(uint64(x))
   288  }
   289  
   290  // Len8 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
   291  func Len8(x uint8) int {
   292  	return int(len8tab[x])
   293  }
   294  
   295  // Len16 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
   296  func Len16(x uint16) (n int) {
   297  	if x >= 1<<8 {
   298  		x >>= 8
   299  		n = 8
   300  	}
   301  	return n + int(len8tab[x])
   302  }
   303  
   304  // Len32 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
   305  func Len32(x uint32) (n int) {
   306  	if x >= 1<<16 {
   307  		x >>= 16
   308  		n = 16
   309  	}
   310  	if x >= 1<<8 {
   311  		x >>= 8
   312  		n += 8
   313  	}
   314  	return n + int(len8tab[x])
   315  }
   316  
   317  // Len64 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
   318  func Len64(x uint64) (n int) {
   319  	if x >= 1<<32 {
   320  		x >>= 32
   321  		n = 32
   322  	}
   323  	if x >= 1<<16 {
   324  		x >>= 16
   325  		n += 16
   326  	}
   327  	if x >= 1<<8 {
   328  		x >>= 8
   329  		n += 8
   330  	}
   331  	return n + int(len8tab[x])
   332  }
   333  
   334  // --- Add with carry ---
   335  
   336  // Add returns the sum with carry of x, y and carry: sum = x + y + carry.
   337  // The carry input must be 0 or 1; otherwise the behavior is undefined.
   338  // The carryOut output is guaranteed to be 0 or 1.
   339  func Add(x, y, carry uint) (sum, carryOut uint) {
   340  	yc := y + carry
   341  	sum = x + yc
   342  	if sum < x || yc < y {
   343  		carryOut = 1
   344  	}
   345  	return
   346  }
   347  
   348  // Add32 returns the sum with carry of x, y and carry: sum = x + y + carry.
   349  // The carry input must be 0 or 1; otherwise the behavior is undefined.
   350  // The carryOut output is guaranteed to be 0 or 1.
   351  func Add32(x, y, carry uint32) (sum, carryOut uint32) {
   352  	yc := y + carry
   353  	sum = x + yc
   354  	if sum < x || yc < y {
   355  		carryOut = 1
   356  	}
   357  	return
   358  }
   359  
   360  // Add64 returns the sum with carry of x, y and carry: sum = x + y + carry.
   361  // The carry input must be 0 or 1; otherwise the behavior is undefined.
   362  // The carryOut output is guaranteed to be 0 or 1.
   363  func Add64(x, y, carry uint64) (sum, carryOut uint64) {
   364  	yc := y + carry
   365  	sum = x + yc
   366  	if sum < x || yc < y {
   367  		carryOut = 1
   368  	}
   369  	return
   370  }
   371  
   372  // --- Subtract with borrow ---
   373  
   374  // Sub returns the difference of x, y and borrow: diff = x - y - borrow.
   375  // The borrow input must be 0 or 1; otherwise the behavior is undefined.
   376  // The borrowOut output is guaranteed to be 0 or 1.
   377  func Sub(x, y, borrow uint) (diff, borrowOut uint) {
   378  	yb := y + borrow
   379  	diff = x - yb
   380  	if diff > x || yb < y {
   381  		borrowOut = 1
   382  	}
   383  	return
   384  }
   385  
   386  // Sub32 returns the difference of x, y and borrow, diff = x - y - borrow.
   387  // The borrow input must be 0 or 1; otherwise the behavior is undefined.
   388  // The borrowOut output is guaranteed to be 0 or 1.
   389  func Sub32(x, y, borrow uint32) (diff, borrowOut uint32) {
   390  	yb := y + borrow
   391  	diff = x - yb
   392  	if diff > x || yb < y {
   393  		borrowOut = 1
   394  	}
   395  	return
   396  }
   397  
   398  // Sub64 returns the difference of x, y and borrow: diff = x - y - borrow.
   399  // The borrow input must be 0 or 1; otherwise the behavior is undefined.
   400  // The borrowOut output is guaranteed to be 0 or 1.
   401  func Sub64(x, y, borrow uint64) (diff, borrowOut uint64) {
   402  	yb := y + borrow
   403  	diff = x - yb
   404  	if diff > x || yb < y {
   405  		borrowOut = 1
   406  	}
   407  	return
   408  }
   409  
   410  // --- Full-width multiply ---
   411  
   412  // Mul returns the full-width product of x and y: (hi, lo) = x * y
   413  // with the product bits' upper half returned in hi and the lower
   414  // half returned in lo.
   415  func Mul(x, y uint) (hi, lo uint) {
   416  	if UintSize == 32 {
   417  		h, l := Mul32(uint32(x), uint32(y))
   418  		return uint(h), uint(l)
   419  	}
   420  	h, l := Mul64(uint64(x), uint64(y))
   421  	return uint(h), uint(l)
   422  }
   423  
   424  // Mul32 returns the 64-bit product of x and y: (hi, lo) = x * y
   425  // with the product bits' upper half returned in hi and the lower
   426  // half returned in lo.
   427  func Mul32(x, y uint32) (hi, lo uint32) {
   428  	tmp := uint64(x) * uint64(y)
   429  	hi, lo = uint32(tmp>>32), uint32(tmp)
   430  	return
   431  }
   432  
   433  // Mul64 returns the 128-bit product of x and y: (hi, lo) = x * y
   434  // with the product bits' upper half returned in hi and the lower
   435  // half returned in lo.
   436  func Mul64(x, y uint64) (hi, lo uint64) {
   437  	const mask32 = 1<<32 - 1
   438  	x0 := x & mask32
   439  	x1 := x >> 32
   440  	y0 := y & mask32
   441  	y1 := y >> 32
   442  	w0 := x0 * y0
   443  	t := x1*y0 + w0>>32
   444  	w1 := t & mask32
   445  	w2 := t >> 32
   446  	w1 += x0 * y1
   447  	hi = x1*y1 + w2 + w1>>32
   448  	lo = x * y
   449  	return
   450  }
   451  
   452  // --- Full-width divide ---
   453  
   454  // Div returns the quotient and remainder of (hi, lo) divided by y:
   455  // quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper
   456  // half in parameter hi and the lower half in parameter lo.
   457  // Div panics for y == 0 (division by zero) or y <= hi (quotient overflow).
   458  func Div(hi, lo, y uint) (quo, rem uint) {
   459  	if UintSize == 32 {
   460  		q, r := Div32(uint32(hi), uint32(lo), uint32(y))
   461  		return uint(q), uint(r)
   462  	}
   463  	q, r := Div64(uint64(hi), uint64(lo), uint64(y))
   464  	return uint(q), uint(r)
   465  }
   466  
   467  // Div32 returns the quotient and remainder of (hi, lo) divided by y:
   468  // quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper
   469  // half in parameter hi and the lower half in parameter lo.
   470  // Div32 panics for y == 0 (division by zero) or y <= hi (quotient overflow).
   471  func Div32(hi, lo, y uint32) (quo, rem uint32) {
   472  	if y != 0 && y <= hi {
   473  		panic(overflowError)
   474  	}
   475  	z := uint64(hi)<<32 | uint64(lo)
   476  	quo, rem = uint32(z/uint64(y)), uint32(z%uint64(y))
   477  	return
   478  }
   479  
   480  // Div64 returns the quotient and remainder of (hi, lo) divided by y:
   481  // quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper
   482  // half in parameter hi and the lower half in parameter lo.
   483  // Div64 panics for y == 0 (division by zero) or y <= hi (quotient overflow).
   484  func Div64(hi, lo, y uint64) (quo, rem uint64) {
   485  	const (
   486  		two32  = 1 << 32
   487  		mask32 = two32 - 1
   488  	)
   489  	if y == 0 {
   490  		panic(divideError)
   491  	}
   492  	if y <= hi {
   493  		panic(overflowError)
   494  	}
   495  
   496  	s := uint(LeadingZeros64(y))
   497  	y <<= s
   498  
   499  	yn1 := y >> 32
   500  	yn0 := y & mask32
   501  	un32 := hi<<s | lo>>(64-s)
   502  	un10 := lo << s
   503  	un1 := un10 >> 32
   504  	un0 := un10 & mask32
   505  	q1 := un32 / yn1
   506  	rhat := un32 - q1*yn1
   507  
   508  	for q1 >= two32 || q1*yn0 > two32*rhat+un1 {
   509  		q1--
   510  		rhat += yn1
   511  		if rhat >= two32 {
   512  			break
   513  		}
   514  	}
   515  
   516  	un21 := un32*two32 + un1 - q1*y
   517  	q0 := un21 / yn1
   518  	rhat = un21 - q0*yn1
   519  
   520  	for q0 >= two32 || q0*yn0 > two32*rhat+un0 {
   521  		q0--
   522  		rhat += yn1
   523  		if rhat >= two32 {
   524  			break
   525  		}
   526  	}
   527  
   528  	return q1*two32 + q0, (un21*two32 + un0 - q0*y) >> s
   529  }
   530  
   531  //go:linkname overflowError runtime.overflowError
   532  var overflowError error
   533  
   534  //go:linkname divideError runtime.divideError
   535  var divideError error
   536  

View as plain text