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

Optimization problem with int to long int cast

Axel_P_
Beginner
268 Views

An integer overflow may be ignored if an int value is assigned to a long int variable. The problem does not occur if optimization is turned off (compiler version 13.1) or reduced to -O1 (compiler version 11.1). Operating system Linux SLED11SP3. gcc produces the expected result with all optimization levels.

Smallest code example to reproduce:

#include <stdio.h>

 

int main( int argc, char** argv)

{

  int i,i0;

  unsigned int ui;

  long int l;

  unsigned long int ul;

  i0=1200000000;

  i=2*i0;

  ui=2*i0;

  l=i;

  ul=i;

  printf(" int=%d\n unsigned int=%u\n", i,ui);

  printf("source int\n long int=%ld\n unsigned long int=%lu\n", l,ul);

  l=ui;

  ul=ui;

  printf("source unsigned int\n long int=%ld\n unsigned long int=%lu\n", l,ul);

}

 

progs> icc --version

icc (ICC) 13.1.2 20130514

Copyright (C) 1985-2013 Intel Corporation.  All rights reserved.

 

progs> icc iccbug.c;./a.out

 int=-1894967296

 unsigned int=2400000000

source int

 long int=2400000000

 unsigned long int=2400000000

source unsigned int

 long int=2400000000

 unsigned long int=2400000000

 

progs> icc -O0 iccbug.c;./a.out

 int=-1894967296

 unsigned int=2400000000

source int

 long int=-1894967296

 unsigned long int=18446744071814584320

source unsigned int

 long int=2400000000

 unsigned long int=2400000000

0 Kudos
1 Reply
KitturGanesh
Employee
268 Views

Hi Phillip, 

This is a bug and I've escalated this issue to the product team and will keep you updated accordingly, thanks.

_Kittur

0 Kudos
Reply