"Fossies" - the Fresh Open Source Software Archive

Member "glusterfs-8.2/libglusterfs/src/glusterfs/byte-order.h" (16 Sep 2020, 5737 Bytes) of package /linux/misc/glusterfs-8.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "byte-order.h" see the Fossies "Dox" file reference documentation.

    1 /*
    2   Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
    3   This file is part of GlusterFS.
    4 
    5   This file is licensed to you under your choice of the GNU Lesser
    6   General Public License, version 3 or any later version (LGPLv3 or
    7   later), or the GNU General Public License, version 2 (GPLv2), in all
    8   cases as published by the Free Software Foundation.
    9 */
   10 
   11 #ifndef _BYTE_ORDER_H
   12 #define _BYTE_ORDER_H
   13 
   14 #include <inttypes.h>
   15 
   16 #define LS1 0x00ffU
   17 #define MS1 0xff00U
   18 #define LS2 0x0000ffffU
   19 #define MS2 0xffff0000U
   20 #define LS4 0x00000000ffffffffULL
   21 #define MS4 0xffffffff00000000ULL
   22 
   23 static uint16_t (*hton16)(uint16_t);
   24 static uint32_t (*hton32)(uint32_t);
   25 static uint64_t (*hton64)(uint64_t);
   26 
   27 #define ntoh16 hton16
   28 #define ntoh32 hton32
   29 #define ntoh64 hton64
   30 
   31 static uint16_t (*htole16)(uint16_t);
   32 static uint32_t (*htole32)(uint32_t);
   33 static uint64_t (*htole64)(uint64_t);
   34 
   35 #define letoh16 htole16
   36 #define letoh32 htole32
   37 #define letoh64 htole64
   38 
   39 static uint16_t (*htobe16)(uint16_t);
   40 static uint32_t (*htobe32)(uint32_t);
   41 static uint64_t (*htobe64)(uint64_t);
   42 
   43 #define betoh16 htobe16
   44 #define betoh32 htobe32
   45 #define betoh64 htobe64
   46 
   47 #define do_swap2(x) (((x & LS1) << 8) | (((x & MS1) >> 8)))
   48 #define do_swap4(x) ((do_swap2(x & LS2) << 16) | (do_swap2((x & MS2) >> 16)))
   49 #define do_swap8(x) ((do_swap4(x & LS4) << 32) | (do_swap4((x & MS4) >> 32)))
   50 
   51 static inline uint16_t
   52 __swap16(uint16_t x)
   53 {
   54     return do_swap2(x);
   55 }
   56 
   57 static inline uint32_t
   58 __swap32(uint32_t x)
   59 {
   60     return do_swap4(x);
   61 }
   62 
   63 static inline uint64_t
   64 __swap64(uint64_t x)
   65 {
   66     return do_swap8(x);
   67 }
   68 
   69 static inline uint16_t
   70 __noswap16(uint16_t x)
   71 {
   72     return x;
   73 }
   74 
   75 static inline uint32_t
   76 __noswap32(uint32_t x)
   77 {
   78     return x;
   79 }
   80 
   81 static inline uint64_t
   82 __noswap64(uint64_t x)
   83 {
   84     return x;
   85 }
   86 
   87 static inline uint16_t
   88 __byte_order_n16(uint16_t i)
   89 {
   90     uint32_t num = 1;
   91 
   92     if (((char *)(&num))[0] == 1) {
   93         /* cpu is le */
   94         hton16 = __swap16;
   95         hton32 = __swap32;
   96         hton64 = __swap64;
   97     } else {
   98         /* cpu is be */
   99         hton16 = __noswap16;
  100         hton32 = __noswap32;
  101         hton64 = __noswap64;
  102     }
  103 
  104     return hton16(i);
  105 }
  106 
  107 static inline uint32_t
  108 __byte_order_n32(uint32_t i)
  109 {
  110     uint32_t num = 1;
  111 
  112     if (((char *)(&num))[0] == 1) {
  113         /* cpu is le */
  114         hton16 = __swap16;
  115         hton32 = __swap32;
  116         hton64 = __swap64;
  117     } else {
  118         /* cpu is be */
  119         hton16 = __noswap16;
  120         hton32 = __noswap32;
  121         hton64 = __noswap64;
  122     }
  123 
  124     return hton32(i);
  125 }
  126 
  127 static inline uint64_t
  128 __byte_order_n64(uint64_t i)
  129 {
  130     uint32_t num = 1;
  131 
  132     if (((char *)(&num))[0] == 1) {
  133         /* cpu is le */
  134         hton16 = __swap16;
  135         hton32 = __swap32;
  136         hton64 = __swap64;
  137     } else {
  138         /* cpu is be */
  139         hton16 = __noswap16;
  140         hton32 = __noswap32;
  141         hton64 = __noswap64;
  142     }
  143 
  144     return hton64(i);
  145 }
  146 
  147 static uint16_t (*hton16)(uint16_t) = __byte_order_n16;
  148 static uint32_t (*hton32)(uint32_t) = __byte_order_n32;
  149 static uint64_t (*hton64)(uint64_t) = __byte_order_n64;
  150 
  151 static inline uint16_t
  152 __byte_order_le16(uint16_t i)
  153 {
  154     uint32_t num = 1;
  155 
  156     if (((char *)(&num))[0] == 1) {
  157         /* cpu is le */
  158         htole16 = __noswap16;
  159         htole32 = __noswap32;
  160         htole64 = __noswap64;
  161     } else {
  162         /* cpu is be */
  163         htole16 = __swap16;
  164         htole32 = __swap32;
  165         htole64 = __swap64;
  166     }
  167 
  168     return htole16(i);
  169 }
  170 
  171 static inline uint32_t
  172 __byte_order_le32(uint32_t i)
  173 {
  174     uint32_t num = 1;
  175 
  176     if (((char *)(&num))[0] == 1) {
  177         /* cpu is le */
  178         htole16 = __noswap16;
  179         htole32 = __noswap32;
  180         htole64 = __noswap64;
  181     } else {
  182         /* cpu is be */
  183         htole16 = __swap16;
  184         htole32 = __swap32;
  185         htole64 = __swap64;
  186     }
  187 
  188     return htole32(i);
  189 }
  190 
  191 static inline uint64_t
  192 __byte_order_le64(uint64_t i)
  193 {
  194     uint32_t num = 1;
  195 
  196     if (((char *)(&num))[0] == 1) {
  197         /* cpu is le */
  198         htole16 = __noswap16;
  199         htole32 = __noswap32;
  200         htole64 = __noswap64;
  201     } else {
  202         /* cpu is be */
  203         htole16 = __swap16;
  204         htole32 = __swap32;
  205         htole64 = __swap64;
  206     }
  207 
  208     return htole64(i);
  209 }
  210 
  211 static uint16_t (*htole16)(uint16_t) = __byte_order_le16;
  212 static uint32_t (*htole32)(uint32_t) = __byte_order_le32;
  213 static uint64_t (*htole64)(uint64_t) = __byte_order_le64;
  214 
  215 static inline uint16_t
  216 __byte_order_be16(uint16_t i)
  217 {
  218     uint32_t num = 1;
  219 
  220     if (((char *)(&num))[0] == 1) {
  221         /* cpu is le */
  222         htobe16 = __swap16;
  223         htobe32 = __swap32;
  224         htobe64 = __swap64;
  225     } else {
  226         /* cpu is be */
  227         htobe16 = __noswap16;
  228         htobe32 = __noswap32;
  229         htobe64 = __noswap64;
  230     }
  231 
  232     return htobe16(i);
  233 }
  234 
  235 static inline uint32_t
  236 __byte_order_be32(uint32_t i)
  237 {
  238     uint32_t num = 1;
  239 
  240     if (((char *)(&num))[0] == 1) {
  241         /* cpu is le */
  242         htobe16 = __swap16;
  243         htobe32 = __swap32;
  244         htobe64 = __swap64;
  245     } else {
  246         /* cpu is be */
  247         htobe16 = __noswap16;
  248         htobe32 = __noswap32;
  249         htobe64 = __noswap64;
  250     }
  251 
  252     return htobe32(i);
  253 }
  254 
  255 static inline uint64_t
  256 __byte_order_be64(uint64_t i)
  257 {
  258     uint32_t num = 1;
  259 
  260     if (((char *)(&num))[0] == 1) {
  261         /* cpu is le */
  262         htobe16 = __swap16;
  263         htobe32 = __swap32;
  264         htobe64 = __swap64;
  265     } else {
  266         /* cpu is be */
  267         htobe16 = __noswap16;
  268         htobe32 = __noswap32;
  269         htobe64 = __noswap64;
  270     }
  271 
  272     return htobe64(i);
  273 }
  274 
  275 static uint16_t (*htobe16)(uint16_t) = __byte_order_be16;
  276 static uint32_t (*htobe32)(uint32_t) = __byte_order_be32;
  277 static uint64_t (*htobe64)(uint64_t) = __byte_order_be64;
  278 
  279 #endif /* _BYTE_ORDER_H */