in reply to 64-bit *nix/gcc expertise required (THIS IS NOT OFF TOPIC!)

Please modify this MS specific C code to compile under gcc on both 32-bit & 64-bit platforma and produce the same output:

Pointer 1<<34 doesn't make sense on 32-bit platforms.

I also don't see how you can get the same output on 32-bit and 64-bit platforms when ALIGN_BITS depends on the pointer size (sizeof(void*) >> 1i64). The data or ALIGN_BITS would need to be changed.

On to the real question, you need the following to be configurable:

So here goes:

#include <stdio.h> /* **** For a system where a pointer is an unsigned long long: */ typedef unsigned long long uintptr_t; #define UINTPTR_CONST(p) ((uintptr_t)p##ULL) #define UINTPTRXf "llX" /* **** For a system where a pointer is an unsigned long: typedef unsigned long uintptr_t; #define UINTPTR_CONST(p) ((uintptr_t)p##UL) #define UINTPTRXf "lX" */ #define NOP_BITS 2 #define BIT_BITS 3 #define BYTE_BITS 14 #define NOP_MASK UINTPTR_CONST(0x00000003) #define BIT_MASK UINTPTR_CONST(0x00000007) #define BYTE_MASK UINTPTR_CONST(0x00003FFF) #define NOP_OFFSET 0 #define BIT_OFFSET (sizeof(void*) >> 1) #define BYTE_OFFSET (BIT_OFFSET + BIT_BITS) #define SLOT_OFFSET (BYTE_OFFSET + BYTE_BITS) void check_new(void* p) { register uintptr_t i = (uintptr_t)p; uintptr_t slot = ( i >> SLOT_OFFSET ); uintptr_t byte = ( i >> BYTE_OFFSET ) & BYTE_MASK; uintptr_t bit = ( i >> BIT_OFFSET ) & BIT_MASK; uintptr_t nop = ( i >> NOP_OFFSET ) & NOP_MASK; printf("address: %012p " "slot: %012p " "byte: %4"UINTPTRXf" " "bit: %4"UINTPTRXf" " "nop: %"UINTPTRXf"\n", p, (void*)slot, byte, bit, nop ); } int main(int argc, char *argv[]) { void* p; for ( p = (void*)UINTPTR_CONST(123456); p < (void*)(UINTPTR_CONST(1) << 34); p += (UINTPTR_CONST(1) << 29) ) check_new(p); return 0; }

Replies are listed 'Best First'.
Re^2: 64-bit *nix/gcc expertise required
by BrowserUk (Patriarch) on Dec 24, 2010 at 18:45 UTC
    Pointer 1<<34 doesn't make sense on 32-bit platforms.

    The for loop in main, indeed the whole of main is just a test harness.

    I need to simulate addresses that span the 32-bit boundary in 64-bit and slot size in both 32/64-bit.

    I also don't see how you can get the same output on 32-bit and 64-bit platforms when ALIGN_BITS depends on the pointer size (sizeof(void*) >> 1i64). The data or ALIGN_BITS would need to be changed.

    You are right that the output won't be the same in both modes.

    The nice thing about the MS compiler intrinsic types is that my posted code compiles clean under /Wall in both modes. And runs, doing the 'right thing' in both modes. Albiet that the for loop never terminates in 32-bit mode due to wraparound.

    Your code

    The salient bit missing from your code is the conditional test for 32/64 bit compilation?

    Also, what you've called NOP_BITS (which is unused) would be 3 or 4 (32/64 repectively) not 2.

    Does your code produce the same output for 64-bit?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      The nice thing about the MS compiler intrinsic types is that my posted code compiles clean under /Wall in both modes

      MS compiler is usually good about giving these kind warnings from what I hear. I guess this is a situation it misses.

      Does your code produce the same output for 64-bit?

      Aside from printf formatting differences, yes.

      Also, what you've called NOP_BITS (which is unused) would be 3 or 4 (32/64 repectively) not 2.

      Your code uses & 0x3. That's 2 bits. I take it that's a bug.

      The salient bit missing from your code is the conditional test for 32/64 bit compilation?

      That's normally handled by a configuration script. It's not just based on the size of the pointers, but on the compiler's definition of "unsigned long" and such.

      stdint.h provides uintptr_t, but not the others.

      If this is XS code, Perl providers similar tools for you already.

      The code is now:

      #include <stdint.h> #include <stdio.h> #if UINTPTR_MAX == UINT32_MAX # define UINTPTR_C(p) UINT32_C(p) #elif UINTPTR_MAX == UINT64_MAX # define UINTPTR_C(p) UINT64_C(p) #else # error uintptr_t size not handled. #endif #define PAD_BITS (sizeof(void*) >> 1) #define BIT_BITS 3 #define BYTE_BITS 14 #define PAD_MASK ((UINTPTR_C(1) << PAD_BITS) - 1) #define BIT_MASK UINTPTR_C(0x00000007) #define BYTE_MASK UINTPTR_C(0x00003FFF) #define PAD_OFFSET 0 #define BIT_OFFSET (PAD_OFFSET + PAD_BITS) #define BYTE_OFFSET (BIT_OFFSET + BIT_BITS) #define SLOT_OFFSET (BYTE_OFFSET + BYTE_BITS) void check_new(void* p) { register uintptr_t i = (uintptr_t)p; uintptr_t slot = ( i >> SLOT_OFFSET ); uintptr_t byte = ( i >> BYTE_OFFSET ) & BYTE_MASK; uintptr_t bit = ( i >> BIT_OFFSET ) & BIT_MASK; uintptr_t pad = ( i >> PAD_OFFSET ) & PAD_MASK; printf("address: %016p " "slot: %016p " "byte: %4x " "bit: %4x " "nop: %x\n", p, (void*)slot, (unsigned)byte, (unsigned)bit, (unsigned)pad ); } int main(int argc, char *argv[]) { void* p; for ( p = (void*)UINTPTR_C(123456); p < (void*)(UINTPTR_C(1) << 34); p += (UINTPTR_C(1) << 29) ) check_new(p); return 0; }
      $ gcc -Wall -m64 -o a a.c && a
      a.c: In function ‘check_new’:
      a.c:38: warning: '0' flag used with ‘%p’ printf format
      a.c:38: warning: '0' flag used with ‘%p’ printf format
      address: 0x0000000001e240 slot:            (nil) byte:  3c4 bit:    4 nop: 0
      address: 0x0000002001e240 slot: 0x00000000000100 byte:  3c4 bit:    4 nop: 0
      address: 0x0000004001e240 slot: 0x00000000000200 byte:  3c4 bit:    4 nop: 0
      address: 0x0000006001e240 slot: 0x00000000000300 byte:  3c4 bit:    4 nop: 0
      address: 0x0000008001e240 slot: 0x00000000000400 byte:  3c4 bit:    4 nop: 0
      address: 0x000000a001e240 slot: 0x00000000000500 byte:  3c4 bit:    4 nop: 0
      address: 0x000000c001e240 slot: 0x00000000000600 byte:  3c4 bit:    4 nop: 0
      address: 0x000000e001e240 slot: 0x00000000000700 byte:  3c4 bit:    4 nop: 0
      address: 0x0000010001e240 slot: 0x00000000000800 byte:  3c4 bit:    4 nop: 0
      address: 0x0000012001e240 slot: 0x00000000000900 byte:  3c4 bit:    4 nop: 0
      address: 0x0000014001e240 slot: 0x00000000000a00 byte:  3c4 bit:    4 nop: 0
      address: 0x0000016001e240 slot: 0x00000000000b00 byte:  3c4 bit:    4 nop: 0
      address: 0x0000018001e240 slot: 0x00000000000c00 byte:  3c4 bit:    4 nop: 0
      address: 0x000001a001e240 slot: 0x00000000000d00 byte:  3c4 bit:    4 nop: 0
      address: 0x000001c001e240 slot: 0x00000000000e00 byte:  3c4 bit:    4 nop: 0
      address: 0x000001e001e240 slot: 0x00000000000f00 byte:  3c4 bit:    4 nop: 0
      address: 0x0000020001e240 slot: 0x00000000001000 byte:  3c4 bit:    4 nop: 0
      address: 0x0000022001e240 slot: 0x00000000001100 byte:  3c4 bit:    4 nop: 0
      address: 0x0000024001e240 slot: 0x00000000001200 byte:  3c4 bit:    4 nop: 0
      address: 0x0000026001e240 slot: 0x00000000001300 byte:  3c4 bit:    4 nop: 0
      address: 0x0000028001e240 slot: 0x00000000001400 byte:  3c4 bit:    4 nop: 0
      address: 0x000002a001e240 slot: 0x00000000001500 byte:  3c4 bit:    4 nop: 0
      address: 0x000002c001e240 slot: 0x00000000001600 byte:  3c4 bit:    4 nop: 0
      address: 0x000002e001e240 slot: 0x00000000001700 byte:  3c4 bit:    4 nop: 0
      address: 0x0000030001e240 slot: 0x00000000001800 byte:  3c4 bit:    4 nop: 0
      address: 0x0000032001e240 slot: 0x00000000001900 byte:  3c4 bit:    4 nop: 0
      address: 0x0000034001e240 slot: 0x00000000001a00 byte:  3c4 bit:    4 nop: 0
      address: 0x0000036001e240 slot: 0x00000000001b00 byte:  3c4 bit:    4 nop: 0
      address: 0x0000038001e240 slot: 0x00000000001c00 byte:  3c4 bit:    4 nop: 0
      address: 0x000003a001e240 slot: 0x00000000001d00 byte:  3c4 bit:    4 nop: 0
      address: 0x000003c001e240 slot: 0x00000000001e00 byte:  3c4 bit:    4 nop: 0
      address: 0x000003e001e240 slot: 0x00000000001f00 byte:  3c4 bit:    4 nop: 0
      

      Update: I couldn't get UINTPTR_C and UINTPTRxf configured automatically. I initially tried to use sizeof in conditional pre-processor directives (as shown below), but that's not allowed. Fixed.

      #if sizeof(uintptr_t) == 4 # define UINTPTR_C(p) UINT32_C(p) #elif sizeof(uintptr_t) == 8 # define UINTPTR_C(p) UINT64_C(p) #else # error uintptr_t size not handled. #endif #if sizeof(uintptr_t) == sizeof(unsigned) # define UINTPTRxf "x" #if sizeof(uintptr_t) == sizeof(unsigned long) # define UINTPTRxf "lx" #if sizeof(uintptr_t) == sizeof(unsigned long long) # define UINTPTRxf "Lx" #else # error uintptr_t size not handled. #endif
        I guess this is a situation it misses.

        You miss the point of compiler intrinsics.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
          A reply falls below the community's threshold of quality. You may see it by logging in.