Page 1 of 1

Pointer comparisons on X86_64 different between 32 and 64 bit pointer sizes.

Posted: Sun Dec 04, 2022 11:06 pm
by jonesd
The program below using the X86 cross compiler (VSI C X7.4-547) gives different results than on Alpha or IA64. Relation comparisons between pointers treats the pointer values as signed if using 32 bit pointers and unsigned if using 64 bit pointers. On the other architecures, the pointers are unsigned in all cases.

Code: Select all

/*
 * Test if pointer comparison treats pointers as signed, specifically if
 * ((char *)(-1)) is the largest address.
 */
#include <stdio.h>
#include <stdlib.h>

#pragma required_pointer_size save
#pragma required_pointer_size 32
static void test_short ( void )
{
    /* Test 32 bit pointers */
    char buffer[500], *p, *p_max;

    printf ( "\nShort pointer size: %d\n", sizeof(p) );
    p = buffer; p_max = (char *)(-1);
    printf ( "   stack address %x is %s than %x\n", 
	p, (p<p_max) ? "less" : "NOT less", p_max );
}

#pragma required_pointer_size 64
static void test_long ( void )
{
    /* Test 64 bit pointers */
    char buffer[500], *p, *p_max;

    printf ( "\nLong pointer size: %d\n", sizeof(p) );
    p = buffer; p_max = (char *)(-1);
    printf ( "   stack address %llx is %s than %llx\n", 
	p, (p<p_max) ? "less" : "NOT less", p_max );
}
#pragma required_pointer_size restore

int main ( int argc, char **argv )
{
    test_short();
    test_long();
    return 1;
}

Re: Pointer comparisons on X86_64 different between 32 and 64 bit pointer sizes.

Posted: Mon Dec 05, 2022 9:27 am
by jreagan
Can you submit a report via the service platform so we can track that? Yes, it should match the Itanium behavior.

As for whether addresses are signed or unsigned is a somewhat different question. On OpenVMS (Alpha, Itanium, and x86) a 32-bit address is always sign-extended before its use. The underlying hardware architectures only have 64-bit addresses (we always run x86-64 in 64-bit mode) so it up to the software to decide what to do with 32-bit "pointers".

Re: Pointer comparisons on X86_64 different between 32 and 64 bit pointer sizes.

Posted: Mon Dec 05, 2022 1:23 pm
by jonesd
I don't have access to the service platform. I've thought about joining the ISV program, but haven't convinced myself the reward would be worth the effort.

The way I discovered this inconsistency was by chasing down why the ".schema tablename" command fails on the 32-bit build of SQLite on X86.