bit parallelism (WAS: Programming Language Complexity) 
Author Message
 bit parallelism (WAS: Programming Language Complexity)


|> >|>
|> >|> Is there such a thing as portable, efficient bit-manipulation? I'm not
|> >|> aware of any language that supports it. C certainly doesn't--and its
|> >|> crummy shift-and-mask approach to bit manipulation is hardly efficient.
|> >
|> >Yes, there *is* portable, efficient bit-manipulation. I am currently writing
|> >code that is extremely bit-oriented. It uses 64 bit word operations which
|> >are #define'd as macros because not every machine/compiler combination supports
|> >that data type (when will metrowerks support long long? Do we *really* have to
|> >wait for the first MPC620 based PowerMacintosh?).
|>
|> I'm still skeptical, though I'd love to be proven wrong.  How do you handle
|> bit *rotation* in C, portably and efficiently?

I never felt the need for bit rotation, but I coded something for you. You
never know when you will need it...

Note that c compilers are really *bad* when it comes to generate rotate instructions.
I heard that there is one compiler (for the ARM cpu) that actually uses this instruction,
but I never saw a compiler use it. Really.

BTW tech writers, go and hide away... Here comes the code... (^8

/*
    portable efficient bit rotation macros in ansi c, Peter W. Gillgasch 95/04/02


    Supports char, short and long data types. ints are handled correctly, since they
    are identified (at compile time) as shorts or longs, depending on the compiler.
    Note that we give cpp a real workout...
*/

#define NDEBUG

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <assert.h>

#define bitsof(a) (sizeof(a) * CHAR_BIT)

#if (CHAR_BIT == 8) || (CHAR_BIT == 16) || (CHAR_BIT == 32) || (CHAR_BIT == 64)
/* this should catch most machines */
#define ROT_REDUCE(a,n) (assert((n) >= 0), ((n) & (bitsof(a)-1)))
#else
/* some machines have strange char sizes (which deserves a modulo operation!) */
#define ROT_REDUCE(a,n) (assert((n) >= 0), ((n) % bitsof(a)))
#endif

/* generic type safe operations */

/*  "n" bit right rotation. the "n" has to be positive and reduced to 0 <= n <= bitsof(type). */

#define GENERIC_ROR(a,n,type) (assert(n >= 0), assert(n <= bitsof(a)),\
 (unsigned type) ((((unsigned type)(a)) >> (n)) | (((unsigned type)(a)) << (bitsof(a) - (n)))))

/* "n" bit left rotation. the "n" has to be positive and reduced to 0 <= n <= bitsof(type). */

#define GENERIC_ROL(a,n,type)  (assert(n >= 0), assert(n <= bitsof(a)),\
(unsigned type) ((((unsigned type)(a)) << (n)) | (((unsigned type)(a)) >> (bitsof(a) - (n)))))

/* let the compiler select a type safe operation */

#define SELECT_GENERIC(a,n,GENERIC) (\
  (sizeof(a)==sizeof(char))? GENERIC(a,n,char)\
 :((sizeof(a)==sizeof(short))?GENERIC(a,n,short)\
 :GENERIC(a,n,long) )\
)

/* user macros */

#define ROR(a,n) SELECT_GENERIC(a,n,GENERIC_ROR)    
#define ROL(a,n) SELECT_GENERIC(a,n,GENERIC_ROL)    
#define ROT(a,n) (((n) >= 0) ? ROL(a,n) : ROR(a,-(n)))  
#define ROR_ANY(a,n) ROR(a,ROT_REDUCE(a,(n)))
#define ROL_ANY(a,n) ROL(a,ROT_REDUCE(a,(n)))
#define ROT_ANY(a,n) (((n) >= 0) ? ROL_ANY(a,n) : ROR_ANY(a,-(n)))

int main(int argc, char *argv[]) {

    int loop;

    for(loop = 0; loop < 256; loop ++) {
        const char c = loop; /* or short or long or int */
        const unsigned long rotright = ROT_ANY(c,-1);
        const unsigned long rotleft = ROT_ANY(c,1);
        printf("%8X ROR %8X ROL %8X\n", loop, rotright,rotleft);
    };

    exit(0);
    return 0; /* to make the compiler happy */

Quote:
}

Hope this helps. Any comments from the Pascal periphery? Or
are you all busy with printing t shirts? (^8

-- Peter

------------------------------------------------------------
Peter W. Gillgasch

76131 Karlsruhe/Germany            Phone ++49/(0)721/6904255



Thu, 18 Sep 1997 04:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Complexity (Was: Why use a functional language?)

2. I am new to programming and am lost

3. Calling 64 bit lib. from 32 bit program

4. Mixed 16-bit, 32-bit programming question

5. Mixed 16 bit, 32 bit programming question

6. problem in 16 bit program calling 32 bit DLL

7. using 16-bit DLL in 32-bit programming environment

8. HELP: porting 16 bit mfc program to 32 bit (mfc v5)

9. deque - I am being bitten

10. SURVEY on MS-DOS C/C++ Parallelism/Multitasking

11. new freeware programming tool, source available: Programming Language Creator 1.00c (with C Interpreter Module)

12. How can I get good at programming C programming language

 

 
Powered by phpBB® Forum Software