Skip to content
Snippets Groups Projects
Commit c8cc2c90 authored by Sangbaek Lee's avatar Sangbaek Lee
Browse files

test fx

parent 201765b4
No related branches found
No related tags found
No related merge requests found
#include<stdio.h>
#include"fx_s4716_double.h"
#define _USE_MATH_DEFINES
void fx_to_double(fx_s4716 a){
double f2d=FX_TO_DOUBLE(a);
printf("fx to double: %.16lf\n",f2d);
}
void double_to_fx(double a){
double d2f=DOUBLE_TO_FX(a);
printf("double to fx: %.16lf\n",d2f);
}
void fx_add(fx_s4716 a, fx_s4716 b){
double add=FX_S4716_DOUBLE_ADD(a,b);
printf("fx add: %.16lf\n",add);
}
void fx_sub(fx_s4716 a, fx_s4716 b){
double sub=FX_S4716_DOUBLE_SUB(a,b);
printf("fx sub : %.16lf\n",sub);
}
void fx_mul(fx_s4716 a, fx_s4716 b){
double mul=FX_S4716_DOUBLE_MUL(a,b);
printf("fx mul : %.16lf\n",mul);
}
void fx_div(fx_s4716 a, fx_s4716 b){
double div=FX_S4716_DOUBLE_DIV(a,b);
printf("fx div : %.16lf\n",div);
}
void fx_pi(){
printf("pi : %.16lf\n",FX_PI);
}
void fx_inv_pi(){
printf("inverse pi : %.16lf\n",FX_INV_PI);
}
void fx_sqrt(fx_s4716 a){
double square=FX_S4716_DOUBLE_SQRT(a);
printf("fx sqrt : %.16lf\n",square);
}
void fx_pow(fx_s4716 a){
double power=FX_S4716_DOUBLE_POW(a);
printf("fx pow : %.16lf\n",power);
}
#include <math.h> #include <math.h>
// #define f_POWER_2_16 65536.0
// #define fx_to_double(a) a/f_POWER_2_16
// #define double_to_fx(a) (long long)(a*f_POWER_2_16)
// #define fx_s4716_double_add(a, b) ((a)+(b))
// #define fx_s4716_double_sub(a, b) ((a)-(b))
// #define fx_s4716_double_mul(a, b) (double_to_fx(fx_to_double(a)*fx_to_double(b))
// #define fx_s4716_double_div(a, b) (double_to_fx(fx_to_double(a)/fx_to_double(b))
// #define fx_PI double_to_fx(3.141592653)
// #define fx_s4716_double_sqrt(a) (sqrt(a)/(f_POWER_2_16/2))
// #define fx_s4716_double_pow(a) (pow((double)a, 2.0)*f_POWER_2_16)
typedef long long fx_s4716; typedef long long fx_s4716;
#define f_POWER_2_16 65536.0 #define F_POWER_2_16 65536.0
#define F_POWER_2_8 256.0
#define fx_to_double(a) a/f_POWER_2_16 #define FX_TO_DOUBLE(a) (a)/(F_POWER_2_16)
#define double_to_fx(a) (long long)(a*f_POWER_2_16) #define DOUBLE_TO_FX(a) ((a)*(F_POWER_2_16))
//#define DOUBLE_TO_FX(a) (long long)((a)*(F_POWER_2_16))
#define fx_s4716_double_add(a, b) ((a)+(b)) #define FX_S4716_DOUBLE_ADD(a, b) ((a)+(b))
#define fx_s4716_double_sub(a, b) ((a)-(b)) #define FX_S4716_DOUBLE_SUB(a, b) ((a)-(b))
#define fx_s4716_double_mul(a, b) (double_to_fx(fx_to_double(a)*fx_to_double(b)) #define FX_S4716_DOUBLE_MUL(a, b) (DOUBLE_TO_FX((FX_TO_DOUBLE(a))*(FX_TO_DOUBLE(b))))
#define fx_s4716_double_div(a, b) (double_to_fx(fx_to_double(a)/fx_to_double(b)) #define FX_S4716_DOUBLE_DIV(a, b) (DOUBLE_TO_FX((FX_TO_DOUBLE(a))/(FX_TO_DOUBLE(b))))
#define fx_PI double_to_fx(3.141592653) #define FX_PI (DOUBLE_TO_FX(M_PI))
#define fx_s4716_double_sqrt(a) (sqrt(a)/(f_POWER_2_16/2)) #define FX_INV_PI (DOUBLE_TO_FX(1/(M_PI)))
#define fx_s4716_double_pow(a) (pow((double)a, 2.0)*f_POWER_2_16) #define FX_S4716_DOUBLE_SQRT(a) ((sqrt(a))/(F_POWER_2_8))
#define FX_S4716_DOUBLE_POW(a) ((pow((double)a, 2.0))*(F_POWER_2_16))
File added
test.c 0 → 100644
#include<stdio.h>
#include "fx_s4716_double.h"
extern void fx_to_double(fx_s4716 a);
extern void double_to_fx(double a);
extern void fx_add(fx_s4716 a, fx_s4716 b);
extern void fx_sub(fx_s4716 a, fx_s4716 b);
extern void fx_mul(fx_s4716 a, fx_s4716 b);
extern void fx_div(fx_s4716 a, fx_s4716 b);
extern void fx_pi();
extern void fx_inv_pi();
extern void fx_sqrt(fx_s4716 a);
extern void fx_pow(fx_s4716 a);
int main()
{
fx_s4716 a,b;
double c;
int option;
printf("This is program to test fixed point arithmetic\n");
printf(" OPTION \n");
printf("0. add \n");
printf("1. subtract \n");
printf("2. multiply \n");
printf("3. divide \n");
printf("4. square root \n");
printf("5. power \n");
printf("6. print PI \n");
printf("7. print inverse PI \n");
printf("8. fix to double \n");
printf("9. double to fix \n");
printf("10. quit\n\n");
printf("Type option : ");
fflush(stdout);
scanf("%d", &option);
switch (option)
{
case 0:
printf("Input two number : "); fflush(stdout);
scanf("%lld %lld", &a, &b);
fx_add(a, b);
break;
case 1:
printf("Input two number : "); fflush(stdout);
scanf("%lld %lld", &a, &b);
fx_sub(a, b);
break;
case 2:
printf("Input two number : "); fflush(stdout);
scanf("%lld %lld", &a, &b);
fx_mul(a, b);
break;
case 3:
printf("Input two number : "); fflush(stdout);
scanf("%lld %lld", &a, &b);
fx_div(a, b);
break;
case 4:
printf("Input one number : "); fflush(stdout);
scanf("%lld", &a);
fx_sqrt(a);
break;
case 5:
printf("Input one number : "); fflush(stdout);
scanf("%lld", &a);
fx_pow(a);
break;
case 6:
fx_pi();
break;
case 7:
fx_inv_pi();
break;
case 8:
printf("Input fix number : "); fflush(stdout);
scanf("%lld",&a);
fx_to_double(a);
break;
case 9:
printf("Input double number : "); fflush(stdout);
scanf("%lf",&c);
double_to_fx(c);
break;
default:
break;
}
}
\ No newline at end of file
test.o 0 → 100644
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment