Just noticed in the archives that my response never went to the mailing list. People have covered most of it already but here ya go anyways... (:
Well, you could certainly make a set of functions for the basic lego pieces of bit twiddling, and overload them for each type you want to support. You'd want to mark them as inline to make sure and not pay the overhead of having them be a function though of course. Like this... inline void RotateLeft(int &value) { // do it } inline void RotateLeft(char &value) { // do it } inline void RotateLeft(long int &value) { // do it } Then, in your code, you don't care what type it is, you just call RotateLeft() and it does the rotation, calling the correct version of the function. (watch out for implicit conversion though - which could happen if you start using a type that you didn't implement the functions for!) If you further want to divorce your code from specific types, template functions and template classes can help you get there too. For maximum performance, just make sure and keep functions marked as inlined, and stay away from virtual functions. Another potential tool in your toolbox could be "stdtypes.h" which defines specifically sized data types e.g. uint8, int32 so you can work in known sized data types instead of the loosely sized types built into C++. In a somewhat similar vein, I wrote an article yesterday about a technique that is really useful for writing lightning fast code that also needs to be configurable (dsp is one such type of software) Sharing in case anyone finds it interesting or useful, it's a bit on topic :P http://blog.demofox.org/2013/05/01/permutation-programming-without-maintenance-nightmares/ On Wed, May 1, 2013 at 1:35 PM, Sampo Syreeni <de...@iki.fi> wrote: > For the longest time I took out a compiler and started cranking out an old > idea. In that vein, I'm using libsndfile and its (highly reasonable) > processing model: you just keep everything to zero padded ints (preferably > signed) and go from there. > > The trouble is that my code is of the kind that also requires lots of bit > twiddling. My current problem comes from trying to make the code more or > less adaptive to any bit width, while I also have to do stuff like computed > shifts. > > So, how do you go about systematically and portably implementing what you > would expect from your logical operations, using standard C operations, > without knowing the basic width of your types? (Logical, not arithmetic) > right shifts of signed quantities, efficient parity, and computed shifts > with negative offsets are proving particularly nasty at the moment. (It has > to do with dithering at arbitrary word length which also has to be > reasonably efficient if any set in silicon.) > -- > Sampo Syreeni, aka decoy - de...@iki.fi, http://decoy.iki.fi/front > +358-50-5756111, 025E D175 ABE5 027C 9494 EEB0 E090 8BA9 0509 85C2 > -- > dupswapdrop -- the music-dsp mailing list and website: > subscription info, FAQ, source code archive, list archive, book reviews, dsp > links > http://music.columbia.edu/cmc/music-dsp > http://music.columbia.edu/mailman/listinfo/music-dsp -- dupswapdrop -- the music-dsp mailing list and website: subscription info, FAQ, source code archive, list archive, book reviews, dsp links http://music.columbia.edu/cmc/music-dsp http://music.columbia.edu/mailman/listinfo/music-dsp