J
jack
Hi all,
I have problem in creating a 128 bit integer with 4 unsigned
integers.
My first problem is with shift right and left bits. The function
shiftleft64 , shifts one bit by left. Shiftleft64 works perfectly
well. I did shiftleft128 in the same way, but it doesn't work the way
it has to.
second problem is, i implemented multiplication with 2 unsigned
integers (4*16 bit) for uint64. it works fine. for 128 bit integer, it
does nt work properly since i get 8 unsigned integers (8*16). I have
no idea to do with 8*16bit. The system is old and it supports only
upto 32bit integers. I tried hard, and atlast i posted in to groups.
It would be great if any of you could help me in this issue. Thank
you all.
My implementation is as follows:
#define BIT64 (0x80000000ULL)
typedef struct {
unsigned int lo;
unsigned int hi;
} uint64;
typedef struct {
uint64 lo;
uint64 hi;
} uint128;
uint64
shiftleft64 (uint64 x)
{
unsigned long sbit = x.lo & BIT64;
x.hi <<= 1;
x.lo <<= 1;
if (sbit)
{
x.hi |= 1;
return x;
}
return x;
}
uint128
shiftleft128 (uint128 x)
{
uint64 sbit;
sbit.lo = x.lo.lo & BIT64;
sbit.hi = x.lo.hi & BIT64;
x.hi = shiftleft64(x.hi);
x.lo = shiftleft64(x.lo);
if ((sbit.lo) || (sbit.hi))
{
x.hi.lo |=1;
x.hi.hi |=1;
return x;
}
return x;
}
uint64
mult64 (unsigned int a, unsigned int b)
{
uint64 product;
unsigned int a0, a1;
unsigned int b0, b1;
unsigned int d, d0, d1;
unsigned int e, e0, e1;
unsigned int f, f0, f1;
unsigned int g, g0, g1;
unsigned int sum, carry, roll, pmax;
a1 = a >> 16;
a0 = a - (a1 << 16);
b1 = b >> 16;
b0 = b - (b1 << 16);
d = a0 * b0;
d1 = d >> 16;
d0 = d - (d1 << 16);
e = a0 * b1;
e1 = e >> 16;
e0 = e - (e1 << 16);
f = a1 * b0;
f1 = f >> 16;
f0 = f - (f1 << 16);
g = a1 * b1;
g1 = g >> 16;
g0 = g - (g1 << 16);
sum = d1 + e0 + f0;
carry = 0;
roll = 1 << 14;
roll <<= 2;
pmax = roll - 1;
while (pmax < sum)
{
sum -= roll;
carry ++;
}
product.lo = d0 + (sum << 16);
product.hi = carry + e1 + f1 + g0 + (g1 << 16);
return product;
}
I have problem in creating a 128 bit integer with 4 unsigned
integers.
My first problem is with shift right and left bits. The function
shiftleft64 , shifts one bit by left. Shiftleft64 works perfectly
well. I did shiftleft128 in the same way, but it doesn't work the way
it has to.
second problem is, i implemented multiplication with 2 unsigned
integers (4*16 bit) for uint64. it works fine. for 128 bit integer, it
does nt work properly since i get 8 unsigned integers (8*16). I have
no idea to do with 8*16bit. The system is old and it supports only
upto 32bit integers. I tried hard, and atlast i posted in to groups.
It would be great if any of you could help me in this issue. Thank
you all.
My implementation is as follows:
#define BIT64 (0x80000000ULL)
typedef struct {
unsigned int lo;
unsigned int hi;
} uint64;
typedef struct {
uint64 lo;
uint64 hi;
} uint128;
uint64
shiftleft64 (uint64 x)
{
unsigned long sbit = x.lo & BIT64;
x.hi <<= 1;
x.lo <<= 1;
if (sbit)
{
x.hi |= 1;
return x;
}
return x;
}
uint128
shiftleft128 (uint128 x)
{
uint64 sbit;
sbit.lo = x.lo.lo & BIT64;
sbit.hi = x.lo.hi & BIT64;
x.hi = shiftleft64(x.hi);
x.lo = shiftleft64(x.lo);
if ((sbit.lo) || (sbit.hi))
{
x.hi.lo |=1;
x.hi.hi |=1;
return x;
}
return x;
}
uint64
mult64 (unsigned int a, unsigned int b)
{
uint64 product;
unsigned int a0, a1;
unsigned int b0, b1;
unsigned int d, d0, d1;
unsigned int e, e0, e1;
unsigned int f, f0, f1;
unsigned int g, g0, g1;
unsigned int sum, carry, roll, pmax;
a1 = a >> 16;
a0 = a - (a1 << 16);
b1 = b >> 16;
b0 = b - (b1 << 16);
d = a0 * b0;
d1 = d >> 16;
d0 = d - (d1 << 16);
e = a0 * b1;
e1 = e >> 16;
e0 = e - (e1 << 16);
f = a1 * b0;
f1 = f >> 16;
f0 = f - (f1 << 16);
g = a1 * b1;
g1 = g >> 16;
g0 = g - (g1 << 16);
sum = d1 + e0 + f0;
carry = 0;
roll = 1 << 14;
roll <<= 2;
pmax = roll - 1;
while (pmax < sum)
{
sum -= roll;
carry ++;
}
product.lo = d0 + (sum << 16);
product.hi = carry + e1 + f1 + g0 + (g1 << 16);
return product;
}