Page 1 of 1

MOD5270 Time Example, has some compile warnings

Posted: Wed Jul 28, 2010 7:10 am
by seulater
I tried the C:\nburn\examples\mod5270\time, example. After compiling i got 3 warnings

in the function below the warning is "dereferencing type-punned pointer will break strict-aliasing rules"

/* Get the most significat DWORD of a long long usually the seconds in an NTP long long */
DWORD GetMsb( unsigned long long lv )
{
return *( ( ( PDWORD ) & lv ) );
}

What would be the corrected function?

Re: MOD5270 Time Example, has some compile warnings

Posted: Wed Jul 28, 2010 10:54 am
by pbreed
You could Probably do

DWORD GetMsb( unsigned long long lv )
{
return (DWORD) (lv>>32);
}

Calls library function and is slower.

Paul

Re: MOD5270 Time Example, has some compile warnings

Posted: Wed Jul 28, 2010 4:50 pm
by tod
If you just want to get rid of the warning you can add -fno-strict-aliasing to the c++ compiler command line for the project.

The corrected function (which belongs in a utility class in a static library) is IMO:

Code: Select all

enum WhichBigEndianWord{LowerBigEndian, UpperBigEndian};
uint32_t GetWord32From64( uint64_t value, WhichBigEndianWord whichWord )
{
	union
	{
		uint64_t value64;
		uint32_t word32[2];
	} union_64_32;
	union_64_32.value64 = value;
	return union_64_32.word32[whichWord];
}
The test code would look like this

Code: Select all

	uint64_t value_64bit = 0x100000008LL;
	uint32_t high_32 = GetWord32From64(value_64bit,UpperBigEndian);
	uint32_t low_32 = GetWord32From64(value_64bit,LowerBigEndian);
	cout << "High:" <<high_32 <<endl;
	cout << "Low:" << low_32 << endl;

High:1
Low:8

Re: MOD5270 Time Example, has some compile warnings

Posted: Wed Jul 28, 2010 6:54 pm
by seulater
Thanks guys, its more of a heads up for NB to fix the example.