Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7944 Discussions

icc: bug: __builtin_mul_overflow: wrong overflow value when casting type

KamilCuk
Beginner
1,795 Views
[Godbolt.com link](https://godbolt.org/z/aecPTaxc3). The following code:
 
#include <stdio.h>
#include <limits.h>
void test(unsigned a) {
unsigned c1;
int o1 = __builtin_mul_overflow((int)a, 1, &c1);
printf("%d %u %d\n", (int)a, c1, o1);
}
int main() {
test(-1);
test(INT_MIN);
}
 
When compiled with gcc produces:
 
-1 4294967295 1
-2147483648 2147483648 1
 

However, when compiled with icc 2021.1.2 produces:
 
-1 4294967295 0
-2147483648 2147483648 0
 
`(int)a` value is negative, so negative multiplied by 1 is still negative, so it
does not fit into unsigned type, so overflow should be set.
Seems like `__builtin_mul_overflow` ignores the cast and works as-if `(int)a` has
`unsigned` type. One workaround is just to use a temporary variable instead
of a cast:
 
int a1 = a;
int o1 = __builtin_mul_overflow(a1, 1, &c1);
Labels (1)
0 Kudos
4 Replies
VarshaS_Intel
Moderator
1,743 Views

Hi,


Thanks for reporting us.


We are working on this internally and will get back to you soon. Meanwhile, you can try using the Intel icx compiler to get the expected results.


Thanks & Regards

Varsha


0 Kudos
Viet_H_Intel
Moderator
1,724 Views

Hi,


Can you use a workaround or icx/icpx? I would recommend using icx/icpx because at some point, the classic C/C++ compilers will enter “Legacy Product Support” mode signaling the end of regular updates to the classic compiler base, and they will no longer appear in oneAPI toolkits.


Please see the article for more details:

https://www.intel.com/content/www/us/en/developer/articles/technical/adoption-of-llvm-complete-icx.html


Thanks,


0 Kudos
KamilCuk
Beginner
1,662 Views

Hey, thanks for responses!

> Can you use a workaround or icx/icpx?

 

Well, I am not a user of icc, and I'm not using icc in any production project. I only found the problem, when doing a research project with no current practical use. I was only testing "if it works" on icc compiler and stumbled across the problem when unit-testing. I was implementing ckd library https://gitlab.com/Kamcuk/ckd based on proposition to the standard http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2792.pdf . If it's for example not "worth fixing", that's "fine for me". I have a path in the library that does not use any `__builtin` functions, and it works fine on icc anyway.

> I would recommend using icx/icpx because at some point, the classic C/C++ compilers

 

Sure, `icx -xc` works fine.

0 Kudos
Viet_H_Intel
Moderator
1,373 Views

As a workaround in icc, a temporary variable can be used:

int x = a;

int o1 = __builtin_mul_overflow(x, 1, &c1);


We are going to close this issue as "wont fix".


Sorry for the inconvenience.



0 Kudos
Reply