Reply by H. Peter Anvin September 22, 20042004-09-22
Followup to:  <8m34d.186242$%n4.110639@bignews6.bellsouth.net>
By author:    mike@mnmoran.org
In newsgroup: comp.arch.embedded
> > Fred Viles wrote: > > "Roman Mashak" <mrv@tusur.ru> wrote in > >>I'd like to understand how to > >>determine the endianness from software. > > > > > > one generic way (assuming sizeof(int)>sizeof(char)): > > > > int i=1; > > if (*(char *)&i) > > /* little endian */; > > BOO. > Beware of optimizer. :-) >
If the optimizer optimizes that out, then the optimizer is broken. However, try: static volatile uint16_t i = 0x0102; volatile uint8_t *p = (volatile uint8_t *)&i; switch ( *p ) { case 1: /* bigendian */ break; case 2: /* littleendian */ break; default: /* wtf */ break; } If the optimizer optimizes *THAT* out then napalm your compiler vendor. -hpa
Reply by Michael N. Moran September 21, 20042004-09-21
Fred Viles wrote:
> "Roman Mashak" <mrv@tusur.ru> wrote in >>I'd like to understand how to >>determine the endianness from software. > > > one generic way (assuming sizeof(int)>sizeof(char)): > > int i=1; > if (*(char *)&i) > /* little endian */;
BOO. Beware of optimizer. :-) -- Michael N. Moran (h) 770 516 7918 5009 Old Field Ct. (c) 678 521 5460 Kennesaw, GA, USA 30144 http://mnmoran.org "... abstractions save us time working, but they don't save us time learning." Joel Spolsky, The Law of Leaky Abstractions The Beatles were wrong: 1 & 1 & 1 is 1
Reply by Fred Viles September 21, 20042004-09-21
"Roman Mashak" <mrv@tusur.ru> wrote in
news:cimifa$2cik$1@mpeks.tomsk.su: 

>... > I'd like to understand how to > determine the endianness from software.
one generic way (assuming sizeof(int)>sizeof(char)): int i=1; if (*(char *)&i) /* little endian */;
Reply by Roman Mashak September 20, 20042004-09-20
Hello, All!

Could you please explain me the process of establishing the endianness on
Alchemy board? This board has the DIP switches for establish endian mode,
and I'd like to understand how to determine the endianness from software.

On-board CPU is MIPS32.

With best regards, Roman Mashak.  E-mail: mrv@tusur.ru