public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Ref:unsigned int 2 ulong type promotion issue in GCC!
@ 2020-05-24 13:31 Thamilarasu Kandasamy (thamil)
  2020-05-24 13:50 ` Liu Hao
  0 siblings, 1 reply; 2+ messages in thread
From: Thamilarasu Kandasamy (thamil) @ 2020-05-24 13:31 UTC (permalink / raw)
  To: gcc-help

Hi

I am observing a unsigned int to unsigned long type promotion issue which I described with a sample program.

gcc version 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)

The output of program is here


Result with signed   int:  0x7ffee7fd3b0c      --> int to unsigned long type promotion happens

Result with unsigned int:  0xe7fd3b0c          --> in the expression involving unsigned int and long, there is no type promotion, hence truncated output

Result with type casting:  0x7ffee7fd3b0c.     --> Type casting unsigned int to unsigned long produces result as expected



In the expression involving int & ulong operands, promotion of int to ulong happen as expected.

But the same doesn’t happen in expression involving uint and ulong.

If there is an explicit cast of unsigned to ulong, the result is  as expected.



As per the type promotion rules, unsigned int to unsigned long promotion should have happened.



Will appreciate your input on whether this behaviour is as expected or a bug ? Or am I missing something here ?



#include  <stdio.h>



int main(int argc, char *argv[])

{

    int var;



    void *result1;

    void *result2;

    void *result3;



    int size = 100;

    unsigned int usize = 100;



    result1 = (void *) (((uintptr_t) &var) & ~(size - 1));

    result2 = (void *) (((uintptr_t) &var) & ~(usize - 1));

    result3 = (void *) (((uintptr_t) &var) & ~((unsigned long  int)usize - 1));



    printf("\nResult with signed   int:  %p", result1);

    printf("\nResult with unsigned int:  %p", result2);

    printf("\nResult with type casting:  %p", result3);



    printf("\n");



    return (0);

}




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Ref:unsigned int 2 ulong type promotion issue in GCC!
  2020-05-24 13:31 Ref:unsigned int 2 ulong type promotion issue in GCC! Thamilarasu Kandasamy (thamil)
@ 2020-05-24 13:50 ` Liu Hao
  0 siblings, 0 replies; 2+ messages in thread
From: Liu Hao @ 2020-05-24 13:50 UTC (permalink / raw)
  To: Thamilarasu Kandasamy (thamil), gcc-help


[-- Attachment #1.1: Type: text/plain, Size: 900 bytes --]

在 2020/5/24 21:31, Thamilarasu Kandasamy (thamil) via Gcc-help 写道:
> 
>     int size = 100;
>     unsigned int usize = 100;
> 
>     result1 = (void *) (((uintptr_t) &var) & ~(size - 1));
>     result2 = (void *) (((uintptr_t) &var) & ~(usize - 1));
>     result3 = (void *) (((uintptr_t) &var) & ~((unsigned long  int)usize - 1));
> 

`result1` is bitwise AND `&var` with `(uintptr_t)~(int)99` which is
`(uintptr_t)(int)-100` which is probably `0xFFFFFFFFFFFFFF9C` on a
64-bit machine.

`result2` is bitwise AND `&var` with `(uintptr_t)~(unsigned)99` which is
likely `(uintptr_t)(unsigned)4294967196` which is `0xFFFFFF9C` on a
64-bit machine. So higher half is truncated as expected.

`result3` is bitwise AND `&var` with `(uintptr_t)~(unsigned long)99`
which is probably `0xFFFFFFFFFFFFFF9C` on a 64-bit machine.


There is no bug.


-- 
Best regards,
LH_Mouse


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-05-24 13:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-24 13:31 Ref:unsigned int 2 ulong type promotion issue in GCC! Thamilarasu Kandasamy (thamil)
2020-05-24 13:50 ` Liu Hao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).