RE: [PATCH net-next v4 2/8] 6lowpan: add uncompress header size function
From: David Laight <hidden>
Date: 2014-02-26 17:06:47
From: Alexander Aring
On Wed, Feb 26, 2014 at 04:10:05PM +0000, David Laight wrote:quoted
From: Alexander Aring
...
quoted
quoted
+static inline u8 lowpan_addr_mode_size(const u8 addr_mode) +{ + switch (addr_mode) { + case LOWPAN_IPHC_ADDR_00: + return 16; + case LOWPAN_IPHC_ADDR_01: + return 8; + case LOWPAN_IPHC_ADDR_02: + return 2; + default: + return 0; + } +}The compiler will generate much better code if you index an array instead of using a switch statement.You mean something like: static inline u8 lowpan_addr_mode_size(const u8 addr_mode) { const u8 res[] = { 16, 8, 2, 0 }; return res[addr_mode]; } or should I drop the array from the stack and declare it static?
You definitely want the array to be static (as well as const).
You could also use named initialisers.
If I've got the syntax right that would be:
static const u8 sizes[] = {
[LOWPAN_IPHC_ADDR_00] = 16,
[LOWPAN_IPHC_ADDR_01] = 8,
[LOWPAN_IPHC_ADDR_02] = 2,
};
return sizes[addr_mode];
Whether you need a bound check (or the 4th array index) depends on where
the input value comes from.
David