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

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.
  • Comment on Re^2: 64-bit *nix/gcc expertise required

Replies are listed 'Best First'.
Re^3: 64-bit *nix/gcc expertise required
by ikegami (Patriarch) on Dec 24, 2010 at 20:17 UTC

    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.

        Quite the contrary. If printf wasn't an instrinsic, the compiler couldn't even give you warnings that you're risking getting garbage or causing segfaults.

        • Implicit coercion from int to pointer: %p expects a pointer, but you pass it an ptrdiff_t. (twice)
        • Data doesn't match format specifier: %x expects (a pointer to) an int or unsigned int, but you pass it (a pointer to) a ptrdiff_t (thrice).

        The other warning is for void main. I guess that's no longer kosher?

        So the minimal change is:

        • Add casts to the parameters of printf to they match the format string.
        • Adjust the return value of main.
        • Avoid MS-specific extensions.
        #include <stdint.h> #include <stdio.h> #define ALIGN_BITS ( sizeof(void*) >> 1 ) #define BIT_BITS 3 #define BYTE_BITS 14 int check_new( uintptr_t p ) { uintptr_t slot = ( p >> (BYTE_BITS + BIT_BITS + ALIGN_BITS) ); uintptr_t byte = ( p >> (ALIGN_BITS + BIT_BITS)) & 0x00003fffULL; uintptr_t bit = ( p >> ALIGN_BITS) & 0x00000007ULL; uintptr_t nop = p & 0x3ULL; printf( "address: %p slot: %p byte: %4x bit: %4x nop:%x\n", (void*)p, (void*)slot, (unsigned)byte, (unsigned)bit, (unsigne +d)nop ); return 1; } int main( void ) { uintptr_t p; for( p = 123456ULL; p < ( 1ULL << 31 ); p += ( 1ULL << 29 ) ) { check_new( p ); } return 0; }
        A reply falls below the community's threshold of quality. You may see it by logging in.