Bithacks.h - bit hack macros
11–20 of 20 posts
Re: Bithacks.h - bit hack macros
#12Re: Bithacks.h - bit hack macros
#13So this is nice, but is it super fast/efficient? It would seem to me that this sort of thing is where inline assembly, on a per processor basis, would really make a huge difference. Is this sort of thing well enough known that compilers already do it for us? Anyone know a lot about this sort of thing?
A smart compiler could theoretically transform your crappy implementation of "is the Nth bit set?" into something more intelligent, but it is rarely profitable enough to do so, as programmers who write code in which bit-banging is the bottleneck typically know how to do this themselves; in fact, most of the macros linked are what I consider to be the most parsimonious implementation.
One thing where an intrinsic will definitely beat a C-level implementation is population count (how many bits are set in this int/long?) Many recent x86 parts and many DSPs implement a POPCOUNT/BC instruction which will outperform even a smart LUT-based implementation (without the memory capacity/bandwidth requirement too). There's an interesting anecdote about that instruction here (http://www.moyogo.com/blog/2005/09/secret-opcodes.html ), no idea if it's true.
Re: Bithacks.h - bit hack macros
#14Earlier quoted context omitted.
As languages go C may be a lot of things but I don't think you can call it "unpopular".
Indeed. I'm always surprised at the number of people I meet (and, distressingly, the number of posters on this site) that honestly believe that "most" software is written by web geeks or IT droids in scripting languages or Java or .NET. That, and the fact that this belief persists as they turn off their digital televisions, click their garage door remotes, text a friend on their phone, fire up the engine in their per…
Re: Bithacks.h - bit hack macros
#15Whenever I need to do bit manipulations, I always check the Linux kernel source. I figure that any bit manipulation I'd ever want to do is also done in the kernel, and I'm usually right: http://miller.cs.wm.edu/lxr3.linux/http/source/include/linux... http://miller.cs.wm.edu/lxr3.linux/http/source/include/linux... http://miller.cs.wm.edu/lxr3.linux/http/source/lib/bitmap.c?...
Re: Bithacks.h - bit hack macros
#16For this sort of thing, it would seem helpful to release it under "any license viewed as free by the FSF" or similar. Is that sort of statement likely to cause problems? Or at least license it MIT/BSD/whatever. For such a small piece, it would be nice if projects can just import it without having to move from "all code is BSD" to "all code is BSD except bithacks.h which is MIT, but we also comply with that license".
Re: Bithacks.h - bit hack macros
#17Whenever I need to do bit manipulations, I always check the Linux kernel source. I figure that any bit manipulation I'd ever want to do is also done in the kernel, and I'm usually right: http://miller.cs.wm.edu/lxr3.linux/http/source/include/linux... http://miller.cs.wm.edu/lxr3.linux/http/source/include/linux... http://miller.cs.wm.edu/lxr3.linux/http/source/lib/bitmap.c?...
Re: Bithacks.h - bit hack macros
#18Whenever I need to do bit manipulations, I always check the Linux kernel source. I figure that any bit manipulation I'd ever want to do is also done in the kernel, and I'm usually right: http://miller.cs.wm.edu/lxr3.linux/http/source/include/linux... http://miller.cs.wm.edu/lxr3.linux/http/source/include/linux... http://miller.cs.wm.edu/lxr3.linux/http/source/lib/bitmap.c?...
These are great, I had forgotten about them!
Re: Bithacks.h - bit hack macros
#19So this is nice, but is it super fast/efficient? It would seem to me that this sort of thing is where inline assembly, on a per processor basis, would really make a huge difference. Is this sort of thing well enough known that compilers already do it for us? Anyone know a lot about this sort of thing?
In general, this will probably get you close to maximum performance. Most CPUs implement shift, rotate, AND, OR, XOR, and NOT instructions, and usually not much else in the way of bit manipulation (besides maybe a few very specialized intrinsics for popular encryption algorithms/hash functions (see SSE)). DSPs and more domain-specific microcontrollers generally have more bit-banging instructions, and you might be abl…
Re: Bithacks.h - bit hack macros
#20Earlier quoted context omitted.
As languages go C may be a lot of things but I don't think you can call it "unpopular".
Indeed. I'm always surprised at the number of people I meet (and, distressingly, the number of posters on this site) that honestly believe that "most" software is written by web geeks or IT droids in scripting languages or Java or .NET. That, and the fact that this belief persists as they turn off their digital televisions, click their garage door remotes, text a friend on their phone, fire up the engine in their per…