• __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;

  • 相关阅读:
    cookie简介
    Javaweb
    apache-tomcat-7.0.92
    学javaweb 先学Servlet 应用理论很重要
    DataGuard切换(主库为Rac+备库为Rac)
    Oracle 迁移 序列
    oracle SQL 执行进度
    ORA-20011 ORA-29913 KUP-11024 GATHER_TABLE_STATS
    盘点 Oracle 11g 中新特性带来的10大性能影响
    查询近一个月的表空间使用情况
  • 原文地址:https://www.cnblogs.com/banwhui/p/4754089.html
Copyright © 2020-2023  润新知