变量操作位操作数据结构

/**
  本文内的结构类型主要为针对与硬件交互(通信或设置)数据、变量操作时,对位的操作而编写;
方便上位机与硬件数据对接;文中定义了8位、16位、24位、32位、64位、128位的结构,但使用时
要注意对于128位的操作。
日期:2022-02-17
63744457@qq.com
  */

#ifndef GLOBAL_INT_H
#define GLOBAL_INT_H

#include <stdint.h>


#if !defined (__GNUC__)
#define HAS_BOOST  2
#if (HAS_BOOST==0)
#error "不支持128位整数, 可以到https://www.boost.org下载boost库并设置导入,然后#define HAS_BOOST 1 , 如果#define HAS_BOOST 2 则默认,但运算符操作功能不全,但可以自己写!"
#elif (HAS_BOOST==1)
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
typedef int128_t  __int128_t ;
typedef uint128_t  __uint128_t ;
typedef int256_t  __int256_t ;
typedef uint256_t  __uint256_t ;
typedef int512_t  __int512_t ;
typedef uint512_t  __uint512_t ;
typedef int1024_t  __int1024_t ;
typedef uint1024_t  __uint1024_t ;
#else
typedef int64_t  __SOFT_INT__ ;
typedef uint64_t  __SOFT_UINT__ ;
#endif
#else
typedef __int128_t  __SOFT_INT__ ;
typedef __uint128_t  __SOFT_UINT__ ;
#endif


#pragma pack(1)
typedef union
{
    struct {
        uint8_t b0:1;
        uint8_t b1:1;
        uint8_t b2:1;
        uint8_t b3:1;
        uint8_t b4:1;
        uint8_t b5:1;
        uint8_t b6:1;
        uint8_t b7:1;
    };
    union {
        uint8_t uval,byte;
        int8_t sval;
    };
} TBits8, TByte, TUint8, TInt8;

typedef union
{
    struct {
        TByte L8;
        TByte H8;
    };
    union {
        TBits8 byte[2];
        uint16_t uword,uval;
        int16_t sword,sval;
    };
} TBits16, TUint16, TInt16;

typedef union
{
    struct {
        TByte L8;
        TByte M8;
        TByte H8;
    };
    union {
        TBits8 byte[3];
        uint32_t uval:24;
        int32_t sval:24;
    };
} TBits24, TUint24, TInt24;

typedef union
{
    struct {
        TBits16 L16;
        TBits16 H16;
    };
    union {
        TBits8 byte[4];
        uint32_t uword,uval;
        int32_t sword,sval;
    };
} TBits32, TUint32, TInt32;

typedef union
{
    struct {
        TBits32 L32;
        TBits32 H32;
    };
    union {
        TBits8 byte[8];
        uint64_t uword,uval;
        int64_t sword,sval;
    };
} TBits64, TUint64, TInt64;


typedef union TBits128 {
    struct {
        TBits64 L64;
        TBits64 H64;
    };


#if defined (__GNUC__)
    union {
        TBits8 byte[16];
        __uint128_t uword,uval;
        __int128_t sword,sval;
        struct {
            uint64_t uwordL,uwordH;
        };
        struct {
            int64_t swordL,swordH;
        };
        struct {
            uint64_t uvalL,uvalH;
        };
        struct {
            int64_t svalL,svalH;
        };
    };
#else
    union {
        TBits8 byte[16];
        __SOFT_UINT__ uword,uval;
        __SOFT_INT__ sword,sval;
        struct {
            uint64_t uwordL,uwordH;
        };
        struct {
            int64_t swordL,swordH;
        };
        struct {
            uint64_t uvalL,uvalH;
        };
        struct {
            int64_t svalL,svalH;
        };
    };
#endif



    inline TBits128 &operator<<(const int &);
    inline TBits128 &operator<<=(const int &) ;
    inline TBits128 &operator>>(const int &) ;
    inline TBits128 &operator>>=(const int &) ;

    inline TBits128 &operator|( const __SOFT_UINT__ &c );//{ this->uval |= c;  return *this; } ;
    inline TBits128 &operator|=(const __SOFT_UINT__ &) ;
    inline TBits128 &operator|(const TBits128 &) ;
    inline TBits128 &operator|=(const TBits128 &) ;

    inline TBits128 &operator&(const __SOFT_UINT__ &c);// { this->uval &= c;  return *this; } ;
    inline TBits128 &operator&=(const __SOFT_UINT__ &) ;
    inline TBits128 &operator&(const TBits128 &) ;
    inline TBits128 &operator&=(const TBits128 &) ;

    inline TBits128 &operator=(const __SOFT_UINT__ &) ;
    inline TBits128 &operator=(const TBits128 &) ;

    inline bool operator==(const __SOFT_UINT__ &) ;
    inline bool operator!=(const __SOFT_UINT__ &) ;
    inline bool operator!=(const TBits128 &) ;
    inline bool operator==(const TBits128 &) ;

    inline bool operator>(const __SOFT_UINT__ &) ;
    inline bool operator<(const __SOFT_UINT__ &) ;
    inline bool operator>(const TBits128 &) ;
    inline bool operator<(const TBits128 &) ;

    inline bool operator>=(const __SOFT_UINT__ &) ;
    inline bool operator<=(const TBits128 &) ;



    inline TBits128 &operator+=(const __SOFT_UINT__ &) ;
    inline TBits128 &operator-=(const __SOFT_UINT__ &) ;
    inline TBits128 &operator+=(const TBits128 &) ;
    inline TBits128 &operator-=(const TBits128 &) ;
    inline TBits128 &operator*=(double c) ;
    inline TBits128 &operator*=(const TBits128 &) ;

#if defined (__GNUC__)
    inline TBits128 &operator/=(double c);

//     inline  bool operator==(const TBits128 &, const TBits128 &) ;
//     inline  bool operator!=(const TBits128 &, const TBits128 &) ;
//     inline  const TBits128 &operator+(const TBits128 &, const TBits128 &) ;
//     inline  const TBits128 &operator-(const TBits128 &, const TBits128 &) ;
//     inline  const TBits128 &operator*(const TBits128 &, double) ;
//     inline  const TBits128 &operator*(double, const TBits128 &) ;
//     inline const TBits128& operator/(const TBits128 &, double);
#endif


} TBits128, TUint128, TInt128;

#if defined (__GNUC__)
inline TBits128 &TBits128::operator<<(const int &c){ this->uval <<= c; return *this; };
inline TBits128 &TBits128::operator<<=(const int &c) { this->uval <<= c; return *this; };
inline TBits128 &TBits128::operator>>(const int &c) { this->uval >>= c; return *this; };
inline TBits128 &TBits128::operator>>=(const int &c) { this->uval >>= c; return *this; };

inline TBits128 &TBits128::operator|( const __uint128_t &c ){ this->uval |= c;  return *this; } ;
inline TBits128 &TBits128::operator|=(const __uint128_t &c) { this->uval |= c; return *this; };
inline TBits128 &TBits128::operator|(const TBits128 &c) { this->uval |= c.uval; return *this; };
inline TBits128 &TBits128::operator|=(const TBits128 &c) { this->uval |= c.uval; return *this; };

inline TBits128 &TBits128::operator&(const __uint128_t &c) { this->uval &= c;  return *this; } ;
inline TBits128 &TBits128::operator&=(const __uint128_t &c) { this->uval &= c; return *this; };
inline TBits128 &TBits128::operator&(const TBits128 &c) { this->uval &= c.uval; return *this; };
inline TBits128 &TBits128::operator&=(const TBits128 &c) { this->uval &= c.uval; return *this; };

inline TBits128 &TBits128::operator=(const __uint128_t &c) { this->uval = c; return *this; };
inline TBits128 &TBits128::operator=(const TBits128 &c) { this->uval = c.uval; return *this; };

inline bool TBits128::operator==(const __uint128_t &c) {  return this->uval == c;; };
inline bool TBits128::operator!=(const __uint128_t &c) {  return this->uval != c;; };
inline bool TBits128::operator!=(const TBits128 &c) {  return this->uval != c.uval;; };
inline bool TBits128::operator==(const TBits128 &c) {  return this->uval == c.uval;; };

inline bool TBits128::operator>(const __uint128_t &c) {  return this->uval > c;; };
inline bool TBits128::operator<(const __uint128_t &c) {  return this->uval < c;; };
inline bool TBits128::operator>(const TBits128 &c) {  return this->uval > c.uval;; };
inline bool TBits128::operator<(const TBits128 &c) {  return this->uval < c.uval;; };

inline bool TBits128::operator>=(const __uint128_t &c) {  return this->uval > c;; };
inline bool TBits128::operator<=(const TBits128 &c) {  return this->uval < c.uval;; };


inline TBits128 &TBits128::operator+=(const __uint128_t &c) { this->uval += c; return *this; };
inline TBits128 &TBits128::operator-=(const __uint128_t &c) {  this->uval -= c;return *this; };
inline TBits128 &TBits128::operator+=(const TBits128 &c) {  this->uval += c.uval;return *this; };
inline TBits128 &TBits128::operator-=(const TBits128 &c) { this->uval -= c.uval; return *this; };
inline TBits128 &TBits128::operator*=(double c) { this->uval *= c; return *this; };
inline TBits128 &TBits128::operator*=(const TBits128 &c) { this->uval *= c.uval; return *this; };
inline TBits128 &TBits128::operator/=(double c){ this->uval /= c; return *this; };

// inline  bool TBits128::operator==(const TBits128 &a, const TBits128 &b) {  return this->uval == b.uval;; };
// inline  bool TBits128::operator!=(const TBits128 &a, const TBits128 &b) {  return this->uval != b.uval;; };
// inline  const TBits128 &TBits128::operator+(const TBits128 &a, const TBits128 &b)  { this->uval = a.uval + b.uval; return a; };
// inline  const TBits128 &TBits128::operator-(const TBits128 &a, const TBits128 &b) { this->uval = a.uval - b.uval; return *this; };
// inline  const TBits128 &TBits128::operator*(const TBits128 &a, double b)  { this->uval = a.uval + b; return *this; };
// inline  const TBits128 &TBits128::operator*(double a, const TBits128 &b)  { this->uval = a + b.uval; return *this; };
// inline const TBits128 &TBits128::operator/(const TBits128 &a, double b) { this->uval = a.uval / b; return a; };
#elif !defined (__GNUC__) && (HAS_BOOST==2)
inline TBits128 &TBits128::operator=(const __SOFT_UINT__ &c)
{
    this->uval = c;
    return *this;
}

inline TBits128 &TBits128::operator=(const TBits128 &c)
{
    this->uvalH = c.uvalH;
    this->uvalL = c.uvalL;
    return *this;
}

inline bool TBits128::operator==(const __SOFT_UINT__ &c)
{
    return (this->uval == c && this->uvalH==0);
}

inline bool TBits128::operator!=(const __SOFT_UINT__ &c)
{
  return  this->uvalH != 0 || this->uvalL != c;
}

inline bool TBits128::operator>(const __SOFT_UINT__ &c)
{
    return this->uval > c || this->uvalH!=0;
}

inline bool TBits128::operator<(const TBits128 &c)
{
  return  this->uvalH < c.uvalH || (this->uvalL < c.uvalL && this->uvalH == c.uvalH);
}

inline bool TBits128::operator<(const __SOFT_UINT__ &c)
{
    return this->uval < c && this->uvalH==0;
}

inline bool TBits128::operator>(const TBits128 &c)
{
  return  this->uvalH > c.uvalH ||
   (this->uvalH == c.uvalH && this->uvalL > c.uvalL);
}

inline bool TBits128::operator==(const TBits128 &c)
{
    return  this->uvalH == c.uvalH && this->uvalL == c.uvalL;
}

inline bool TBits128::operator!=(const TBits128 &c)
{
    return  this->uvalH != c.uvalH || this->uvalL != c.uvalL;
}

inline TBits128 &TBits128::operator|(const TBits128 &c)
{
    *this |= c;
    return *this;
}

inline TBits128 &TBits128::operator&(const TBits128 &c)
{
    *this &= c;
    return *this;
}


inline TBits128 &TBits128::operator|=(const __SOFT_UINT__ &c)
{
    this->uval |= c;
    return *this;
}
inline TBits128 &TBits128::operator|=(const TBits128 &c)
{
    this->uvalH |= c.uvalH;
    this->uvalL |= c.uvalL;
    return *this;
}

inline TBits128 &TBits128::operator&=(const __SOFT_UINT__ &c)
{
    this->uval &= c;
    return *this;
}
inline TBits128 &TBits128::operator&=(const TBits128 &c)
{
    this->uvalH &= c.uvalH;
    this->uvalL &= c.uvalL;
    return *this;
}

inline TBits128 &TBits128::operator<<=(const int &c)
{ return *this<<=c; }
inline TBits128 &TBits128::operator<<(const int & c)
{
    TBits128 r;
    r.uvalL=this->L64.H32.uval;
    r.uvalH=0;

    this->H64.uval <<= c;
    this->L64.uval <<= c;
    r.uvalL<<=c;
    this->H64.L32.uval |= r.L64.H32.uval;

    return *this;
}
inline TBits128 &TBits128::operator>>=(const int &c)
{ return *this>>=c; }
inline TBits128 &TBits128::operator>>(const int &c)
{
    TBits128 r;
    r.uvalL=0;
    r.uvalH=0;
    r.H64.H32.uval=this->H64.L32.uval;

    this->H64.uval >>= c;
    this->L64.uval >>= c;
    r.uvalH>>=c;
    this->L64.H32.uval |= r.H64.L32.uval;

    return *this;
}

inline TBits128 &TBits128::operator+=(const __SOFT_UINT__ &s)
{
    this->uwordL+=s;
    if(this->uwordL<s)
        this->uwordH++;
    return *this;
}

inline TBits128 &TBits128::operator-=(const __SOFT_UINT__ &s)
{
    if(this->uvalL<s)
    {
        this->uwordH--;
    }
    this->uwordL-=s;
    return *this;
}

inline TBits128 &TBits128::operator+=(const TBits128 &s)
{
    this->uwordL+=s.uwordL;
    this->uwordH+=s.uwordH;
    if(this->uwordL<s.uwordL)
        this->uwordH++;
    return *this;
}

inline TBits128 &TBits128::operator-=(const TBits128 &s)
{
    if(this->uvalL<s.uwordL)
    {
        this->uwordH--;
    }
    this->uwordL-=s.uwordL;
    this->uwordH-=s.uwordH;
    return *this;
}

inline TBits128 &TBits128::operator*=(double c)
{
    uint32_t u=-1;
    TBits64 t;
    TBits128 r;
    r.uvalL=0;
    r.uvalH=0;

    t.uval = this->L64.L32.uval;
    t.uval = t.uval*c;
    r.L64.L32.uval+=t.L32.uval;
    r.L64.H32.uval+=t.H32.uval;

    t.uval = this->L64.H32.uval;
    t.uval = t.uval*c;
    r.L64.H32.uval+=t.L32.uval;
    r.H64.L32.uval+=t.H32.uval;

    t.uval = this->H64.L32.uval;
    t.uval = t.uval*c;
    r.H64.L32.uval+=t.L32.uval;
    r.H64.H32.uval+=t.H32.uval;

    t.uval = this->H64.H32.uval;
    t.uval = t.uval*c;
    r.H64.H32.uval+=t.L32.uval;

    *this=r;
    return r;
}

inline TBits128 &TBits128::operator*=(const TBits128 &c)
{
    uint32_t u=-1;
    TBits64 t;
    TBits128 r,k;
    r.uvalL= k.uvalL=this->uvalL;
    r.uvalH= k.uvalH=this->uvalH;


    r*=c.uvalL;
    k*=c.uvalH;

    r.uvalH += k.uvalL ;

    *this=r;
    return r;
}
#endif

#pragma pack(0)

#endif // GLOBAL_INT_H

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值