473,396 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

swapd() undefined behavior?

Hi

in the following source code, I think swap32() itself should be ok,
while I am unsure if swapd() and swapd_wrap() invoke undefined
behavior.
The doubt is reinforced by gcc -O3 giving strange results with the
latter.
Any comments?

Specifically, consider a function that receives a double parameter and
returns a
double parameter like:

double func( double d )
{
// return d after whatever bit manipulations imaginable
}

that is used like:

double d1=SOME_VALUE, d2;

d2 = func(d1);

Will the parameter passing and the assignment in this code have to
preserve the
bit pattern? Even if d1 or d2 are unnormalized or NaN?

thanks for your attention
Herbert

/*
swapd.c

programs compiled with gcc 3.3.3 with
gcc -std=c99 -O3 -pedantic -W -Wall swapd.c -o swapd
and
gcc -std=c99 -pedantic -W -Wall swapd.c -o swapd
yield different results
*/

#include <stdio.h>
#include <stdint.h>

// reverse order of bytes in 32bit word, no undefined behavior, right?
void swap32( int32_t *in,
int32_t *out )
{
char *p_in = (char*)in;
char *p_out = (char*)out;

p_out[0] = p_in[3];
p_out[1] = p_in[2];
p_out[2] = p_in[1];
p_out[3] = p_in[0];
}

int32_t swap32_wrap( int32_t in )
{
int32_t ret;

swap32( (void*)&in, (void*)&ret );

return ret;
}

double swapd( double d )
{
double ret;
int32_t *p_out = (void*)&ret;
int32_t *p_in = (void*)&d;

swap32( &p_in[1], &p_out[0] );
swap32( &p_in[0], &p_out[1] );

return ret;
}

double swapd_wrap( double d )
{
double ret;
int32_t *p_out = (void*)&ret;
int32_t *p_in = (void*)&d;

p_out[0] = swap32_wrap( p_in[1] );
p_out[1] = swap32_wrap( p_in[0] );

return ret;
}

int main(void)
{
double d_init = 1.23456e7;
double d = 0;
double ds = 0;
double ds2;

fprintf( stderr, "sizeof(double) = %d:\n", sizeof(double) );

d = d_init;
ds = swapd( d );
ds2 = swapd( ds );
fprintf( stderr, "swapd():\n" );
fprintf( stderr, "d = %.8e\n", d );
fprintf( stderr, "ds = %.8e\n", ds );
fprintf( stderr, "ds2 = %.8e\n", ds2 );
fprintf( stderr, "ds2 - d = %.8e\n", ds2-d );

d = d_init;
ds = swapd_wrap( d );
ds2 = swapd_wrap( ds );
fprintf( stderr, "swapd_wrap():\n" );
fprintf( stderr, "d = %.8e\n", d );
fprintf( stderr, "ds = %.8e\n", ds );
fprintf( stderr, "ds2 = %.8e\n", ds2 );
fprintf( stderr, "ds2 - d = %.8e\n", ds2-d );

/*
with -03 prints:

sizeof(double) = 8:
swapd():
d = 1.23456000e+07
ds = 2.69795606e-315
ds2 = 1.23456000e+07
ds2 - d = 0.00000000e+00
swapd_wrap():
d = 1.23456000e+07
ds = 1.16542096e-314
ds2 = -1.99574670e+00
ds2 - d = -1.23456020e+07
*/

/*
without -03 prints:

sizeof(double) = 8:
swapd():
d = 1.23456000e+07
ds = 2.69795606e-315
ds2 = 1.23456000e+07
ds2 - d = 0.00000000e+00
swapd_wrap():
d = 1.23456000e+07
ds = 2.69795606e-315
ds2 = 1.23456000e+07
ds2 - d = 0.00000000e+00
*/

return 0;
}

Nov 14 '05 #1
1 1452
hj*******@gmx.de wrote:
Hi

in the following source code, I think swap32() itself should be ok,
while I am unsure if swapd() and swapd_wrap() invoke undefined
behavior.

Please use masks and shift if you want to be correct.
Nov 14 '05 #2

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

Similar topics

48
by: marbac | last post by:
Hi, i heard a lot about "undefined behaviour" in this and other newsgroups dealing with c/c++. Is there a list where all cases with undefined behaviour in C++ are listed? regards marbac
19
by: E. Robert Tisdale | last post by:
In the context of the comp.lang.c newsgroup, the term "undefined behavior" actually refers to behavior not defined by the ANSI/ISO C 9 standard. Specifically, it is *not* true that "anything can...
66
by: Mantorok Redgormor | last post by:
#include <stdio.h> struct foo { int example; struct bar *ptr; }; int main(void) { struct foo baz; baz.ptr = NULL; /* Undefined behavior? */ return 0;
25
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
30
by: jimjim | last post by:
Hello, #include <stdio.h> int main(int argc, char *argv) { int x = 1; printf("%d %d %d\n", ++x, x, x++); return 0; }
33
by: dragoncoder | last post by:
Hi all, Does the following code invoke undefined behaviour ? $ cat a1.cc #include <iostream> #include <limits> int main() { int a = INT_MAX/2;
14
by: avsharath | last post by:
In "Bjarne Stroustrup's C++ Style and Technique FAQ" at: http://www.research.att.com/~bs/bs_faq2.html#evaluation-order for the statement: f(v,i++); he says that "the result is undefined...
12
by: Rajesh S R | last post by:
Can anyone tell me what is the difference between undefined behavior and unspecified behavior? Though I've read what is given about them, in ISO standards, I'm still not able to get the...
33
by: coolguyaroundyou | last post by:
Will the following statement invoke undefined behavior : a^=b,b^=a,a^=b ; given that a and b are of int-type ?? Be cautious, I have not written a^=b^=a^=b ; which, of course, is undefined....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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
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,...

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.