2017-08-24 03:43:49 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
|
|
|
|
struct fixed_type {
|
|
|
|
char i;
|
|
|
|
unsigned char f;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-08-24 21:07:21 +00:00
|
|
|
static void double_to_fixed(double d, struct fixed_type *f) {
|
2017-08-24 03:43:49 +00:00
|
|
|
|
2017-08-24 17:30:19 +00:00
|
|
|
int temp;
|
2017-08-24 03:43:49 +00:00
|
|
|
|
2017-08-24 17:30:19 +00:00
|
|
|
temp=d*256;
|
2017-08-24 03:43:49 +00:00
|
|
|
|
2017-08-24 17:30:19 +00:00
|
|
|
f->i=(temp>>8)&0xff;
|
2017-08-24 03:43:49 +00:00
|
|
|
|
2017-08-24 17:30:19 +00:00
|
|
|
f->f=temp&0xff;
|
2017-08-24 03:43:49 +00:00
|
|
|
|
2017-08-24 21:07:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_fixed(struct fixed_type *f) {
|
|
|
|
|
2018-01-10 05:38:50 +00:00
|
|
|
printf("0x%02X,0x%02X",f->i,f->f);
|
2017-08-24 03:43:49 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 21:07:21 +00:00
|
|
|
static void fixed_to_double(struct fixed_type *f, double *d) {
|
2017-08-24 03:43:49 +00:00
|
|
|
|
|
|
|
*d=f->i;
|
|
|
|
*d+=((double)(f->f))/256.0;
|
|
|
|
|
2017-08-24 21:07:21 +00:00
|
|
|
// printf("(%02x.%02x)=%lf\n",f->i,f->f,*d);
|
2017-08-24 03:43:49 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-08-24 21:07:21 +00:00
|
|
|
static void fixed_add(struct fixed_type *x, struct fixed_type *y, struct fixed_type *z) {
|
2017-08-24 03:43:49 +00:00
|
|
|
int carry;
|
|
|
|
short sum;
|
|
|
|
|
|
|
|
sum=(short)(x->f)+(short)(y->f);
|
|
|
|
|
|
|
|
if (sum>=256) carry=1;
|
|
|
|
else carry=0;
|
|
|
|
|
|
|
|
z->f=sum&0xff;
|
|
|
|
|
|
|
|
z->i=x->i+y->i+carry;
|
|
|
|
}
|
|
|
|
|
2017-08-24 21:07:21 +00:00
|
|
|
static void fixed_mul(struct fixed_type *x, struct fixed_type *y, struct fixed_type *z) {
|
2017-08-24 17:30:19 +00:00
|
|
|
|
|
|
|
int a,b,c;
|
|
|
|
|
|
|
|
a=((x->i)<<8)+(x->f);
|
|
|
|
b=((y->i)<<8)+(y->f);
|
|
|
|
|
|
|
|
c=a*b;
|
2017-08-24 21:07:21 +00:00
|
|
|
// printf("%x %x %x\n",a,b,c);
|
2017-08-24 17:30:19 +00:00
|
|
|
|
|
|
|
c>>=8;
|
|
|
|
|
|
|
|
z->i=(c>>8);
|
|
|
|
z->f=(c&0xff);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-24 03:43:49 +00:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
|
|
|
|
struct fixed_type f,fa,fb,fc;
|
|
|
|
double d,c;
|
2017-08-24 18:01:16 +00:00
|
|
|
int i;
|
2017-08-24 03:43:49 +00:00
|
|
|
|
2017-08-24 21:07:21 +00:00
|
|
|
printf("Testing positive:\n");
|
|
|
|
|
|
|
|
printf("\tConverting 4.25 to fixed: ");
|
2017-08-24 03:43:49 +00:00
|
|
|
double_to_fixed(4.25,&f);
|
|
|
|
fixed_to_double(&f,&d);
|
2017-08-24 21:07:21 +00:00
|
|
|
print_fixed(&f);
|
|
|
|
printf(" %lf\n",d);
|
2017-08-24 03:43:49 +00:00
|
|
|
|
2017-08-24 21:07:21 +00:00
|
|
|
printf("\tConverting 3.14159265358979 to fixed: ");
|
2017-08-24 03:43:49 +00:00
|
|
|
double_to_fixed(3.14159265358979,&f);
|
|
|
|
fixed_to_double(&f,&d);
|
2017-08-24 21:07:21 +00:00
|
|
|
print_fixed(&f);
|
|
|
|
printf(" %lf\n",d);
|
2017-08-24 03:43:49 +00:00
|
|
|
|
2017-08-24 21:07:21 +00:00
|
|
|
printf("\tConverting 127.333333333 to fixed: ");
|
2017-08-24 03:43:49 +00:00
|
|
|
double_to_fixed(127.333333333,&f);
|
|
|
|
fixed_to_double(&f,&d);
|
2017-08-24 21:07:21 +00:00
|
|
|
print_fixed(&f);
|
|
|
|
printf(" %lf\n",d);
|
2017-08-24 03:43:49 +00:00
|
|
|
|
2017-08-24 21:07:21 +00:00
|
|
|
printf("\tConverting 50.9 to fixed: ");
|
2017-08-24 03:43:49 +00:00
|
|
|
double_to_fixed(50.9,&f);
|
|
|
|
fixed_to_double(&f,&d);
|
2017-08-24 21:07:21 +00:00
|
|
|
print_fixed(&f);
|
|
|
|
printf(" %lf\n",d);
|
|
|
|
|
|
|
|
printf("Testing negative:\n");
|
|
|
|
|
|
|
|
printf("\tConverting -4.25 to fixed: ");
|
|
|
|
double_to_fixed(-4.25,&f);
|
|
|
|
fixed_to_double(&f,&d);
|
|
|
|
print_fixed(&f);
|
|
|
|
printf(" %lf\n",d);
|
|
|
|
|
|
|
|
printf("\tConverting -3.14159265358979 to fixed: ");
|
|
|
|
double_to_fixed(-3.14159265358979,&f);
|
|
|
|
fixed_to_double(&f,&d);
|
|
|
|
print_fixed(&f);
|
|
|
|
printf(" %lf\n",d);
|
2017-08-24 03:43:49 +00:00
|
|
|
|
2017-08-24 21:07:21 +00:00
|
|
|
printf("\tConverting -127.333333333 to fixed: ");
|
|
|
|
double_to_fixed(-127.333333333,&f);
|
|
|
|
fixed_to_double(&f,&d);
|
|
|
|
print_fixed(&f);
|
|
|
|
printf(" %lf\n",d);
|
2017-08-24 03:43:49 +00:00
|
|
|
|
2017-08-24 21:07:21 +00:00
|
|
|
printf("\tConverting -50.9 to fixed: ");
|
|
|
|
double_to_fixed(-50.9,&f);
|
|
|
|
fixed_to_double(&f,&d);
|
|
|
|
print_fixed(&f);
|
|
|
|
printf(" %lf\n",d);
|
|
|
|
|
|
|
|
printf("Testing addition:\n");
|
|
|
|
|
|
|
|
printf("\t2.75 + 12.75 = %lf, ",2.75+12.75);
|
2017-08-24 03:43:49 +00:00
|
|
|
double_to_fixed(2.75,&fa);
|
|
|
|
double_to_fixed(12.75,&fb);
|
|
|
|
fixed_add(&fa,&fb,&fc);
|
|
|
|
fixed_to_double(&fc,&c);
|
2017-08-24 21:07:21 +00:00
|
|
|
print_fixed(&fc);
|
|
|
|
printf(" %lf\n",c);
|
2017-08-24 03:43:49 +00:00
|
|
|
|
2017-08-24 21:07:21 +00:00
|
|
|
printf("\t-1.25 + -1.75 = %lf, ",-1.25 + -1.75);
|
2017-08-24 03:43:49 +00:00
|
|
|
double_to_fixed(-1.25,&fa);
|
|
|
|
double_to_fixed(-1.75,&fb);
|
|
|
|
fixed_add(&fa,&fb,&fc);
|
|
|
|
fixed_to_double(&fc,&c);
|
2017-08-24 21:07:21 +00:00
|
|
|
print_fixed(&fc);
|
|
|
|
printf(" %lf\n",c);
|
|
|
|
|
|
|
|
printf("\t5.0 + -4.1 = %lf, ",5.0 + -4.1);
|
|
|
|
double_to_fixed(5.0,&fa);
|
|
|
|
double_to_fixed(-4.1,&fb);
|
|
|
|
fixed_add(&fa,&fb,&fc);
|
|
|
|
fixed_to_double(&fc,&c);
|
|
|
|
print_fixed(&fc);
|
|
|
|
printf(" %lf\n",c);
|
|
|
|
|
|
|
|
printf("Testing multiplication:\n");
|
2017-08-24 03:43:49 +00:00
|
|
|
|
2017-08-24 21:07:21 +00:00
|
|
|
printf("\t2.5 * 2.5 = %lf, ",2.5 * 2.5);
|
2017-08-24 17:30:19 +00:00
|
|
|
double_to_fixed(2.5,&fa);
|
|
|
|
double_to_fixed(2.5,&fb);
|
|
|
|
fixed_mul(&fa,&fb,&fc);
|
|
|
|
fixed_to_double(&fc,&c);
|
2017-08-24 21:07:21 +00:00
|
|
|
print_fixed(&fc);
|
|
|
|
printf(" %lf\n",c);
|
2017-08-24 17:30:19 +00:00
|
|
|
|
2017-08-24 21:07:21 +00:00
|
|
|
printf("\t-1.1 * -1.1 = %lf, ",-1.1 * -1.1);
|
2017-08-24 17:30:19 +00:00
|
|
|
double_to_fixed(-1.1,&fa);
|
|
|
|
double_to_fixed(-1.1,&fb);
|
|
|
|
fixed_mul(&fa,&fb,&fc);
|
|
|
|
fixed_to_double(&fc,&c);
|
2017-08-24 21:07:21 +00:00
|
|
|
print_fixed(&fc);
|
|
|
|
printf(" %lf\n",c);
|
2017-08-24 17:30:19 +00:00
|
|
|
|
2017-08-24 21:07:21 +00:00
|
|
|
printf("\t-2 * 5 = %lf, ",-2.0 * 5.0);
|
2017-08-24 17:30:19 +00:00
|
|
|
double_to_fixed(-2.0,&fa);
|
|
|
|
double_to_fixed(5.0,&fb);
|
|
|
|
fixed_mul(&fa,&fb,&fc);
|
|
|
|
fixed_to_double(&fc,&c);
|
2017-08-24 21:07:21 +00:00
|
|
|
print_fixed(&fc);
|
|
|
|
printf(" %lf\n",c);
|
2017-08-24 17:30:19 +00:00
|
|
|
|
2017-08-24 21:07:21 +00:00
|
|
|
printf("\t13.2 * -0.5 = %lf, ",13.2 * -0.5);
|
2017-08-24 17:30:19 +00:00
|
|
|
double_to_fixed(13.2,&fa);
|
|
|
|
double_to_fixed(-0.5,&fb);
|
|
|
|
fixed_mul(&fa,&fb,&fc);
|
|
|
|
fixed_to_double(&fc,&c);
|
2017-08-24 21:07:21 +00:00
|
|
|
print_fixed(&fc);
|
|
|
|
printf(" %lf\n",c);
|
2017-08-24 03:43:49 +00:00
|
|
|
|
2018-01-10 05:38:50 +00:00
|
|
|
int sin_steps=64;
|
|
|
|
|
|
|
|
printf("Sine table %d steps\n",sin_steps);
|
|
|
|
for(i=0;i<sin_steps;i++) {
|
|
|
|
d=sin((double)i/sin_steps*3.1415926535897932384*2.0);
|
|
|
|
double_to_fixed(d,&fa);
|
|
|
|
print_fixed(&fa);
|
|
|
|
printf("// %lf\n",d);
|
2017-08-24 18:01:16 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 21:07:21 +00:00
|
|
|
double space_z=0.5;
|
|
|
|
double coach_z;
|
|
|
|
double horizon=-2.0;
|
|
|
|
|
2017-11-26 20:12:21 +00:00
|
|
|
printf("horizontal_scale_lookup_20:\n");
|
2017-08-24 21:07:21 +00:00
|
|
|
for(space_z=0.5;space_z<7.5;space_z+=1.0) {
|
|
|
|
printf("{");
|
|
|
|
for(i=8;i<40;i+=2) {
|
|
|
|
coach_z=space_z/((double)i-horizon);
|
|
|
|
double_to_fixed(coach_z,&fa);
|
|
|
|
if (fa.i!=0) {
|
|
|
|
printf("CRITICAL ERROR TOP NOT 0\n");
|
|
|
|
}
|
|
|
|
printf("0x%02X,",fa.f);
|
|
|
|
|
|
|
|
}
|
|
|
|
printf("},\n");
|
|
|
|
}
|
|
|
|
|
2017-11-26 20:12:21 +00:00
|
|
|
printf("horizontal_scale_lookup_40:\n");
|
|
|
|
for(space_z=0.5;space_z<7.5;space_z+=1.0) {
|
|
|
|
printf("{");
|
|
|
|
for(i=8;i<40;i+=1) {
|
|
|
|
coach_z=space_z/((double)i-horizon);
|
|
|
|
double_to_fixed(coach_z,&fa);
|
|
|
|
if (fa.i!=0) {
|
|
|
|
printf("CRITICAL ERROR TOP NOT 0\n");
|
|
|
|
}
|
|
|
|
printf("0x%02X,",fa.f);
|
|
|
|
|
|
|
|
}
|
|
|
|
printf("},\n");
|
|
|
|
}
|
|
|
|
|
2017-08-24 21:07:21 +00:00
|
|
|
for(coach_z=-1.0;coach_z<1.0;coach_z+=0.1) {
|
|
|
|
double_to_fixed(coach_z,&fa);
|
|
|
|
printf("%lf\t%02X,%02X\n",coach_z,fa.i,fa.f);
|
|
|
|
}
|
|
|
|
|
2017-08-24 03:43:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|