MOD5270 Time Example, has some compile warnings

Discussion to talk about software related topics only.
Post Reply
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

MOD5270 Time Example, has some compile warnings

Post 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?
User avatar
pbreed
Posts: 1088
Joined: Thu Apr 24, 2008 3:58 pm

Re: MOD5270 Time Example, has some compile warnings

Post by pbreed »

You could Probably do

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

Calls library function and is slower.

Paul
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: MOD5270 Time Example, has some compile warnings

Post 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
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Re: MOD5270 Time Example, has some compile warnings

Post by seulater »

Thanks guys, its more of a heads up for NB to fix the example.
Post Reply