math

package
v0.0.0-...-2f611ba Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2022 License: MIT Imports: 0 Imported by: 0

README

Math

BitCount

32-bit

The most efficient implementations for 32-bit are:

BitCountUint32Pop1Alt:

func BitCountUint32Pop1Alt(x uint32) uint {
	x = x - ((x >> 1) & 0x55555555)
	x = (x & 0x33333333) + ((x >> 2) & 0x33333333)
	x = (x + (x >> 4)) & 0x0f0f0f0f
	return uint((x * 0x01010101) >> 24)
}

BitCountUint32Pop3:

func BitCountUint32Pop3(x uint32) uint {
	n := (x >> 1) & 0x77777777 // Count bits in
	x = x - n                  // each 4-bit
	n = (n >> 1) & 0x77777777  // field.
	x = x - n
	n = (n >> 1) & 0x77777777
	x = x - n
	x = (x + (x >> 4)) & 0x0f0f0f0f // Get byte sums.
	x = x * 0x01010101              // Add the bytes.
	return uint(x >> 24)
}

BitCountUint32Pop6:

var byteToBitCountTable = [...]uint{
	0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
	1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
	1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
	2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,

	1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
	2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
	2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
	3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,

	1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
	2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
	2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
	3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,

	2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
	3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
	3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
	4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
}

func BitCountUint32Pop6(x uint32) uint {
	return byteToBitCountTable[x&0xff] +
		byteToBitCountTable[(x>>8)&0xff] +
		byteToBitCountTable[(x>>16)&0xff] +
		byteToBitCountTable[(x>>24)]
}

The implementations BitCountUint32Pop1Alt and BitCountUint32Pop6 are more widely used. They are easier than BitCountUint32Pop3 to understand. As a result, they are used as the optimized implementation and fallback implementation of GCC's __builtin_popcount respectively. In benchmarks, these 3 implementations score almost the same most of the time. BitCountUint32Pop1Alt is usually the best among them when there is a difference.

64-bit

Here are the 64-bit implementations:

BitCountUint64Pop1Alt:

func BitCountUint64Pop1Alt(x uint64) uint {
	x = x - ((x >> 1) & 0x5555555555555555)
	x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
	x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
	return uint((x * 0x0101010101010101) >> 56)
}

BitCountUint64Pop3:

func BitCountUint64Pop3(x uint64) uint {
	n := (x >> 1) & 0x7777777777777777 // Count bits in
	x = x - n                          // each 4-bit
	n = (n >> 1) & 0x7777777777777777  // field.
	x = x - n
	n = (n >> 1) & 0x7777777777777777
	x = x - n
	x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f // Get byte sums.
	x = x * 0x0101010101010101              // Add the bytes.
	return uint(x >> 56)
}

BitCountUint64Pop6:

// byteToBitCountTable is the same as that in the 32-bit implementation above.

func BitCountUint64Pop6(x uint64) uint {
	return byteToBitCountTable[x&0xff] +
		byteToBitCountTable[(x>>8)&0xff] +
		byteToBitCountTable[(x>>16)&0xff] +
		byteToBitCountTable[(x>>24)&0xff] +
		byteToBitCountTable[(x>>32)&0xff] +
		byteToBitCountTable[(x>>40)&0xff] +
		byteToBitCountTable[(x>>48)&0xff] +
		byteToBitCountTable[(x>>56)]
}

For 64-bit, the best implementations are BitCountUint64Pop1Alt and BitCountUint64Pop3. The table-based implementation BitCountUint64Pop6 is significantly slower than its 32-bit counterpart.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsEven0

func IsEven0(x int) bool

IsEven0 returns whether x is an even number.

func IsEven1

func IsEven1(x int) bool

IsEven1 returns whether x is an even number.

func IsEvenNaive

func IsEvenNaive(x int) bool

IsEvenNaive returns whether x is an even number.

func IsOdd0

func IsOdd0(x int) bool

IsOdd0 returns whether x is an odd number.

func IsOdd1

func IsOdd1(x int) bool

IsOdd1 returns whether x is an odd number.

func IsOddNaive

func IsOddNaive(x int) bool

IsOddNaive returns whether x is an odd number.

func Prime

func Prime() func() uint64

Types

This section is empty.

Directories

Path Synopsis
Package bits implements bit counting and manipulation functions for the predeclared unsigned integer types.
Package bits implements bit counting and manipulation functions for the predeclared unsigned integer types.
impl/onescount
References: http://www.hackersdelight.org/hdcodetxt/pop.c.txt http://www.dalkescientific.com/writings/diary/archive/2008/07/03/hakmem_and_other_popcounts.html
References: http://www.hackersdelight.org/hdcodetxt/pop.c.txt http://www.dalkescientific.com/writings/diary/archive/2008/07/03/hakmem_and_other_popcounts.html
impl/onescount/hakmem
Source: https://stackoverflow.com/q/8590432/142239 Source: http://www.stmintz.com/ccc/index.php?id=94570
Source: https://stackoverflow.com/q/8590432/142239 Source: http://www.stmintz.com/ccc/index.php?id=94570
impl/onescount/pop0
Source: http://www.hackersdelight.org/hdcodetxt/pop.c.txt (pop0)
Source: http://www.hackersdelight.org/hdcodetxt/pop.c.txt (pop0)
impl/onescount/pop1
Source: http://www.hackersdelight.org/hdcodetxt/pop.c.txt (pop1) Source: java.lang.Integer#bitCount Source: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/lang/Integer.java#Integer.bitCount%28int%29 Source: java.lang.Long#bitCount Source: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/lang/Long.java#Long.bitCount%28long%29
Source: http://www.hackersdelight.org/hdcodetxt/pop.c.txt (pop1) Source: java.lang.Integer#bitCount Source: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/lang/Integer.java#Integer.bitCount%28int%29 Source: java.lang.Long#bitCount Source: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/lang/Long.java#Long.bitCount%28long%29
impl/onescount/pop1a
Source: http://www.hackersdelight.org/hdcodetxt/pop.c.txt (pop1 + alternative) Source: https://github.com/gcc-mirror/gcc/blob/master/libgcc/libgcc2.c#L840-L859 Source: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36041#c8
Source: http://www.hackersdelight.org/hdcodetxt/pop.c.txt (pop1 + alternative) Source: https://github.com/gcc-mirror/gcc/blob/master/libgcc/libgcc2.c#L840-L859 Source: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36041#c8
impl/onescount/pop2
Source: http://www.hackersdelight.org/hdcodetxt/pop.c.txt (pop2)
Source: http://www.hackersdelight.org/hdcodetxt/pop.c.txt (pop2)
impl/onescount/pop2a
Source: http://www.hackersdelight.org/hdcodetxt/pop.c.txt (pop2 + alternative)
Source: http://www.hackersdelight.org/hdcodetxt/pop.c.txt (pop2 + alternative)
impl/onescount/pop3
Source: http://www.hackersdelight.org/hdcodetxt/pop.c.txt (pop3)
Source: http://www.hackersdelight.org/hdcodetxt/pop.c.txt (pop3)
impl/onescount/pop5
Source: http://www.hackersdelight.org/hdcodetxt/pop.c.txt (pop5)
Source: http://www.hackersdelight.org/hdcodetxt/pop.c.txt (pop5)
impl/onescount/reset
Source: http://www.hackersdelight.org/hdcodetxt/pop.c.txt (pop4)
Source: http://www.hackersdelight.org/hdcodetxt/pop.c.txt (pop4)
impl/onescount/subtract
Source: http://www.hackersdelight.org/hdcodetxt/pop.c.txt (pop5a)
Source: http://www.hackersdelight.org/hdcodetxt/pop.c.txt (pop5a)
impl/onescount/table
Source: http://www.hackersdelight.org/hdcodetxt/pop.c.txt (pop6)
Source: http://www.hackersdelight.org/hdcodetxt/pop.c.txt (pop6)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL