This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Do I need modify newlib code for endian changed?


On 07/24/11 17:29, Liu wrote:
> On Sun, Jul 24, 2011 at 7:25 AM, Stuart Longland <redhatter@gentoo.org> wrote:
>> On 07/24/11 09:21, Liu wrote:
>>> Hi all,
>>>
>>> We've changed a CPU into little-endian from big-endian, and binutils
>>> and gcc have been rewrite for little-endian.
>>> Do I need modify newlib code for endian changed? If I do, what should I do?
>>
>> Do you assume endianness anywhere?  If so, then yes.  If not, then no.
> I'm not sure about that, I know very few about newlib.
> I working on a little-endian openrisc CPU, may you tell me what to do?
> 

Well, as I say, it depends on the code you've written.  The following
for instance, will break on big endian systems:

uint16_t htons(uint16_t word)
{
	union {
		uint16_t w;
		uint8_t b[2];
	} in, out;

	in.w = word;
	out.b[0] = in.b[1];
	out.b[1] = in.b[0];
	return out.w;
}

Likewise, this would break on little endian systems:
uint16_t htons(uint16_t word)
{
	return word;
}

The following however, would work on either the same:
uint16_t htons(uint16_t word)
{
	return ((word << 8) | (word >> 8));
}

The first assumes that the least significant byte is stored first.  The
second assumes big endian is the native representation.  The third makes
no assumptions on byte order.

newlib itself, will generally be compiled either for big-endian or
little-endian.  No rewriting of it, binutils, or gcc should be
necessary, just simply clean and recompile the source trees specifying
the new CHOST.
-- 
Stuart Longland (aka Redhatter, VK4MSL)      .'''.
Gentoo Linux/MIPS Cobalt and Docs Developer  '.'` :
. . . . . . . . . . . . . . . . . . . . . .   .'.'
http://dev.gentoo.org/~redhatter             :.'

I haven't lost my mind...
  ...it's backed up on a tape somewhere.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]