Poached or Fried?

Completely unrelated to any goings-on this week, I just thought I’d stick my head up and make a little observation about writing endian-safe code.

This is one way to do it:

#if defined(__BIG_ENDIAN_BITFIELD)
#define X(a,b)  b,a
#else
#define X(a,b)  a,b
#endif
struct config {
/*0*/   u8 X(byte_count:6, pad0:2);
/*1*/   u8 X(X(rx_fifo_limit:4, tx_fifo_limit:3), pad1:1);
/*2*/   u8 adaptive_ifs;
/*3*/   u8 X(X(X(X(mwi_enable:1, type_enable:1), read_align_enable:1),
       term_write_cache_line:1), pad3:4);
/*4*/   u8 X(rx_dma_max_count:7, pad4:1);
/*5*/   u8 X(tx_dma_max_count:7, dma_max_count_enable:1);
/*6*/   u8 X(X(X(X(X(X(X(late_scb_update:1, direct_rx_dma:1),
       tno_intr:1), cna_intr:1), standard_tcb:1), standard_stat_counter:1),
       rx_discard_overruns:1), rx_save_bad_frames:1);
/*7*/   u8 X(X(X(X(X(rx_discard_short_frames:1, tx_underrun_retry:2),
       pad7:2), rx_extended_rfd:1), tx_two_frames_in_fifo:1),
       tx_dynamic_tbd:1);
/*8*/   u8 X(X(mii_mode:1, pad8:6), csma_disabled:1);
/*9*/   u8 X(X(X(X(X(rx_tcpudp_checksum:1, pad9:3), vlan_arp_tco:1),
       link_status_wake:1), arp_wake:1), mcmatch_wake:1);
/*10*/  u8 X(X(X(pad10:3, no_source_addr_insertion:1), preamble_length:2),
       loopback:2);
/*11*/  u8 X(linear_priority:3, pad11:5);
/*12*/  u8 X(X(linear_priority_mode:1, pad12:3), ifs:4);
/*13*/  u8 ip_addr_lo;
/*14*/  u8 ip_addr_hi;
/*15*/  u8 X(X(X(X(X(X(X(promiscuous_mode:1, broadcast_disabled:1),
       wait_after_win:1), pad15_1:1), ignore_ul_bit:1), crc_16_bit:1),
       pad15_2:1), crs_or_cdt:1);
/*16*/  u8 fc_delay_lo;
/*17*/  u8 fc_delay_hi;
/*18*/  u8 X(X(X(X(X(rx_stripping:1, tx_padding:1), rx_crc_transfer:1),
       rx_long_ok:1), fc_priority_threshold:3), pad18:1);
/*19*/  u8 X(X(X(X(X(X(X(addr_wake:1, magic_packet_disable:1),
       fc_disable:1), fc_restop:1), fc_restart:1), fc_reject:1),
       full_duplex_force:1), full_duplex_pin:1);
/*20*/  u8 X(X(X(pad20_1:5, fc_priority_location:1), multi_ia:1), pad20_2:1);
/*21*/  u8 X(X(pad21_1:3, multicast_all:1), pad21_2:4);
/*22*/  u8 X(X(rx_d102_mode:1, rx_vlan_drop:1), pad22:6);
    u8 pad_d102[9];
};

This code is from drivers/net/e100.c, Copyright © 1999 – 2004 Intel Corporation, and licensed under the GNU GPL v2.

Posted by mike on Friday June 10th, 2005, tagged with ,

Comments are closed.