bits

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

README

Miscellaneous Bit Operations

Introduction

The following operations are implemented:
'Count' counts number of '1' (set) bits ia a vector
'Populate' populates a preallocated [64]int32 array with bit positions in a int64 input
'Lsb/Msb' least significant/most significan bit position in a vector

Usage:

Count
package mypkg;

import (
	"fmt"
	"github.com/viant/vec/bits"
	"math/rand"
	"time"
)

func Example_Count() {
	rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
	var vec = make([]uint64, 267)
	for i := range vec {
		vec[i] = rnd.Uint64()
	}
	cnt := bits.Count(vec)
	fmt.Println(cnt)
}
Populate
package mypkg;

import (
	"fmt"
	"github.com/viant/vec/bits"
	"math/rand"
	"time"
)

func Example_Count() {
	var out bits.Positions
	input := rand.New(rand.NewSource(time.Now().UnixNano())).Uint64()
	lastIndex := out.Populate(input)
	fmt.Println(out[0:lastIndex])
}
LSb/Msb
package mypkg;

import (
	"fmt"
	"github.com/viant/vec/bits"
	"math/rand"
	"time"
)

func Example_Lsb() {
	rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
	var vec = make([]uint64, 267)
	for i := range vec {
		vec[i] = rnd.Uint64()
	}
	position := bits.Lsb(vec)
	fmt.Println(position)}

Benchmarks

ARM64 (Neon)
goarch: arm64
pkg: github.com/viant/vec/bits
BenchmarkCountNaive-16        	60427422	        19.49 ns/op
BenchmarkCount-16             	176838339	         6.719 ns/op
BenchmarkLsbNaive-16          	63967446	        19.11 ns/op
BenchmarkLsb-16               	160451912	         7.408 ns/op
BenchmarkMsbNaive-16          	62994744	        19.14 ns/op
BenchmarkMsb-16               	136166562	         8.788 ns/op
BenchmarkPositionsNaive-16    	50261076	        23.71 ns/op
BenchmarkPositions-16         	199557674	         5.987 ns/op
AMD64 (AVX2)

** MacBook2.4 GHz 8-Core Intel Core i9**

goarch: amd64
pkg: github.com/viant/vec/bits
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkCountNaive-16          34619952                31.37 ns/op
BenchmarkCount-16               82600533                13.84 ns/op
BenchmarkPositionsNaive-16      43401525                26.80 ns/op
BenchmarkPositions-16           170968920                6.977 ns/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Count

func Count(data []uint64) int

func Lsb added in v0.2.0

func Lsb(data []uint64) int

func Msb added in v0.2.0

func Msb(data []uint64) int

Types

type Positions

type Positions [64]uint32

func (*Positions) Populate

func (p *Positions) Populate(input uint64) uint8

Jump to

Keyboard shortcuts

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