7 ms·
ARM64 Popcount in Golang and Assembler
- eqvinox 6y ago__builtin_popcount() ... which gcc and clang will replace with best available. 'nuff said. Edit: or OnesCount() in Go.
- majke 6y agoCompulsory popcount resources from Wojciech Mula http://0x80.pl/articles/sse-popcount.html http://0x80.pl/articles/sse-popcount.html
- fanf2 6y agoNice link, thanks! My interest in popcount is for radix trie node compression https://dotat.at/prog/qp/ https://dotat.at/prog/qp/ so these bulk popcount hacks are interesting but not directly useful for my purposes :-)
- 13415 6y agoThere is also a C library based on it: https://github.com/kimwalisch/libpopcnt https://github.com/kimwalisch/libpopcnt
- mikece 6y agoSlight tangent, but writing custom assembler code to make this go faster on the ARM chip makes me wonder how much optimization the Apple team has put into the Swift compiler for the A-series, work that has been going on for years for iOS. I suspect the answer is "a lot more than we realize" and it would be interesting to see this Golang comparison re-run on macOS/ARM (when available) and then compared to the same program written in Swift.
- donarb 6y agoApple has (or will be) submitting ARM patches for the Go project. https://twitter.com/wongmjane/status/1275177255681982464?lang=en https://twitter.com/wongmjane/status/1275177255681982464?lan...
- innocenat 6y agoWow. This is the first time I have seen Go Assembler Syntax, and it confused me at first, as I thought it was the standard Intel syntax.
- cesarb 6y ago> Take whichever side you want in the Intel vs GNU syntax debate, creating a third option means relearning all the quirks from scratch, and ignoring any documentation that already exists. Isn't the "Intel vs GNU syntax debate" only on x86? For ARM, AFAIK there's only one assembly syntax.
- barbegal 6y agoARM assembly and GNU assembly for ARM are subtly different in terms of syntax. I think the labels and indenting is different but everything else is broadly the same. I can happily read either.
- waynecochran 6y agoFWIW, I learned a little bit about how to check for supported features in x86_64 from the linked code (I added the comments): TEXT ·hasPOPCNT(SB),NOSPLIT,$0 XORQ AX, AX ; AX <- 0 INCL AX ; AX <- 1 CPUID ; check cpu caps SHRQ $23, CX ; shift bit 23 to bit 0 (bit 23 => POPCNT support) ANDQ $1, CX ; examine bit 0 MOVB CX, ret+0(FP) ; return 1 iff POPCNT instruction supported RET It seems Intel CPU's have such a bloated and varied instruction set you have to query to see what flavor of CPU you have to create portable code. Writing compilers for this must be a real pain. Full disclosure : I work for Intel
- saagarjha 6y agoUsually what a compiler will do is target some "reasonable" CPU from the past, for your choice of "reasonable". For example, the C compiler on my system defaults to Penryn. Unrelated: what does Intel Sports do?
- waynecochran 6y agoImmersive viewing of sporting events: https://www.intel.com/content/www/us/en/sports/sports-overview.html https://www.intel.com/content/www/us/en/sports/sports-overvi...
- prattmic 6y agoWhile CPUID itself is far from elegant, I don't think ARM is fundamentally different. e.g., the ID_AA64ISAR1_EL1 register (ARM wins no naming awards here) indicates the presence of certain instructions. https://developer.arm.com/docs/ddi0595/h/aarch64-system-registers/id_aa64isar1_el1 https://developer.arm.com/docs/ddi0595/h/aarch64-system-regi...
- saagarjha 6y agoaarch64 is actually worse as far as I can tell because reading from that register is restricted and cannot be done from userspace (hence "EL1") and thus doing this portably is a huge pain. On Linux I think some of this is exported via /proc/cpuinfo and auxval, so that's what most people use… Edit: Also there's a ton of them, this lists a couple: https://www.kernel.org/doc/html/latest/arm64/cpu-feature-registers.html https://www.kernel.org/doc/html/latest/arm64/cpu-feature-reg...
- pantalaimon 6y agoI would have expected the Go compiler to emit the POPCNT instruction on it's own. Most current C compilers will even detect if you implement a popcount in software and replace it with a single POPCNT instruction - why does Go not do something like that as well?
- smartisch 6y agoBecause Go Gc amd64 target supports CPUs that do not have support for the popcnt instruction and runtime detection + checking a bool around each popcnt instruction is a big overhead. There is no mtune or march for Go Gc amd64 like (Go) gcc does.
- pantalaimon 6y agoHuh, TIL. I always assumed this to be a rather old instruction, but turns out it was only added fairly recently on x86 https://vaibhavsagar.com/blog/2019/09/08/popcount/ https://vaibhavsagar.com/blog/2019/09/08/popcount/
- saagarjha 6y agoGo's compiler values being fast over potential optimizations, I believe, so this might be something that they don't think is something the compiler should do.
- loeg 6y agoGo's compiler is not particularly sophisticated.
- saagarjha 6y ago(By design.)
- deleted 6y ago[deleted]
- fulafel 6y agoAny clues on what they author is using it for?
- StringyBob 6y agoThere’s a few potential uses these days - more than just the famous crypto folklore one! Some examples on https://vaibhavsagar.com/blog/2019/09/08/popcount/ https://vaibhavsagar.com/blog/2019/09/08/popcount/
- mzs 6y agohttps://news.ycombinator.com/item?id=23832978 https://news.ycombinator.com/item?id=23832978
- RenThraysk 6y agoErm, math/bits package OnesCount() family of functions? https://golang.org/pkg/math/bits/#OnesCount https://golang.org/pkg/math/bits/#OnesCount go compiler output... 0x0020 00032 ($GOROOT/src/math/bits/bits.go:115) FMOVD R0, F0 0x0024 00036 ($GOROOT/src/math/bits/bits.go:115) VCNT V0.B8, V0.B8 0x0028 00040 ($GOROOT/src/math/bits/bits.go:115) VUADDLV V0.B8, V0