写了几个16位编译环境下64位计算的函数

typedef union {
    struct {
        U32 Low;
        U32 High;
    } u;
#if defined(DT64BSUPPORTED)
    _U64 Value;
#endif
} U64;

#define LOWORD(x)            ((unsigned short)(x & 0xFFFF))
#define HIWORD(x)            ((unsigned short)(x >> 16))
#define MAKEDWORD(h,l)        (((unsigned long)h << 16) | l)

// written by LIYUE
// result = a + b
void UADD64(unsigned long a, unsigned long b, U64 &result)
{
 unsigned long tmp = LOWORD(a) + LOWORD(b);
  unsigned short low  = LOWORD(tmp);
  unsigned short add  = HIWORD(tmp);
  unsigned long  high = HIWORD(a) + HIWORD(b) + add;

  result.u.High = HIWORD(high);
  result.u.Low = MAKEDWORD(LOWORD(high), low);
}

// written by LIYUE
// result = a + b
void UADD64(U64 a, U64 b, U64 &result)
{
  U64 low, high;
  UADD64(a.u.Low, b.u.Low, low);
  UADD64(a.u.High, b.u.High, high);

  result.u.Low = low.u.Low;
  result.u.High = low.u.High + high.u.Low;
}

// written by LIYUE
// result = a * b
void UMUL64(unsigned long a, unsigned long b, U64 &result)
{
  unsigned long al = LOWORD(a);
  unsigned long ah = HIWORD(a);
  unsigned long bl = LOWORD(b);
  unsigned long bh = HIWORD(b);

  U64 mul1, mul2;
  UADD64( al * bl, (al * bh) << 16, mul1 );
  UADD64( ah * bl, (ah * bh) << 16, mul2 );
  mul2.u.High = HIWORD(mul2.u.Low);
  mul2.u.Low = mul2.u.Low << 16;

  UADD64(mul1, mul2, result);
}

// written by LIYUE
void ShiftLeft(U32 &high, U32 &low)
{
  unsigned add = low & 0x80000000 ? 1 : 0;
  high = high << 1;
  high |= add;
  low = low << 1;
}

// written by LIYUE
void ShiftRight(U32 &high, U32 &low)
{
  unsigned long add = high & 1 ? 0x80000000 : 0;
  low = low >> 1;
  low |= add;
  high = high >> 1;
}

// written by LIYUE
void ShiftLeft(U32 &high, U32 &low, unsigned bits)
{
  for (unsigned i = 0; i < bits; i++)
    ShiftLeft(high, low);
}

// written by LIYUE
void ShiftRight(U32 &high, U32 &low, unsigned bits)
{
  for (unsigned i = 0; i < bits; i++)
    ShiftRight(high, low);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值