I wrote simple solution (using this work):
#include <math.h>
#include <stdint.h>
#define DOUBLE_PRECISION 53
/* DOUBLE PRECISION FLOATING-POINT TYPE WITH EXTENDED EXPONENT */
typedef struct Real {
double sig; //significand
long exp; //binary exponent
} real;
/* UNION FOR DIVISION DOUBLE BY 2^POW */
union DubleIntUnion
{
double dvalue;
uint64_t ivalue;
};
/* PLACE SIGNIFICAND OF REAL NUMBER IN RANGE [1, 2) */
inline real adjust(real x){
real y;
y.exp = x.exp;
y.sig = x.sig;
if(y.sig == 0){
y.exp = 0;
} else if (fabs(y.sig) >= 2.0){
y.exp = y.exp + 1;
y.sig = y.sig / 2;
} else if(fabs(y.sig) < 1){
y.exp = y.exp - 1;
y.sig = y.sig * 2;
}
return y;
}
/* PLACE SIGNIFICAND OF REAL NUMBER IN RANGE [1, 2) FOR TINY NUMBER */
/* FOR EXAMPLE, AFTER SUBTRATION OR WHEN SET REAL FROM DOUBLE */
inline real adjusttiny(real x){
real y;
y.exp = x.exp;
y.sig = x.sig;
while(1){
x.exp = y.exp;
x.sig = y.sig;
y = adjust(x);
if(x.exp == y.exp && x.sig == y.sig)
break;
}
return y;
}
real set(double x){
real y;
real z;
y.sig = x;
y.exp = 0;
return adjusttiny(y);
};
real set(real x){
real y;
y.exp = x.exp;
y.sig = x.sig;
return y;
};
/* ARITHMETIC OPERATIONS */
//divide x by 2^pow. Assert that x.exp - pow > e_min
inline double div2pow(const double x, const int pow)
{
DubleIntUnion diu;
diu.dvalue = x;
diu.ivalue -= (uint64_t)pow << 52; // subtract pow from exponent
return diu.dvalue;
}
//summation
inline real sum(real x, real y){
real sum;
int dexp = abs(x.exp - y.exp);
if (x.exp > y.exp){
sum.exp = x.exp;
if(dexp <= DOUBLE_PRECISION){
sum.sig = div2pow(y.sig, dexp); // divide y by 2^(x.exp - y.exp)
sum.sig = sum.sig + x.sig;
} else sum.sig = x.sig;
} else if (y.exp > x.exp){
sum.exp = y.exp;
if(dexp <= DOUBLE_PRECISION){
sum.sig = div2pow(x.sig, dexp); // divide x by 2^(y.exp - x.exp)
sum.sig = sum.sig + y.sig;
} else
sum.sig = y.sig;
} else {
sum.exp = x.exp;
sum.sig = x.sig + y.sig;
}
return adjust(sum);
}
//subtraction
inline real sub(real x, real y){
real sub;
int dexp = abs(x.exp - y.exp);
if (x.exp > y.exp){
sub.exp = x.exp;
if(dexp <= DOUBLE_PRECISION){
sub.sig = div2pow(y.sig, dexp); // divide y by 2^(x.exp - y.exp)
sub.sig = x.sig - sub.sig;
} else sub.sig = x.sig;
} else if (y.exp > x.exp){
sub.exp = y.exp;
if(dexp <= DOUBLE_PRECISION){
sub.sig = div2pow(x.sig, dexp); // divide x by 2^(y.exp - x.exp)
sub.sig = sub.sig - y.sig;
} else sub.sig = -y.sig;
} else {
sub.exp = x.exp;
sub.sig = x.sig - y.sig;
}
return adjusttiny(sub);
}
//multiplication
inline real mul(real x, real y){
real product;
product.exp = x.exp + y.exp;
product.sig = x.sig * y.sig;
return adjust(product);
}
//division
inline real div(real x, real y){
real quotient;
quotient.exp = x.exp - y.exp;
quotient.sig = x.sig / y.sig;
return adjust(quotient);
}
At first glance is working correctly. Maybe I missed something or that implementation can be accelerated?
How I can implement functions floor
and ceil
on such numbers?