Skip to content

Commit d94e6fe

Browse files
container/intsets: use fast popcount with gccgo
Use the compiler intrinsic __builtin_popcount. Change-Id: I58286fbcf66d1068390ea9caff2f98b8fe244c2d Reviewed-on: https://go-review.googlesource.com/16831 Reviewed-by: Andrew Gerrand <[email protected]>
1 parent 3dedf80 commit d94e6fe

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

container/intsets/popcnt_gccgo.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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 gccgo
6+
7+
package intsets
8+
9+
func popcount(x word) int

container/intsets/popcnt_gccgo_c.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 gccgo
6+
7+
#include <errno.h>
8+
#include <stdint.h>
9+
#include <unistd.h>
10+
11+
#define _STRINGIFY2_(x) #x
12+
#define _STRINGIFY_(x) _STRINGIFY2_(x)
13+
#define GOSYM_PREFIX _STRINGIFY_(__USER_LABEL_PREFIX__)
14+
15+
extern intptr_t popcount(uintptr_t x) __asm__(GOSYM_PREFIX GOPKGPATH ".popcount");
16+
17+
intptr_t popcount(uintptr_t x) {
18+
return __builtin_popcountl((unsigned long)(x));
19+
}

container/intsets/popcnt_generic.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build !amd64 appengine gccgo
5+
// +build !amd64 appengine
6+
// +build !gccgo
67

78
package intsets
89

0 commit comments

Comments
 (0)