```
union
{
Uint32 Integer;
Float32 Real;
} Field;
```

I have to use that union for a little IEEE trick, does that break strict aliasing? GCC is not throwing any warning(tried with GCC 4.5 and 4.6 even with pedantic strict aliasing, but As Far As I Know, GCC is not very good catching strict aliasing rules infringiments (lots of false positives/negatives).

```
Field A;
A.Integer = (Value1 & B) || Value2;
return A.Real;
```

That's the snippet I'm currently using, it seems working correctly without any warning, but maybe there can be side effects or undefined behaviours with certain compilers optimizations. So If that piece of code can be unsafe under certain conditions I will use some effort to remove that.

Also I assume that this piece of code will require moving data from standard registers to floating pointing registers on most modern CPUs (just curious about that), involving some extra cycles respect to older CPUs, correct?

The code above don't intend to be an optimization, so just don't derogate me for abusing optimizing, the code above was the simplest way for me to obtain a certain result (and luckily the simplest way seems also to be the fastest in my case!), if the result can't be safe, then I'll use a slower way.

Thanks in advance

It is UB (but doesn't require strict aliasing). Also, `union`

d data is always stored in memory by implementations, AFAIK, else would require knowing which register the source data came from, which means knowing the source type.

Aliasing via a union is defined in C but has undefined behaviour in C++; the undefined behaviour is equivalent to that which occurs when reading from an uninitialized variable (a lvalue-to-rvalue conversion).

Accordingly, the most likely way this would break is in the optimiser deciding to eliminate the read from the union, as it does not have a defined value. However, most C-and-C++ compilers are likely to give you the C behaviour as they need to support that anyway.

The safe way to alias the values is via bytewise copy e.g. `std::memcpy`

or `std::copy(reinterpret_cast<char *>(...), ...)`

. Alternatively, if you can compile your project in both C and C++ you could move the union aliasing code to a C source file and compile just that code as C.

Similar Questions

I read http://blog.qt.digia.com/blog/2011/06/10/type-punning-and-strict-aliasing/, and found this piece of code. QDataStream &QDataStream::operator>>(qint16 &i) { ... register uchar *p =

what is the best way to handle floating point numbers in XML? If I have, for instance: double a = 123.456; and I would like to keep it as <A> 123.456 </A> simply using ... myDoc.createTe

I've been reading up on the strict aliasing rules over the last week or so and ran into this article: Understanding C/C++ Strict Aliasing. The article goes through several ways two swap the halves of

I am trying to output flouting point numbers from c# in the format used by VRML 2.0. According to the vrml spec, this is double-precision floating point number in IEEE 1xxx floating point format (se

Given that GDI+ is a wrapper around GDI, how does it handle floating point values? I don't see any support for floating point co-ordinates in the GDI documentation.

I haven't yet created a program to see whether GCC will need it passed, When I do I'd like to know how I'd go about enabling strict floating point mode which will allow reproducible results between ru

I'm learning about programming languages now (BNFs, scanners, etc) and it seems weird that hexadecimal floating point constants in C require an exponent. From the C99 standard, §6.4.4.2 Floating cons

Does it violate strict aliasing rules to move items of any type around using uint32_t, then read them back? If so, does it also violate strict aliasing rules to memcpy from an array of uint32_ts to an

Division by zero in a C program results in abnormal termination with the error message Floating point exception (core dumped). This is unsurprising for floating point division, but why does it say thi

Why are there two different Writer-type monads in Haskell? Intuitively to me, reading strict writer monad means that the <> is strict, so that there's no thunk buildup in the log. However, loo

I have always heard registers as CPU registers. Does other devices or hardware also have its own registers like a decoder hardware, a display device, a DMA controller? If so, how are the values stored

Does anybody know if there are any? I would be interested in a .net floating point type with the range of a double (or greater) combined with the precision of a decimal (or greater). Are there any pla

What's the best way to emulate single-precision floating point in python? (Or other floating point formats for that matter?) Just use ctypes?

I'm trying to understand the impact of strict aliasing on performance in C99. My goal is to optimize a vector dot product, which takes up a large amount of time in my program (profiled it!). I thought

all I am trying to implement a floating point arithmetic library and I have trouble understanding the algorithm of subtracting floats. I have implemented addition succesfully and I thought that subtra

I don't understand how I can add in IEEE 754 Floating Point (mainly the re-alignment of the exponent) Also for rounding, how does the Guard, Round & Sticky come into play? How to do rounding in

I am struggling with implementing a shared memory buffer without breaking C99's strict aliasing rules. Suppose I have some code that processes some data and needs to have some 'scratch' memory to oper

In C++, what is the keyword used to refer to a 32-bit floating point value?:

I have a floating point addition that is somewhat likely to go wrong as the values have different magnitude, so quite a few significant digits are shifted out (possibly even all of them). In the scope

I got this problem I have to solve where I have to multiply to floating point numbers (16 bit), but I have no way of double checking it. Any help is immensely appreciated. Floating Point A: 0 11000 01

I have a template function operating on a floating point argument. The function is templated so that a caller can use either float, double or any other floating point data type. At one point in my cod

I'm having a problem with modulus on a floating point number in Python. This code: ... print '(' + repr(olddir) + ' + ' + repr(self.colsize) + ') % (math.pi*2) = ' + repr((olddir+self.colsize) ... Pr

The write function does not print a floating point number in the following code: #include <unistd.h> int main(){ float f = 4.5; write(1,&f,sizeof float); return 0; } This results in: �@ Wh

Having already read this question I'm reasonably certain that a given process using floating point arithmatic with the same input (on the same hardware, compiled with the same compiler) should be dete

I wonder if someone can clarify what the f behind a floating point number is used to signify? float myFloat = 12.0f; as apposed to: float myFloat = 12.0; I have seen this used many times but its p

Do the following class break the strict aliasing rule: template<typename T> class store { char m_data[sizeof(T)]; bool m_init; public: store() : m_init(false) {} store(const T &t) : init(tru

Note: This question has been edited because my issue became something completely different. You'll find the direct answer to the question What's the keyword for the maximal value for a floating point

I know it's an odd question, but does JavaScript have the capacity to work with double's as opposed to single floats? (64 bit floats vs. 32 bits.)

there exists a floating point system (B, T, L, U) (B=base, T=number of digits, L=lower exponent, U=upper exponent) where pi is exactly represented? And for the number 8/7?

When asking about common undefined behavior in C, souls more enlightened than I referred to the strict aliasing rule. What are they talking about?

I've got code that looks something like this, where addr is a sockaddr*: struct sockaddr_in *sin = (struct sockaddr_in *) addr; const char *IP=inet_ntoa(sin -> sin_addr); I believe this is very ty

I'm searching for a possibility to use wxSlider with floating point values, but cannot find any working examples. Here a subclass is proposed but I don't get if all methods must be re-defined? very gr

There are two fundamental types of microprocessor instructions: integer and floating-point. Accordingly, they are executed on an Integer Processing Unit and on a Floating-Point Processing Unit. That m

I don't know why the following code works fine, without gcc errors (-fstrict-aliasing -Wstrict-aliasing=1). #include <stdio.h> int main(void) { char n = 42; char *p = &n; int *q = (int *)p;

I need to be able to use floating-point arithmetic under my working dev environment in C (CPU : ~12 MHz Motorola 68000). The standard library is not present, meaning it is a bare-bones C and no - it

I got a timedelta object from the subtraction of two datetimes. I need this value as floating point for further calculations. All that i've found enables the calculation with floating-points, but the

'Strict aliasing' optimization needs special care from the source code, s.a. using a union instead of pointer casts. Is there a way to detect using preprocessor directives (#if/else) whether the compi

In IEEE 754 floating point, it is possible that a*(b-c) != a*b-a*c // a, b, c double So expansion of a product is not guaranteed to be equal to the unexpanded product. But what about this: a*(b1+b2+.

I am aware of the errors that can occur when doing conversions between floating point numbers and integers, but what about performance (please disregard the accuracy issues)? Does performance, in gene

I need to make a code in c++ that reads floating point numbers from a text file separated by commas and copies to a vector of floating point numbers.The text file contains several thousands of floatin

Given a floating-point number, I would like to separate it into a sum of parts, each with a given number of bits. For example, given 3.1415926535 and told to separate it into base-10 parts of 4 digits

May I know why ndarray allows floating point index accessing, and what does that mean? >>> wk1 = numpy.arange(10) >>> wk1[1:2.8] array([1]) >>> wk1 = [1,2,3,4,5,6,7,8,9,10]

With this program, I am intending to find the GCD of two numbers. But the result I get is Floating point exception(core dumped). What is the problem? The code I am trying to generate is int main() {

Jshint.com is giving the error: Line 36: var signin_found; Missing use strict statement.

I am used to compare the floating point with the following function. However, I just check that c++11 provides some floating point comparison function like isgreaterequal. My question is whether I sho

I am implementing a conventional (that means not fast), separated Fourier transform for images. I know that in floating point a sum over one period of sin or cos in equally spaced samples is not perfe

I see many questions about methods to detect if a value is a floating-point number, but none of the examples work on numbers like as 1.0, 5.0, etc. Is it possible to distinguish between numbers like 1

I need to convert both 32-bit and 64-bit unsigned integers into floating-point values in xmm registers. There are x86 instructions to convert signed integers into single and double precision floating-

Today I was tracking down why my program was getting some unexpected checksum-mismatch errors, in some code that I wrote that serializes and deserializes IEEE-754 floating-point values, in a format th

I am trying to parallelize an object localization algorithm on a GPU during my internship. But the OpenCL maximum floating point precision became quite problematic. The reference algorithm is impleme