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

Post Reply

Topic author
jonesd
Contributor
Posts: 20
Joined: Mon Aug 09, 2021 7:59 pm
Reputation: 0
Status: Offline

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

Post by jonesd » Sun Dec 04, 2022 11:06 pm

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;
}


jreagan
VSI Expert
Contributor
Posts: 13
Joined: Tue Dec 01, 2020 8:40 am
Reputation: 0
Status: Offline

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

Post by jreagan » Mon Dec 05, 2022 9:27 am

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".


Topic author
jonesd
Contributor
Posts: 20
Joined: Mon Aug 09, 2021 7:59 pm
Reputation: 0
Status: Offline

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

Post by jonesd » Mon Dec 05, 2022 1:23 pm

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.

Post Reply