473,511 Members | 16,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Aliassing and reinterpret_cast and optimization

Hi,

I was wondering what the main reason is why reinterpret_cast fails to work
as expected when using optimizations. Here is the simple example code which
fail to give the correct result when optimizing.

#include <iostream>
int main() {
long D1 = 0xbcfbc4f0d9b65179;
long D2 = 0xbcfbc4f0d9b65042;

std::cout.setf(std::ios::showbase | std::ios::showpos);
std::cout.setf(std::ios::hex,std::ios::basefield);
std::cout.setf(std::ios::scientific,std::ios::floa tfield);
std::cout.setf(std::ios::internal,std::ios::adjust field);
std::cout.precision(20);

double E1 = *reinterpret_cast<double *(&D1);
double E2 = *reinterpret_cast<double *(&D2);

std::cout << E1 << "\t" << D1 << std::endl;
std::cout << E2 << "\t" << D2 << std::endl;

return 0;
}

This gives me the output when compiled without optimization flags :
$ g++ foo.cpp
$ ./a.out
-6.16602326664765606567e-15 0xbcfbc4f0d9b65179
-6.16602326664741072993e-15 0xbcfbc4f0d9b65042

while compiled with optimization (-O3)
$ g++ -O3 foo.cpp
$ ./a.out
+0.00000000000000000000e+00 0xbcfbc4f0d9b65179
-6.16602326664741072993e-15 0xbcfbc4f0d9b65042

Although this is only a minor difference here, it becomes ludricous when
doing some simple math operations. (Example code below). Identical results
are obtained when using pointer casts directly, i.e. *(double *) &D1 for
example.

I have read some stuff about aliassing (i.e.
<http://mail-index.netbsd.org/tech-kern/2003/08/11/0001.html>) And I was
wondering if this is the same problem here, eventhough the standard of C++
does not say anything about this (or I just don't find it, sec 5.2.10). If
this is the same problem here, is there a way to solve it without using
the gnu extension?

Thanks

Ugly piece of code which goes completely bizerque when optimizing.

#include <iostream>
#include <cmath>
#include <float.h>

int main(void) {

int A = 0x40000000;
float B = *reinterpret_cast<float *>(&A);

std::cout.setf(std::ios::showbase | std::ios::showpos);
std::cout.setf(std::ios::hex,std::ios::basefield);
std::cout.setf(std::ios::scientific,std::ios::floa tfield);
std::cout.setf(std::ios::internal,std::ios::adjust field);
std::cout.precision(20);

for (int i = -3; i <= 3; ++i) {
int C = A+i;
B = *reinterpret_cast<float *>(&C);
std::cout << B << "\t" << A+i << std::endl;
}

int C = -A;
B = *reinterpret_cast<float *>(&C);
std::cout << B << "\t" << C << std::endl;
C = A*2;
B = *reinterpret_cast<float *>(&C);
std::cout << B << "\t" << C << std::endl;
B = 40;
C = *reinterpret_cast<int *>(&B);
std::cout << B << "\t" << C << std::endl;
B = 1;
C = *reinterpret_cast<int *>(&B);
std::cout << B << "\t" << C << std::endl;
B = 0.1;
C = *reinterpret_cast<int *>(&B);
std::cout << B << "\t" << C << std::endl;
long D1 = 0xbcfbc4f0d9b65179;
long D2 = 0xbcfbc4f0d9b65042;
std::cout.setf(std::ios::showbase | std::ios::showpos);
std::cout.setf(std::ios::hex,std::ios::basefield);
std::cout.setf(std::ios::scientific,std::ios::floa tfield);
std::cout.setf(std::ios::internal,std::ios::adjust field);
std::cout.precision(20);

double E1 = *reinterpret_cast<double *(&D1);
double E2 = *reinterpret_cast<double *(&D2);
double E3 = fabs(E1-E2);
long D3 = *reinterpret_cast<long *>(&E3);

std::cout << E1 << "\t" << D1 << std::endl;
std::cout << E2 << "\t" << D2 << std::endl;
std::cout << E3 << "\t" << D3 << "\t" << D1-D2 << std::endl;
std::cout << E3/E2 << "\t" << E3/E2/DBL_EPSILON << std::endl;

unsigned long F = D3;
F = F << 1;
F = F >53;
std:: cout << F << std::endl;

long GF = 0x42270feeda10f98e;
long G = GF + 512;
long HF = 0x413cd26c40d51215;
double H = (*reinterpret_cast<double *>(&G)) - (*reinterpret_cast<double
*>(&GF));
double I = H/(*reinterpret_cast<double *(&HF));

std::cout << GF << " " << G << " " << H << " " << I << " " <<
I/DBL_EPSILON << std::endl;

return 0;
}
--
"You're very sure of your facts, " he said at last, "I
couldn't trust the thinking of a man who takes the Universe
- if there is one - for granted. "
Jun 27 '08 #1
2 2651
On Apr 15, 3:14 pm, ciccio <nos...@thismail.comwrote:
Hi,

I was wondering what the main reason is why reinterpret_cast fails to work
as expected when using optimizations. Here is the simple example code which
fail to give the correct result when optimizing.

#include <iostream>
int main() {
long D1 = 0xbcfbc4f0d9b65179;
long D2 = 0xbcfbc4f0d9b65042;

std::cout.setf(std::ios::showbase | std::ios::showpos);
std::cout.setf(std::ios::hex,std::ios::basefield);
std::cout.setf(std::ios::scientific,std::ios::floa tfield);
std::cout.setf(std::ios::internal,std::ios::adjust field);
std::cout.precision(20);

double E1 = *reinterpret_cast<double *(&D1);
double E2 = *reinterpret_cast<double *(&D2);

std::cout << E1 << "\t" << D1 << std::endl;
std::cout << E2 << "\t" << D2 << std::endl;

return 0;

}

This gives me the output when compiled without optimization flags :
$ g++ foo.cpp
$ ./a.out
-6.16602326664765606567e-15 0xbcfbc4f0d9b65179
-6.16602326664741072993e-15 0xbcfbc4f0d9b65042

while compiled with optimization (-O3)
$ g++ -O3 foo.cpp
$ ./a.out
+0.00000000000000000000e+00 0xbcfbc4f0d9b65179
-6.16602326664741072993e-15 0xbcfbc4f0d9b65042

Although this is only a minor difference here, it becomes ludricous when
doing some simple math operations. (Example code below). Identical results
are obtained when using pointer casts directly, i.e. *(double *) &D1 for
example.

I have read some stuff about aliassing (i.e.
<http://mail-index.netbsd.org/tech-kern/2003/08/11/0001.html>) And I was
wondering if this is the same problem here, eventhough the standard of C++
does not say anything about this (or I just don't find it, sec 5.2.10). If
this is the same problem here, is there a way to solve it without using
the gnu extension?

Thanks

Ugly piece of code which goes completely bizerque when optimizing.

#include <iostream>
#include <cmath>
#include <float.h>

int main(void) {

int A = 0x40000000;
float B = *reinterpret_cast<float *>(&A);
Using reinterpret cast to read from a variable with a different type
than the one used to store into it is undefined behaviour (check on
the standard the exact . Some compilers support type punning via an
union as an extension. The only 'portable' way to do that is to use
memcpy: memcpy(&B, &A, sizeof(float). (and making some assumptions
about int and float sizes and representations, of course).

--
gpd
Jun 27 '08 #2
On 15 avr, 17:47, gpderetta <gpdere...@gmail.comwrote:
On Apr 15, 3:14 pm, ciccio <nos...@thismail.comwrote:
I was wondering what the main reason is why reinterpret_cast
fails to work as expected when using optimizations. Here is
the simple example code which fail to give the correct
result when optimizing.
#include <iostream>
int main() {
long D1 = 0xbcfbc4f0d9b65179;
long D2 = 0xbcfbc4f0d9b65042;
std::cout.setf(std::ios::showbase | std::ios::showpos);
std::cout.setf(std::ios::hex,std::ios::basefield);
std::cout.setf(std::ios::scientific,std::ios::floa tfield);
std::cout.setf(std::ios::internal,std::ios::adjust field);
std::cout.precision(20);
double E1 = *reinterpret_cast<double *(&D1);
double E2 = *reinterpret_cast<double *(&D2);
std::cout << E1 << "\t" << D1 << std::endl;
std::cout << E2 << "\t" << D2 << std::endl;
return 0;
}
This gives me the output when compiled without optimization flags :
$ g++ foo.cpp
$ ./a.out
-6.16602326664765606567e-15 0xbcfbc4f0d9b65179
-6.16602326664741072993e-15 0xbcfbc4f0d9b65042
while compiled with optimization (-O3)
$ g++ -O3 foo.cpp
$ ./a.out
+0.00000000000000000000e+00 0xbcfbc4f0d9b65179
-6.16602326664741072993e-15 0xbcfbc4f0d9b65042
Although this is only a minor difference here, it becomes
ludricous when doing some simple math operations.
I'd consider that an error in the compiler (from a quality of
implementation point of view), in this limited case, since the
compiler can easily see the cast, and should turn off all
optimization involving pointers when it does.

Still, you're creating an alias. The standard is rather clear
that accessing an object through an lvalue whose type is neither
the type of the actual object, nor a character type, is
undefined behavior. The motivation for this is, of course, that
aliasing is the bane of
(Example code below). Identical results
are obtained when using pointer casts directly, i.e. *(double *) &D1 for
example.
I have read some stuff about aliassing (i.e.
<http://mail-index.netbsd.org/tech-kern/2003/08/11/0001.html>) And I was
wondering if this is the same problem here, eventhough the standard of C++
does not say anything about this (or I just don't find it, sec 5.2.10). If
this is the same problem here, is there a way to solve it without using
the gnu extension?
Thanks
Ugly piece of code which goes completely bizerque when optimizing.
#include <iostream>
#include <cmath>
#include <float.h>
int main(void) {
int A = 0x40000000;
float B = *reinterpret_cast<float *>(&A);

Using reinterpret cast to read from a variable with a different type
than the one used to store into it is undefined behaviour (check on
the standard the exact . Some compilers support type punning via an
union as an extension. The only 'portable' way to do that is to use
memcpy: memcpy(&B, &A, sizeof(float). (and making some assumptions
about int and float sizes and representations, of course).

--
gpd
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
12763
by: Suzanne Vogel | last post by:
I'd like to convert a double to a binary representation. I can use the "&" bit operation with a bit mask to convert *non* float types to binary representations, but I can't use "&" on doubles. ...
13
3067
by: Jakob Bieling | last post by:
Hi, I am trying to determine the endianness of the system as follows: int i = 1; bool is_little = *reinterpret_cast <char*> (&i) != 0; But now I was asking myself, if this use of...
11
3759
by: Someonekicked | last post by:
I want to save tables of integers to files. One way to write the cells as fixed size in the file, is to use reinterpret_cast. Is that a bad choice, good choice? I remember once before I posted a...
2
2413
by: Taran | last post by:
Hi All, I was trying some code which essentially does what the function 'func' here does. To simplify the things and make a consise post I have created a dummy app to show my problem. The issue...
7
10880
by: Peter | last post by:
I never used reinterpret_cast -- probably because I don't know what it means. Can somebody enlighten me? I looked into Stroustrup's "The annoted C++ reference manual" -- but this was no help....
2
2647
by: Lucy Ludmiller | last post by:
I have two classes: class base { ... }; and class derived : public base { ... }; I have a function :
4
2655
by: tkirankumar | last post by:
Hi all, This is regarding the issue I am facing while porting the my application from SuSe Linux to Sun Solaris. The Existing system details: Itanium boxes 1mhz 4 processor 8 gb machines with...
27
4280
by: Noah Roberts | last post by:
What steps do people take to make sure that when dealing with C API callback functions that you do the appropriate reinterpret_cast<>? For instance, today I ran into a situation in which the wrong...
2
2940
by: jasm | last post by:
hello everybody! I have used reinterpret_cast for interpret a class object as a char*. This is the object: template<class T> class Coordinates { public: T *x; T *y;
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7508
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.