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
In reply to Re^3: 64-bit *nix/gcc expertise required
by ikegami
in thread 64-bit *nix/gcc expertise required (THIS IS NOT OFF TOPIC!)
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |