Re: at2 swap_nibbles
Richard Braakman <[email protected]>
| Newsgroups | gmane.comp.mobile.kannel.devel |
|---|---|
| Message-ID | <20021126022136.GA4918@night> |
On Mon, Nov 25, 2002 at 07:05:23PM +0100, Andreas Fink wrote: > how about this: > > #define HI_NIBBLE(a) ( (int)a >> 4 & 0x0F) > #define LOW_NIBBLE(a) ( (int)a & 0x0F) > #define SWAP_NIBBLE(a) (( HI_NIBBLE(a) | (LOW_NIBBLE(a) << 4)) & 0xFF) > > This would work as a macro and has the advantage that the CPU can run > it pretty much native. Except that casting to int brings back the original sign-extension bug :) If swap_nibbles is used only in the at2 driver, then you can make it a static function and expect the compiler to inline it. If it's used in multiple files, then it's better off as a real library function in gwlib somewhere. (octstr_swap_nibbles perhaps?) If you need a really fast nibble swapper, then a table lookup is probably best. It'll only be a 256-byte table. I wouldn't bother with that kind of optimization unless profiling shows that it matters. Richard Braakman