TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Rust's integer intrinsics are impressive

89 pointsby miqktalmost 8 years ago

11 comments

mrkgnaoalmost 8 years ago
&gt; conspiracy theories around popcount and the NSA<p>Whoa. Some links (with sometimes not-very-well-thought-out allegations):<p><a href="https:&#x2F;&#x2F;groups.google.com&#x2F;forum&#x2F;#!topic&#x2F;comp.arch&#x2F;UXEi7G6WHuU" rel="nofollow">https:&#x2F;&#x2F;groups.google.com&#x2F;forum&#x2F;#!topic&#x2F;comp.arch&#x2F;UXEi7G6WHu...</a><p><a href="https:&#x2F;&#x2F;www.schneier.com&#x2F;blog&#x2F;archives&#x2F;2014&#x2F;05&#x2F;the_nsa_is_not_.html" rel="nofollow">https:&#x2F;&#x2F;www.schneier.com&#x2F;blog&#x2F;archives&#x2F;2014&#x2F;05&#x2F;the_nsa_is_no...</a>
morecoffeealmost 8 years ago
I like that it compiles to a single instruction, but lots of languages already have this; it&#x27;s pretty common.<p>Even boring old Java has had Integer.bitCount for many years.
评论 #14526762 未加载
评论 #14527699 未加载
bobbyi_settvalmost 8 years ago
I did some experimentation with popcnt in C++ six years ago and was impressed to find that the intrinsic in gcc was faster than the best inline assembly I could come up with using the popcnt instruction:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;bobbyi&#x2F;Fast-Bit-Counting" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;bobbyi&#x2F;Fast-Bit-Counting</a>
Animatsalmost 8 years ago
Now that most CPUs have population count, it may as well be exposed at the language level.<p>Doing it by table lookup results in questions such as &quot;am I wasting too much cache space on this?&quot; and &quot;is a 64K table causing cache misses&quot;.
评论 #14527795 未加载
评论 #14526439 未加载
Flowalmost 8 years ago
Exactly what purpose does the surrounding instructions serve in this and similar simple cases? Is it compiler dogma or a missed uncommon optimization?<p><pre><code> push rbp mov rbp, rsp popcnt eax, edi pop rbp ret</code></pre>
评论 #14526821 未加载
评论 #14526878 未加载
评论 #14526842 未加载
评论 #14526833 未加载
评论 #14527530 未加载
dnauticsalmost 8 years ago
It&#x27;s exposed as an intrinsic in llvm. C(99? I think) surfaces these in math.h, although the standard is silent on some edge cases (all zeros), and numbers are autopromoted to 32 bit integer. Julia surfaces these as well.<p>In case you&#x27;re wondering what this could be useful for besides super secret NSA stuff, and Bitcoin mining, here are a few suggestions:<p>1) hyperloglog. (Similar to Bitcoin). Keep an estimated count of items streaming by by hashing them and store the highest lzcount of the hashes for each category you&#x27;re tracking. This will be ~ log2(category count)<p>2) converting from fixed point to floating point. The number of zeros in front of your value represents the exponent of your value (or ones in the case of a 2&#x27;s complement negative fixed point), which is critical to deducting the float representation.<p>Along those lines, one of the things I&#x27;ve done is implemented floating point-like datatypes, which extensively uses lzcount and locount for tracking values and also will use tzcount to measure if the values are exact or not.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;Etaphase&#x2F;FastSigmoids.jl&#x2F;blob&#x2F;master&#x2F;README.md" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Etaphase&#x2F;FastSigmoids.jl&#x2F;blob&#x2F;master&#x2F;READ...</a>
pklausleralmost 8 years ago
I&#x27;ve been using bit population count to traverse packed sparse arrays since the CDC 6600, so it&#x27;s handy for more things than Hamming distance. Always nice to have in hardware, but pretty cheap to synthesize when it isn&#x27;t.
Someonealmost 8 years ago
Reading <a href="http:&#x2F;&#x2F;0x80.pl&#x2F;articles&#x2F;sse-popcount.html" rel="nofollow">http:&#x2F;&#x2F;0x80.pl&#x2F;articles&#x2F;sse-popcount.html</a>, I would think the hardware instruction is slower than the best software implementation. Or has that changed on newer hardware?
评论 #14527522 未加载
dangalmost 8 years ago
This submission originally linked to a blog post whose author (not the HN submitter) asked us to delete it. We don&#x27;t want to kill the thread, so we removed the URL above.
__salmost 8 years ago
&gt; Whereas GCC’s __builtin_popcount() only works on unsigned ints Rust’s count_ones() works on signed, too!<p>C&#x27;s weak typing should handle signed
评论 #14527818 未加载
jblowalmost 8 years ago
<i>I was once asked to come up with a table lookup method for popcount on the spot and could not come up with a solution.</i><p>Oh, Hacker News.<p>If someone can&#x27;t solve a problem like this off the top of their head, does it not act as a strong signal that they are a beginner and you should probably look elsewhere for quality information?
评论 #14527722 未加载