gopkg.in/alecthomas/gometalinter.v3@v3.0.0/_linters/src/golang.org/x/tools/container/intsets/popcnt_generic.go (about)

     1  // Copyright 2015 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  // +build !amd64 appengine
     6  // +build !gccgo
     7  
     8  package intsets
     9  
    10  import "runtime"
    11  
    12  // We compared three algorithms---Hacker's Delight, table lookup,
    13  // and AMD64's SSE4.1 hardware POPCNT---on a 2.67GHz Xeon X5550.
    14  //
    15  // % GOARCH=amd64 go test -run=NONE -bench=Popcount
    16  // POPCNT               5.12 ns/op
    17  // Table                8.53 ns/op
    18  // HackersDelight       9.96 ns/op
    19  //
    20  // % GOARCH=386 go test -run=NONE -bench=Popcount
    21  // Table               10.4  ns/op
    22  // HackersDelight       5.23 ns/op
    23  //
    24  // (AMD64's ABM1 hardware supports ntz and nlz too,
    25  // but they aren't critical.)
    26  
    27  // popcount returns the population count (number of set bits) of x.
    28  func popcount(x word) int {
    29  	if runtime.GOARCH == "386" {
    30  		return popcountHD(uint32(x))
    31  	}
    32  	return popcountTable(x)
    33  }