{"id":7020,"date":"2005-06-10T00:00:00","date_gmt":"2005-06-09T14:00:00","guid":{"rendered":"http:\/\/michael.ellerman.id.au\/blog\/2005\/06\/10\/poached-or-fried\/"},"modified":"2007-10-25T13:51:00","modified_gmt":"2007-10-25T03:51:00","slug":"poached-or-fried","status":"publish","type":"post","link":"https:\/\/michael.ellerman.id.au\/blog\/2005\/06\/10\/poached-or-fried\/","title":{"rendered":"Poached or Fried?"},"content":{"rendered":"<p>Completely unrelated to any goings-on this week, I just thought I&#8217;d stick my head up and make a little observation about writing endian-safe code.<\/p>\n<p>This is one way to do it:<\/p>\n<pre><code>#if defined(__BIG_ENDIAN_BITFIELD)\r\n#define X(a,b)  b,a\r\n#else\r\n#define X(a,b)  a,b\r\n#endif\r\nstruct config {\r\n\/*0*\/   u8 X(byte_count:6, pad0:2);\r\n\/*1*\/   u8 X(X(rx_fifo_limit:4, tx_fifo_limit:3), pad1:1);\r\n\/*2*\/   u8 adaptive_ifs;\r\n\/*3*\/   u8 X(X(X(X(mwi_enable:1, type_enable:1), read_align_enable:1),\r\n       term_write_cache_line:1), pad3:4);\r\n\/*4*\/   u8 X(rx_dma_max_count:7, pad4:1);\r\n\/*5*\/   u8 X(tx_dma_max_count:7, dma_max_count_enable:1);\r\n\/*6*\/   u8 X(X(X(X(X(X(X(late_scb_update:1, direct_rx_dma:1),\r\n       tno_intr:1), cna_intr:1), standard_tcb:1), standard_stat_counter:1),\r\n       rx_discard_overruns:1), rx_save_bad_frames:1);\r\n\/*7*\/   u8 X(X(X(X(X(rx_discard_short_frames:1, tx_underrun_retry:2),\r\n       pad7:2), rx_extended_rfd:1), tx_two_frames_in_fifo:1),\r\n       tx_dynamic_tbd:1);\r\n\/*8*\/   u8 X(X(mii_mode:1, pad8:6), csma_disabled:1);\r\n\/*9*\/   u8 X(X(X(X(X(rx_tcpudp_checksum:1, pad9:3), vlan_arp_tco:1),\r\n       link_status_wake:1), arp_wake:1), mcmatch_wake:1);\r\n\/*10*\/  u8 X(X(X(pad10:3, no_source_addr_insertion:1), preamble_length:2),\r\n       loopback:2);\r\n\/*11*\/  u8 X(linear_priority:3, pad11:5);\r\n\/*12*\/  u8 X(X(linear_priority_mode:1, pad12:3), ifs:4);\r\n\/*13*\/  u8 ip_addr_lo;\r\n\/*14*\/  u8 ip_addr_hi;\r\n\/*15*\/  u8 X(X(X(X(X(X(X(promiscuous_mode:1, broadcast_disabled:1),\r\n       wait_after_win:1), pad15_1:1), ignore_ul_bit:1), crc_16_bit:1),\r\n       pad15_2:1), crs_or_cdt:1);\r\n\/*16*\/  u8 fc_delay_lo;\r\n\/*17*\/  u8 fc_delay_hi;\r\n\/*18*\/  u8 X(X(X(X(X(rx_stripping:1, tx_padding:1), rx_crc_transfer:1),\r\n       rx_long_ok:1), fc_priority_threshold:3), pad18:1);\r\n\/*19*\/  u8 X(X(X(X(X(X(X(addr_wake:1, magic_packet_disable:1),\r\n       fc_disable:1), fc_restop:1), fc_restart:1), fc_reject:1),\r\n       full_duplex_force:1), full_duplex_pin:1);\r\n\/*20*\/  u8 X(X(X(pad20_1:5, fc_priority_location:1), multi_ia:1), pad20_2:1);\r\n\/*21*\/  u8 X(X(pad21_1:3, multicast_all:1), pad21_2:4);\r\n\/*22*\/  u8 X(X(rx_d102_mode:1, rx_vlan_drop:1), pad22:6);\r\n    u8 pad_d102[9];\r\n};\r\n<\/code><\/pre>\n<p><small>This code is from <code>drivers\/net\/e100.c<\/code>, Copyright \u00c2\u00a9 1999 &#8211; 2004 Intel Corporation, and licensed under the <a href=\"http:\/\/www.gnu.org\/licenses\/gpl.txt\">GNU GPL v2<\/a>.<\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Completely unrelated to any goings-on this week, I just thought I&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12],"tags":[17,28],"_links":{"self":[{"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/posts\/7020"}],"collection":[{"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/comments?post=7020"}],"version-history":[{"count":0,"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/posts\/7020\/revisions"}],"wp:attachment":[{"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/media?parent=7020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/categories?post=7020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/tags?post=7020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}