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);
}
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);
}