zoukankan      html  css  js  c++  java
  • __BIG_ENDIAN 和__LITTLE_ENDIAN

    只要通过引入头文件<endian.h>便可以在编译时通过宏判断字节序了

    #if __BYTE_ORDER == __LITTLE_ENDIAN

    #elif __BYTE_ORDER == __BIG_ENDIAN

    #else
    #error "Unknown byte order"
    #endif

    <endian.h>节选

    /* Definitions for byte order, according to significance of bytes,   from low addresses to high addresses.  The value is what you get by   putting '4' in the most significant byte, '3' in the second most   significant byte, '2' in the second least significant byte, and '1'   in the least significant byte, and then writing down one digit for   each byte, starting with the byte at the lowest address at the left,   and proceeding to the byte with the highest address at the right.  */#define __LITTLE_ENDIAN 1234#define __BIG_ENDIAN    4321#define __PDP_ENDIAN    3412/* This file defines `__BYTE_ORDER' for the particular machine.  */#include <bits/endian.h>

    完整举例

    #include <sys/types.h>
    #include <byteswap.h>
    #include <endian.h>
    #include <stdint.h>
    #include <inttypes.h>

    #if __BYTE_ORDER == __BIG_ENDIAN
    # define cpu_to_le16(x) bswap_16(x)
    # define le16_to_cpu(x) bswap_16(x)
    # define cpu_to_le32(x) bswap_32(x)
    # define le32_to_cpu(x) bswap_32(x)
    # define cpu_to_be16(x) (x)
    # define be16_to_cpu(x) (x)
    # define cpu_to_be32(x) (x)
    # define be32_to_cpu(x) (x)
    # define cpu_to_be64(x) (x)
    # define be64_to_cpu(x) (x)
    #elif __BYTE_ORDER == __LITTLE_ENDIAN
    # define cpu_to_le16(x) (x)
    # define le16_to_cpu(x) (x)
    # define cpu_to_le32(x) (x)
    # define le32_to_cpu(x) (x)
    # define cpu_to_be16(x) bswap_16(x)
    # define be16_to_cpu(x) bswap_16(x)
    # define cpu_to_be32(x) bswap_32(x)
    # define be32_to_cpu(x) bswap_32(x)
    # define cpu_to_be64(x) bswap_64(x)
    # define be64_to_cpu(x) bswap_64(x)
    #else
    #error "unknown endianess!"
    #endif

    /* min()/max() that do strict type-checking. Lifted from the kernel. */
    #define min(x, y) ({
    typeof(x) _min1 = (x);
    typeof(y) _min2 = (y);
    (void) (&_min1 == &_min2);
    _min1 < _min2 ? _min1 : _min2; })

    #define max(x, y) ({
    typeof(x) _max1 = (x);
    typeof(y) _max2 = (y);
    (void) (&_max1 == &_max2);
    _max1 > _max2 ? _max1 : _max2; })

    /* ... and their non-checking counterparts. */
    #define min_t(type, x, y) ({
    type _min1 = (x);
    type _min2 = (y);
    _min1 < _min2 ? _min1 : _min2; })

    #define max_t(type, x, y) ({
    type _max1 = (x);
    type _max2 = (y);
    _max1 > _max2 ? _max1 : _max2; })

    typedef uint8_t u8;
    typedef uint16_t u16;
    typedef uint32_t u32;
    typedef uint64_t u64;
    typedef int8_t s8;
    typedef int16_t s16;
    typedef int32_t s32;
    typedef int64_t s64;

  • 相关阅读:
    别再为了this发愁了:JS中的this机制
    专为控制打印设计的CSS样式
    怎样用纯HTML和CSS更改默认的上传文件按钮样式
    将HTML转成XHTML并清除一些无用的标签和属性
    JavaScript中textRange对象使用方法总结
    DIV+CSS规范命名集合
    JS条件判断
    CSS只是进化的一部分
    25 个超棒的 HTML5 & JavaScript 游戏引擎开发库
    开发一个完整的JavaScript组件
  • 原文地址:https://www.cnblogs.com/banwhui/p/4754089.html
Copyright © 2011-2022 走看看