lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lwip-users] Re: [lwip] Anyone Using An Architecture That Isn't little


From: Joel Baumert
Subject: [lwip-users] Re: [lwip] Anyone Using An Architecture That Isn't little or big endian?
Date: Wed, 08 Jan 2003 23:15:43 -0000

You could try to inline the htons... Something like (my parens may be off :-):

#define HTONS(X) (((X)>>8)&0xff)|(((X)<<8)&0xff00)

#define ETHTYPE_ARP (HTONS(0x0806))

When you feed in a constant X it should fold it into a constant swapped 
value.  The HTONS macro would just be blank on architectures of the other 
endian-ness.

Joel

At 03:18 PM 8/19/02 -0700, you wrote:
>Hi Rod,
>
>Thanks for the input. This looks like a good idea, but the compiler I'm 
>using would then insert byte-swapping code for each use of ETHTYPE_ARP or 
>ETHTYPE_IP, and it wouldn't be optimized out, and it ends up being more 
>code than the current approach. Would other C compilers really optimize 
>this away? Anyways, this particular code snippet would produce compile 
>errors in any C compiler since a case statement must contain a constant. 
>It would compile with an if then else statement, but might result in two 
>byte swaps taking place instead of one  ...
>
>Regards,
>
>John
>
>Rod Boyce wrote:
>
>>Might I be so bold as to suggest another approach which would be 
>>befinical for some of the more common tests and would eliminate the need 
>>to htons in the 'C'code fragment you detail below.
>>htons and htonl are defines if I remember correctly so this will works 
>>well.
>>But instead of performing the htons on the variable that will take up 
>>code space perform the htons on the constant in the case statment.  This 
>>whould then be removed from the code either by the pre-processor or the 
>>peep hole optomiser.  This would then mean that for every comparision 
>>with a constant the constans is already in the correct format for the 
>>machine and does not need a seperate set of constants for each type of 
>>endian.  I would suggest changing the code fragment below to look like this:
>>
>><etherarp.h>
>>#define ETHTYPE_ARP (htons(0x0806))
>>#define ETHERTYPE_IP (htons(0x0800))
>>
>><ethernetif.c>
>>    switch(ethhdr-type){
>>        case ETHTYPE_IP:
>>
>>I beleive the rest is obvious this makes the code eaiser to read and from 
>>my experance in multi-platform code anything to make the code easier to 
>>read is a good thing and should be promoted.
>>
>>I have seen this done with a lot of comerical IP stacks for multi-platforms.
>>
>>Regards,
>>Rod Boyce.
>>
>>
>>On Mon, 19 Aug 2002 12:04, John C. Toman wrote:
>>
>>
>>>Just checking. I'm considering writing and submitting code which would
>>>eliminate many unnecessary byte swaps for little endian architectures,
>>>specifically for code constants, without affecting big endian
>>>architectures. I'm planning on making changes at least for my system and
>>>will contribute them back to the lwIP tree if others are interested. For
>>>instance, consider:
>>>
>>>Currently in lwIP (one example from etharp.h):
>>>
>>>#define ETHTYPE_ARP 0x0806
>>>#define ETHTYPE_IP  0x0800
>>
>>[...]
>>----------------------------------
>>  Joel Baumert
>>  Zworld, Senior Software Engineer
>>  http://www.zworld.com
>>----------------------------------

[This message was sent through the lwip discussion list.]




reply via email to

[Prev in Thread] Current Thread [Next in Thread]