Skip to contentSkip to conversation

New: Undefined operation in tgsi_ureg.c left shift of 1 by 31 places cannot be represented in type 'int'

Bug 80266 [email protected] 1 participant 11 messages Jun 20, 2014 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

https://bugs.freedesktop.org/show_bug.cgi?id=80266

Priority: medium
Bug ID: 80266
Assignee: [email protected]
Summary: Undefined operation in tgsi_ureg.c left shift of 1 by
31 places cannot be represented in type 'int'
Severity: minor
Classification: Unclassified
OS: Linux (All)
Reporter: [email protected]
Hardware: x86-64 (AMD64)
Status: NEW
Version: 10.2
Component: Drivers/DRI/nouveau
Product: Mesa

In tgsi_ureg.c:1498
"if (ureg->vs_inputs[i/32] & (1 << (i%32))) {"
when i==31 then 1 << 31 may be computed.
With gcc option -std=c99 this is wrong because the result cannot be int.

Perhaps the right instruction is
"if (ureg->vs_inputs[i/32] & ((unsigned) 1 << (i%32))) {"

Signature · 2 lines
You are receiving this mail because:
You are the assignee for the bug.

_______________________________________________
Nouveau mailing list [email protected]
http://lists.freedesktop.org/mailman/listinfo/nouveau

A message that is not in the archive (sent off-list or not yet received): bug-80266-8800-V0hAGp6uBxMKqLRl/[email protected]/

#2 lore raw

[Bug 80266] Undefined operation in tgsi_ureg.c left shift of 1 by 31 places cannot be represented in type 'int'

https://bugs.freedesktop.org/show_bug.cgi?id=80266

--- Comment #1 from Ilia Mirkin <imirkin-FrUbXkNCsVf2fBVCVOL8/[email protected]> --- (In reply to comment #0)

Quoted 7 lines

In tgsi_ureg.c:1498
"if (ureg->vs_inputs[i/32] & (1 << (i%32))) {"
when i==31 then 1 << 31 may be computed.
With gcc option -std=c99 this is wrong because the result cannot be int.

Perhaps the right instruction is
"if (ureg->vs_inputs[i/32] & ((unsigned) 1 << (i%32))) {"

Where is -std=c99 coming from? Also I'd guess the better thing is 1U instead of (unsigned)1 :)

Feel free to send a patch btw.

Signature · 2 lines
You are receiving this mail because:
You are the assignee for the bug.

_______________________________________________
Nouveau mailing list [email protected]
http://lists.freedesktop.org/mailman/listinfo/nouveau

#3 lore raw

[Bug 80266] Undefined operation in tgsi_ureg.c left shift of 1 by 31 places cannot be represented in type 'int'

In reply to a message not in the archive

https://bugs.freedesktop.org/show_bug.cgi?id=80266

--- Comment #2 from Vittorio <[email protected]> --- Yes I agree that 1U is better. My C knowledge is limited, I am more interested into Fortran.

-std=c99 is in CFLAGS in the Makefile produced by configure and configure.ac.

Not much familiar with patch, please help yourself.

Signature · 2 lines
You are receiving this mail because:
You are the assignee for the bug.

_______________________________________________
Nouveau mailing list [email protected]
http://lists.freedesktop.org/mailman/listinfo/nouveau

#4 lore raw

[Bug 80266] Undefined operation in tgsi_ureg.c left shift of 1 by 31 places cannot be represented in type 'int'

In reply to a message not in the archive

https://bugs.freedesktop.org/show_bug.cgi?id=80266

--- Comment #3 from Vittorio <[email protected]> --- For the same reason I believe that in uniforms.c:79
"if (prog->SamplersUsed & (1 << s)) {" should be
"if (prog->SamplersUsed & (1U << s)) {".
This is running glxgears.

Signature · 2 lines
You are receiving this mail because:
You are the assignee for the bug.

_______________________________________________
Nouveau mailing list [email protected]
http://lists.freedesktop.org/mailman/listinfo/nouveau

#5 lore raw

[Bug 80266] Undefined operation in tgsi_ureg.c left shift of 1 by 31 places cannot be represented in type 'int'

In reply to a message not in the archive

https://bugs.freedesktop.org/show_bug.cgi?id=80266

--- Comment #4 from Vittorio <[email protected]> --- Also in matrix.c:1156 the line
"#define ONE(x) (1 <<(x+16))"
should be
"#define ONE(x) (1U<<(x+16))"
because ONE(15) later, as at line 1226, produces 16 bits left shift of 65536, which is bigger than int.
This is running teapot.

Signature · 2 lines
You are receiving this mail because:
You are the assignee for the bug.

_______________________________________________
Nouveau mailing list [email protected]
http://lists.freedesktop.org/mailman/listinfo/nouveau

#6 lore raw

[Bug 80266] Undefined operation in tgsi_ureg.c left shift of 1 by 31 places cannot be represented in type 'int'

In reply to a message not in the archive

https://bugs.freedesktop.org/show_bug.cgi?id=80266

--- Comment #5 from Ilia Mirkin <imirkin-FrUbXkNCsVf2fBVCVOL8/[email protected]> --- I'm not sure why you're filing these against nouveau... I'm sure there are _tons_ of these throughout mesa...

Does gcc warn about this? Are you _sure_ that (signed int)1 << 31 is illegal in C99?

Signature · 2 lines
You are receiving this mail because:
You are the assignee for the bug.

_______________________________________________
Nouveau mailing list [email protected]
http://lists.freedesktop.org/mailman/listinfo/nouveau

#7 lore raw

[Bug 80266] Undefined operation in tgsi_ureg.c left shift of 1 by 31 places cannot be represented in type 'int'

In reply to a message not in the archive

https://bugs.freedesktop.org/show_bug.cgi?id=80266

--- Comment #6 from Patrick Baggett <[email protected]> --- I'm sure that technically shifting any bits into the sign bit of a signed
number produces undefined behavior according to C99, so yes, (1<<31)
technically is undefined. As a matter of practicality, on basically any CPU, 1<<31 will produce a valid negative number, but the compiler sees it as trying
to assign the constant 2147483648 to a 32-bit signed container, which is
outside of the 2^31-1 range that it can accept.

In the C abstract machine, this would be a serious problem. Obviously you can make such an assignment in hardware, but the result (when interpreted as signed) is not a value of 2147483648, but a value of -2147483648, which is why the compiler gives a warning. It's weird to read that (1<<31) is less than 0. If you don't really mean to assign a value of -2147483648 or 2147483648 and just want a bit pattern, using unsigned is almost always the way to go.

I know that Solaris Studio happens to give a warning for the same case, if that means anything.

Signature · 2 lines
You are receiving this mail because:
You are the assignee for the bug.

_______________________________________________
Nouveau mailing list [email protected]
http://lists.freedesktop.org/mailman/listinfo/nouveau

#8 lore raw

[Bug 80266] Undefined operation in tgsi_ureg.c left shift of 1 by 31 places cannot be represented in type 'int'

In reply to a message not in the archive

https://bugs.freedesktop.org/show_bug.cgi?id=80266

--- Comment #7 from Vittorio <[email protected]> --- In m_matrix.c:1215
" if (m[15] == 1.0F) mask |= (1<<31);"
should be
" if (m[15] == 1.0F) mask |= (1U<<31);"
because 1<<31 does not fit in int.
From teapot.

Signature · 2 lines
You are receiving this mail because:
You are the assignee for the bug.

_______________________________________________
Nouveau mailing list [email protected]
http://lists.freedesktop.org/mailman/listinfo/nouveau

#9 lore raw

[Bug 80266] Undefined operation in tgsi_ureg.c left shift of 1 by 31 places cannot be represented in type 'int'

In reply to a message not in the archive

https://bugs.freedesktop.org/show_bug.cgi?id=80266

--- Comment #8 from Vittorio <[email protected]> --- (In reply to comment #5)

Quoted 5 lines

I'm not sure why you're filing these against nouveau... I'm sure there are _tons_ of these throughout mesa...

Does gcc warn about this? Are you _sure_ that (signed int)1 << 31 is illegal in C99?

Only now I am reading this note.
I compiled mesa with gcc -fsanitize=undefined option, gcc runtime is issuing several, not tons, of runtime error messages, it seems to me that (signed int) 1 << 31 is undefined if computed as int.
The runtime error messages only appear with the -std=c99 option.

Signature · 2 lines
You are receiving this mail because:
You are the assignee for the bug.

_______________________________________________
Nouveau mailing list [email protected]
http://lists.freedesktop.org/mailman/listinfo/nouveau

#10 lore raw

[Bug 80266] Undefined operation in tgsi_ureg.c left shift of 1 by 31 places cannot be represented in type 'int'

In reply to a message not in the archive

https://bugs.freedesktop.org/show_bug.cgi?id=80266

--- Comment #9 from Vittorio <[email protected]> --- (In reply to comment #6)

Quoted 15 lines

I'm sure that technically shifting any bits into the sign bit of a signed number produces undefined behavior according to C99, so yes, (1<<31) technically is undefined. As a matter of practicality, on basically any CPU, 1<<31 will produce a valid negative number, but the compiler sees it as trying to assign the constant 2147483648 to a 32-bit signed container, which is outside of the 2^31-1 range that it can accept.

In the C abstract machine, this would be a serious problem. Obviously you can make such an assignment in hardware, but the result (when interpreted as signed) is not a value of 2147483648, but a value of -2147483648, which is why the compiler gives a warning. It's weird to read that (1<<31) is less than 0. If you don't really mean to assign a value of -2147483648 or 2147483648 and just want a bit pattern, using unsigned is almost always the way to go.

Quoted 2 lines

I know that Solaris Studio happens to give a warning for the same case, if that means anything.

Thanks for the appreciated explanation.
Maybe substituting 1<<31 with 1U<<31 clears any undefined behaviour on the many architectures where mesa runs.

Signature · 2 lines
You are receiving this mail because:
You are the assignee for the bug.

_______________________________________________
Nouveau mailing list [email protected]
http://lists.freedesktop.org/mailman/listinfo/nouveau

#11 lore raw

[Bug 80266] Many instances of 1<<31, which is undefined in C99

In reply to a message not in the archive

https://bugs.freedesktop.org/show_bug.cgi?id=80266

Ilia Mirkin <imirkin-FrUbXkNCsVf2fBVCVOL8/[email protected]> changed:

What |Removed |Added
----------------------------------------------------------------------------

           Assignee|[email protected] |mesa-dev-PD4FTy7X32lNgt0PjOBp9/[email protected]
                   |rg                          |org
            Summary|Undefined operation in      |Many instances of 1<<31,
                   |tgsi_ureg.c left shift of 1 |which is undefined in C99
                   |by 31 places cannot be      |
                   |represented in type 'int'   |
          Component|Drivers/DRI/nouveau         |Mesa core
Signature · 2 lines
You are receiving this mail because:
You are the assignee for the bug.

_______________________________________________
Nouveau mailing list [email protected]
http://lists.freedesktop.org/mailman/listinfo/nouveau