Skip to content
Snippets Groups Projects
Commit f86d73ce authored by Nayoung Kim's avatar Nayoung Kim
Browse files

project

parent fbf11a34
No related branches found
No related tags found
No related merge requests found
Showing with 1257 additions and 0 deletions
#pragma once
// Fixed with signed long long
#define FX_S28_35 ((1<<16) | (28<<8) | (35))
#define FX_S29_34 ((1<<16) | (29<<8) | (34))
#define FX_S30_33 ((1<<16) | (30<<8) | (33))
#define FX_S31_32 ((1<<16) | (31<<8) | (32))
#define FX_S32_31 ((1<<16) | (32<<8) | (31))
#define FX_S33_30 ((1<<16) | (33<<8) | (30))
#define FX_S34_29 ((1<<16) | (34<<8) | (29))
#define FX_S35_28 ((1<<16) | (35<<8) | (28))
// Fixed with unsigned long long
#define FX_U29_35 ((0<<16) | (29<<8) | (35))
#define FX_U30_34 ((0<<16) | (30<<8) | (34))
#define FX_U31_33 ((0<<16) | (31<<8) | (33))
#define FX_U32_32 ((0<<16) | (32<<8) | (32))
#define FX_U33_31 ((0<<16) | (33<<8) | (31))
#define FX_U34_30 ((0<<16) | (34<<8) | (30))
#define FX_U35_29 ((0<<16) | (35<<8) | (29))
#define FX_U36_28 ((0<<16) | (36<<8) | (28))
// Fixed with signed int
#define FX_S12_19 ((1<<16) | (12<<8) | (19))
#define FX_S13_18 ((1<<16) | (13<<8) | (18))
#define FX_S14_17 ((1<<16) | (14<<8) | (17))
#define FX_S15_16 ((1<<16) | (15<<8) | (16))
#define FX_S16_15 ((1<<16) | (16<<8) | (15))
#define FX_S17_14 ((1<<16) | (17<<8) | (14))
#define FX_S18_13 ((1<<16) | (18<<8) | (13))
#define FX_S19_12 ((1<<16) | (19<<8) | (12))
// Fixed with unsigned int
#define FX_S13_19 ((0<<16) | (13<<8) | (19))
#define FX_S14_18 ((0<<16) | (14<<8) | (18))
#define FX_S15_17 ((0<<16) | (15<<8) | (17))
#define FX_S16_16 ((0<<16) | (16<<8) | (16))
#define FX_S17_15 ((0<<16) | (17<<8) | (15))
#define FX_S18_14 ((0<<16) | (18<<8) | (14))
#define FX_S19_13 ((0<<16) | (19<<8) | (13))
#define FX_S20_12 ((0<<16) | (20<<8) | (12))
// Fixed with signed short
#define FX_S04_11 ((1<<16) | (4<<8) | (11))
#define FX_S05_10 ((1<<16) | (5<<8) | (10))
#define FX_S06_09 ((1<<16) | (6<<8) | (9))
#define FX_S07_08 ((1<<16) | (7<<8) | (8))
#define FX_S08_07 ((1<<16) | (8<<8) | (7))
#define FX_S09_06 ((1<<16) | (9<<8) | (6))
#define FX_S10_05 ((1<<16) | (10<<8) | (5))
#define FX_S11_04 ((1<<16) | (11<<8) | (4))
// Fixed with unsigned short
#define FX_S05_11 ((0<<16) | (5<<8) | (11))
#define FX_S06_10 ((0<<16) | (6<<8) | (10))
#define FX_S07_09 ((0<<16) | (7<<8) | (9))
#define FX_S08_08 ((0<<16) | (8<<8) | (8))
#define FX_S09_07 ((0<<16) | (9<<8) | (7))
#define FX_S10_06 ((0<<16) | (10<<8) | (6))
#define FX_S11_05 ((0<<16) | (11<<8) | (5))
#define FX_S12_04 ((0<<16) | (12<<8) | (4))
// Fixed with signed char
#define FX_S00_07 ((1<<16) | (0<<8) | (7))
#define FX_S01_06 ((1<<16) | (1<<8) | (6))
#define FX_S02_05 ((1<<16) | (2<<8) | (5))
#define FX_S03_04 ((1<<16) | (3<<8) | (4))
#define FX_S04_03 ((1<<16) | (4<<8) | (3))
#define FX_S05_02 ((1<<16) | (5<<8) | (2))
#define FX_S06_01 ((1<<16) | (6<<8) | (1))
// #define FX_S07_00 ((1<<16) | (7<<8) | (0)) // It is same as unsigned char
// Fixed with unsigned char
#define FX_S00_08 ((0<<16) | (0<<8) | (8))
#define FX_S01_07 ((0<<16) | (1<<8) | (7))
#define FX_S02_06 ((0<<16) | (2<<8) | (6))
#define FX_S03_05 ((0<<16) | (3<<8) | (5))
#define FX_S04_04 ((0<<16) | (4<<8) | (4))
#define FX_S05_03 ((0<<16) | (5<<8) | (3))
#define FX_S06_02 ((0<<16) | (6<<8) | (2))
#define FX_S07_01 ((0<<16) | (7<<8) | (1))
// Operation Preference
#define FX_OP_FLOAT 1
#define FX_OP_PRECISION 2
#define FX_OP_FAIR 3
#define FX_OP_PERFORMANCE 4
#include "fx_head.h"
#include "fx_s17_14.h"
#define fxADD(a,b) ((a)+(b))
#define fxSUB(a,b) ((a)-(b))
#define FX_Q_NUM_H (FX_Q_NUM/2)
#define FX_Q_NUM_HA (FX_Q_NUM_H-FX_Q_NUM_H/2)
#define FX_Q_NUM_HB (FX_Q_NUM_H/2)
/*
extern fixed fxAdd_float(), fxAdd_precision(), fxAdd_fair(), fxAdd_performance();
extern fixed fxSub_float(), fxSub_precision(), fxSub_fair(), fxSub_performance();
extern fixed fxMul_float(), fxMul_precision(), fxMul_fair(), fxMul_performance();
extern fixed fxDiv_float(), fxDiv_precision(), fxDiv_fair(), fxDiv_performance();
*/
// float function
fixed fxAdd_float(fixed a, fixed b)
{
return fromDouble(toDouble(a) + toDouble(b));
}
fixed fxSub_float(fixed a, fixed b)
{
return fromDouble(toDouble(a) - toDouble(b));
}
fixed fxMul_float(fixed a, fixed b)
{
return fromDouble(toDouble(a) * toDouble(b));
}
fixed fxDiv_float(fixed a, fixed b)
{
return fromDouble(toDouble(a) / toDouble(b));
}
// precision function
fixed fxAdd_precision(fixed a, fixed b)
{
return fromFloat(toFloat(a) + toFloat(b));
}
fixed fxSub_precision(fixed a, fixed b)
{
return fromFloat(toFloat(a) - toFloat(b));
}
fixed fxMul_precision(fixed a, fixed b)
{
long long la = a, lb = b;
return (la * lb) >> FX_Q_NUM;
}
fixed fxDiv_precision(fixed a, fixed b)
{
long long la = a, lb = b;
return (la << FX_Q_NUM) / lb;
}
// fair function
fixed fxAdd_fair(fixed a, fixed b)
{
return fxADD(a,b);
}
fixed fxSub_fair(fixed a, fixed b)
{
return fxSUB(a,b);
}
fixed fxMul_fair(fixed a, fixed b)
{
long long la = a, lb = b;
return ((la >> FX_Q_NUM_HA) * (lb >> FX_Q_NUM_HB)) >> FX_Q_NUM_H;
}
fixed fxDiv_fair(fixed a, fixed b)
{
long long la = a, lb = b;
return ((la << FX_Q_NUM_H) / lb) << FX_Q_NUM_H;
}
// performance function
fixed fxAdd_performance(fixed a, fixed b)
{
return fxADD(a,b);
}
fixed fxSub_performance(fixed a, fixed b)
{
return fxSUB(a,b);
}
fixed fxMul_performance(fixed a, fixed b)
{
long long la = a, lb = b;
return (la>>FX_Q_NUM_H)*(lb>>FX_Q_NUM_H);
}
fixed fxDiv_performance(fixed a, fixed b)
{
long long la = a, lb = b;
return ((la << FX_Q_NUM_H) / (lb >> FX_Q_NUM_H));
}
#include "fx_head.h"
#include <math.h>
#define FX_POINT FX_S17_14
#define FX_Q_NUM (FX_POINT & 0xFF) // 14
// If you want calculate with high precision set 64
#define FX_SYSTEM_INTEGER 64 // 32 or 64
#define FX_SYSTEM_FLOAT 64 // 32 or 64
#define FX_DATA_TYPE signed int
typedef int fx_s17_14;
typedef fx_s17_14 fixed;
#define fromDouble(d) ((fixed)((d)*DOUBLE_Q_VALUE))
#define toDouble(d) ((double)(d)*DOUBLE_1_Q_VALUE)
#define fromFloat(d) ((fixed)((d)*FLOAT_Q_VALUE))
#define toFloat(d) ((float)(d)*FLOAT_1_Q_VALUE)
// CONSTANTS
#define FLOAT_Q_VALUE (float)(1<<FX_Q_NUM)
#define DOUBLE_Q_VALUE (double)(1<<FX_Q_NUM)
#define FLOAT_1_Q_VALUE (float)(1.0f/FLOAT_Q_VALUE)
#define DOUBLE_1_Q_VALUE (double)(1.0f/DOUBLE_Q_VALUE)
#define FX_PI fromDOUBLE(M_PI)
// One of FX_OP_FLOAT, FX_OP_PRECISION, FX_OP_FAIR, FX_OP_PERFORMANCE
#define FX_OP_PREFERENCE FX_OP_FLOAT
extern fixed fxAdd_float(), fxAdd_precision(), fxAdd_fair(), fxAdd_performance();
extern fixed fxSub_float(), fxSub_precision(), fxSub_fair(), fxSub_performance();
extern fixed fxMul_float(), fxMul_precision(), fxMul_fair(), fxMul_performance();
extern fixed fxDiv_float(), fxDiv_precision(), fxDiv_fair(), fxDiv_performance();
#include <stdio.h>
// #### #### #### #### . #### #### #### ####
// S 15 . 16
#define FX_Q_NUM 16
#define FX_2_MINUS_16 1.52587890625e-05F
#define FX_2_PLUS_16 (1<<16)
#define FX_S_15_16 11516
#define FX_SYSTEM FX_S_15_16
typedef int fixed32;
fixed32 fromFloat(float fa)
{
return (fixed32) (fa * FX_2_PLUS_16);
}
float toFloat(fixed32 xa)
{
return ((float) (xa)) * FX_2_MINUS_16;
}
fixed32 fxAdd(fixed32 a, fixed32 b)
{
return fromFloat(toFloat(a)+toFloat(b));
}
fixed32 fxAdd2(fixed32 a, fixed32 b)
{
return a+b;
}
int main()
{
int i = 0;
int ia, ib, ic, ic2;
float fa;
// fscanf(stdin, "%d %d", &ia, &ib);
for (i = 0; i < 5000000; i++){
ic = fxAdd(i, i);
ic2 = fxAdd2(i, i);
fprintf(stdout, "%f + %f : %f, %f diff = %d \n", toFloat(i), toFloat(i), toFloat(ic), toFloat(ic2), ic-ic2);
}
// fprintf(stdout, "%d + %d : %d \n", ia, ib, ic);
// fprintf(stdout, "%f + %f : %f \n", toFloat(ia), toFloat(ib), toFloat(ic));
// fprintf(stdout, "%d + %d : %d \n", ia, ib, ic);
}
Flat profile:
Each sample counts as 0.01 seconds.
no time accumulated
% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
0.00 0.00 0.00 1 0.00 0.00 fxAdd_float
0.00 0.00 0.00 1 0.00 0.00 fxDiv_float
0.00 0.00 0.00 1 0.00 0.00 fxMul_float
0.00 0.00 0.00 1 0.00 0.00 fxSub_float
% the percentage of the total running time of the
time program used by this function.
cumulative a running sum of the number of seconds accounted
seconds for by this function and those listed above it.
self the number of seconds accounted for by this
seconds function alone. This is the major sort for this
listing.
calls the number of times this function was invoked, if
this function is profiled, else blank.
self the average number of milliseconds spent in this
ms/call function per call, if this function is profiled,
else blank.
total the average number of milliseconds spent in this
ms/call function and its descendents per call, if this
function is profiled, else blank.
name the name of the function. This is the minor sort
for this listing. The index shows the location of
the function in the gprof listing. If the index is
in parenthesis it shows where it would appear in
the gprof listing if it were to be printed.
Copyright (C) 2012-2018 Free Software Foundation, Inc.
Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.
Call graph (explanation follows)
granularity: each sample hit covers 2 byte(s) no time propagated
index % time self children called name
0.00 0.00 1/1 main [22]
[1] 0.0 0.00 0.00 1 fxAdd_float [1]
-----------------------------------------------
0.00 0.00 1/1 main [22]
[2] 0.0 0.00 0.00 1 fxDiv_float [2]
-----------------------------------------------
0.00 0.00 1/1 main [22]
[3] 0.0 0.00 0.00 1 fxMul_float [3]
-----------------------------------------------
0.00 0.00 1/1 main [22]
[4] 0.0 0.00 0.00 1 fxSub_float [4]
-----------------------------------------------
This table describes the call tree of the program, and was sorted by
the total amount of time spent in each function and its children.
Each entry in this table consists of several lines. The line with the
index number at the left hand margin lists the current function.
The lines above it list the functions that called this function,
and the lines below it list the functions this one called.
This line lists:
index A unique number given to each element of the table.
Index numbers are sorted numerically.
The index number is printed next to every function name so
it is easier to look up where the function is in the table.
% time This is the percentage of the `total' time that was spent
in this function and its children. Note that due to
different viewpoints, functions excluded by options, etc,
these numbers will NOT add up to 100%.
self This is the total amount of time spent in this function.
children This is the total amount of time propagated into this
function by its children.
called This is the number of times the function was called.
If the function called itself recursively, the number
only includes non-recursive calls, and is followed by
a `+' and the number of recursive calls.
name The name of the current function. The index number is
printed after it. If the function is a member of a
cycle, the cycle number is printed between the
function's name and the index number.
For the function's parents, the fields have the following meanings:
self This is the amount of time that was propagated directly
from the function into this parent.
children This is the amount of time that was propagated from
the function's children into this parent.
called This is the number of times this parent called the
function `/' the total number of times the function
was called. Recursive calls to the function are not
included in the number after the `/'.
name This is the name of the parent. The parent's index
number is printed after it. If the parent is a
member of a cycle, the cycle number is printed between
the name and the index number.
If the parents of the function cannot be determined, the word
`<spontaneous>' is printed in the `name' field, and all the other
fields are blank.
For the function's children, the fields have the following meanings:
self This is the amount of time that was propagated directly
from the child into the function.
children This is the amount of time that was propagated from the
child's children to the function.
called This is the number of times the function called
this child `/' the total number of times the child
was called. Recursive calls by the child are not
listed in the number after the `/'.
name This is the name of the child. The child's index
number is printed after it. If the child is a
member of a cycle, the cycle number is printed
between the name and the index number.
If there are any cycles (circles) in the call graph, there is an
entry for the cycle-as-a-whole. This entry shows who called the
cycle (as parents) and the members of the cycle (as children.)
The `+' recursive calls entry shows the number of function calls that
were internal to the cycle, and the calls entry for each member shows,
for that member, how many times it was called from other members of
the cycle.
Copyright (C) 2012-2018 Free Software Foundation, Inc.
Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.
Index by function name
[1] fxAdd_float [3] fxMul_float
[2] fxDiv_float [4] fxSub_float
File added
#include "fx_s17_14.h"
#include <stdio.h>
int main()
{
double a,b;
fscanf(stdin, "%lf %lf", &a, &b);
fprintf(stdout, "\n");
fprintf(stdout, "fxAdd_float : a + b = %.15lf \n", toDouble(fxAdd_float(fromDouble(a), fromDouble(b))));
fprintf(stdout, "fxSub_float : a - b = %.15lf \n", toDouble(fxSub_float(fromDouble(a), fromDouble(b))));
fprintf(stdout, "fxMul_float : a * b = %.15lf \n", toDouble(fxMul_float(fromDouble(a), fromDouble(b))));
fprintf(stdout, "fxDiv_float : a / b = %.15lf \n", toDouble(fxDiv_float(fromDouble(a), fromDouble(b))));
fprintf(stdout, "------------------------------------------- \n");
fprintf(stdout, "fxAdd_precision : a + b = %.15lf \n", toFloat(fxAdd_precision(fromFloat(a), fromFloat(b))));
fprintf(stdout, "fxSub_precision : a - b = %.15lf \n", toFloat(fxSub_precision(fromFloat(a), fromFloat(b))));
fprintf(stdout, "fxMul_precision : a * b = %.15lf \n", toDouble(fxMul_precision(fromDouble(a), fromDouble(b))));
fprintf(stdout, "fxDiv_precision : a / b = %.15lf \n", toDouble(fxDiv_precision(fromDouble(a), fromDouble(b))));
fprintf(stdout, "------------------------------------------- \n");
fprintf(stdout, "fxAdd_fair : a + b = %.15lf \n", toDouble(fxAdd_fair(fromDouble(a), fromDouble(b))));
fprintf(stdout, "fxSub_fair : a - b = %.15lf \n", toDouble(fxSub_fair(fromDouble(a), fromDouble(b))));
fprintf(stdout, "fxMul_fair : a * b = %.15lf \n", toDouble(fxMul_fair(fromDouble(a), fromDouble(b))));
fprintf(stdout, "fxDiv_fair : a / b = %.15lf \n", toDouble(fxDiv_fair(fromDouble(a), fromDouble(b))));
fprintf(stdout, "------------------------------------------- \n");
fprintf(stdout, "fxAdd_performance : a + b = %.15lf \n", toDouble(fxAdd_performance(fromDouble(a), fromDouble(b))));
fprintf(stdout, "fxSub_performance : a - b = %.15lf \n", toDouble(fxSub_performance(fromDouble(a), fromDouble(b))));
fprintf(stdout, "fxMul_performance : a * b = %.15lf \n", toDouble(fxMul_performance(fromDouble(a), fromDouble(b))));
fprintf(stdout, "fxDiv_performance : a / b = %.15lf \n", toDouble(fxDiv_performance(fromDouble(a), fromDouble(b))));
return 0;
}
CC = gcc
CFLAGS = -c -o
OBJS = project_main.o fx_s17_14.o
HEAD = fx_s17_14.h
FILE = fx_s17_14.c
fx_s17_14.out: $(OBJS)
$(CC) $(OBJS) -o $@
project_main.o: project_main.c $(FILE) $(HEAD)
$(CC) project_main.c $(FILE) $(CFLAGS) $@
fx_s17_14.o: project_main.o $(FILE) $(HEAD)
$(CC) project_main.o $(FILE) $(CFLAGS) $@
clean:
rm $(OBJS) fx_s17_14.out
#pragma once
// Fixed with signed long long
#define FX_S28_35 ((1<<16) | (28<<8) | (35))
#define FX_S29_34 ((1<<16) | (29<<8) | (34))
#define FX_S30_33 ((1<<16) | (30<<8) | (33))
#define FX_S31_32 ((1<<16) | (31<<8) | (32))
#define FX_S32_31 ((1<<16) | (32<<8) | (31))
#define FX_S33_30 ((1<<16) | (33<<8) | (30))
#define FX_S34_29 ((1<<16) | (34<<8) | (29))
#define FX_S35_28 ((1<<16) | (35<<8) | (28))
// Fixed with unsigned long long
#define FX_U29_35 ((0<<16) | (29<<8) | (35))
#define FX_U30_34 ((0<<16) | (30<<8) | (34))
#define FX_U31_33 ((0<<16) | (31<<8) | (33))
#define FX_U32_32 ((0<<16) | (32<<8) | (32))
#define FX_U33_31 ((0<<16) | (33<<8) | (31))
#define FX_U34_30 ((0<<16) | (34<<8) | (30))
#define FX_U35_29 ((0<<16) | (35<<8) | (29))
#define FX_U36_28 ((0<<16) | (36<<8) | (28))
// Fixed with signed int
#define FX_S12_19 ((1<<16) | (12<<8) | (19))
#define FX_S13_18 ((1<<16) | (13<<8) | (18))
#define FX_S14_17 ((1<<16) | (14<<8) | (17))
#define FX_S15_16 ((1<<16) | (15<<8) | (16))
#define FX_S16_15 ((1<<16) | (16<<8) | (15))
#define FX_S17_14 ((1<<16) | (17<<8) | (14))
#define FX_S18_13 ((1<<16) | (18<<8) | (13))
#define FX_S19_12 ((1<<16) | (19<<8) | (12))
// Fixed with unsigned int
#define FX_S13_19 ((0<<16) | (13<<8) | (19))
#define FX_S14_18 ((0<<16) | (14<<8) | (18))
#define FX_S15_17 ((0<<16) | (15<<8) | (17))
#define FX_S16_16 ((0<<16) | (16<<8) | (16))
#define FX_S17_15 ((0<<16) | (17<<8) | (15))
#define FX_S18_14 ((0<<16) | (18<<8) | (14))
#define FX_S19_13 ((0<<16) | (19<<8) | (13))
#define FX_S20_12 ((0<<16) | (20<<8) | (12))
// Fixed with signed short
#define FX_S04_11 ((1<<16) | (4<<8) | (11))
#define FX_S05_10 ((1<<16) | (5<<8) | (10))
#define FX_S06_09 ((1<<16) | (6<<8) | (9))
#define FX_S07_08 ((1<<16) | (7<<8) | (8))
#define FX_S08_07 ((1<<16) | (8<<8) | (7))
#define FX_S09_06 ((1<<16) | (9<<8) | (6))
#define FX_S10_05 ((1<<16) | (10<<8) | (5))
#define FX_S11_04 ((1<<16) | (11<<8) | (4))
// Fixed with unsigned short
#define FX_S05_11 ((0<<16) | (5<<8) | (11))
#define FX_S06_10 ((0<<16) | (6<<8) | (10))
#define FX_S07_09 ((0<<16) | (7<<8) | (9))
#define FX_S08_08 ((0<<16) | (8<<8) | (8))
#define FX_S09_07 ((0<<16) | (9<<8) | (7))
#define FX_S10_06 ((0<<16) | (10<<8) | (6))
#define FX_S11_05 ((0<<16) | (11<<8) | (5))
#define FX_S12_04 ((0<<16) | (12<<8) | (4))
// Fixed with signed char
#define FX_S00_07 ((1<<16) | (0<<8) | (7))
#define FX_S01_06 ((1<<16) | (1<<8) | (6))
#define FX_S02_05 ((1<<16) | (2<<8) | (5))
#define FX_S03_04 ((1<<16) | (3<<8) | (4))
#define FX_S04_03 ((1<<16) | (4<<8) | (3))
#define FX_S05_02 ((1<<16) | (5<<8) | (2))
#define FX_S06_01 ((1<<16) | (6<<8) | (1))
// #define FX_S07_00 ((1<<16) | (7<<8) | (0)) // It is same as unsigned char
// Fixed with unsigned char
#define FX_S00_08 ((0<<16) | (0<<8) | (8))
#define FX_S01_07 ((0<<16) | (1<<8) | (7))
#define FX_S02_06 ((0<<16) | (2<<8) | (6))
#define FX_S03_05 ((0<<16) | (3<<8) | (5))
#define FX_S04_04 ((0<<16) | (4<<8) | (4))
#define FX_S05_03 ((0<<16) | (5<<8) | (3))
#define FX_S06_02 ((0<<16) | (6<<8) | (2))
#define FX_S07_01 ((0<<16) | (7<<8) | (1))
// Operation Preference
#define FX_OP_FLOAT 1
#define FX_OP_PRECISION 2
#define FX_OP_FAIR 3
#define FX_OP_PERFORMANCE 4
#include "fx_head.h"
#include "fx_s17_14.h"
#define fxADD(a,b) ((a)+(b))
#define fxSUB(a,b) ((a)-(b))
#define FX_Q_NUM_H (FX_Q_NUM/2)
#define FX_Q_NUM_HA (FX_Q_NUM_H-FX_Q_NUM_H/2)
#define FX_Q_NUM_HB (FX_Q_NUM_H/2)
/*
extern fixed fxAdd_float(), fxAdd_precision(), fxAdd_fair(), fxAdd_performance();
extern fixed fxSub_float(), fxSub_precision(), fxSub_fair(), fxSub_performance();
extern fixed fxMul_float(), fxMul_precision(), fxMul_fair(), fxMul_performance();
extern fixed fxDiv_float(), fxDiv_precision(), fxDiv_fair(), fxDiv_performance();
*/
// float function
fixed fxAdd_float(fixed a, fixed b)
{
return fromDouble(toDouble(a) + toDouble(b));
}
fixed fxSub_float(fixed a, fixed b)
{
return fromDouble(toDouble(a) - toDouble(b));
}
fixed fxMul_float(fixed a, fixed b)
{
return fromDouble(toDouble(a) * toDouble(b));
}
fixed fxDiv_float(fixed a, fixed b)
{
return fromDouble(toDouble(a) / toDouble(b));
}
// precision function
fixed fxAdd_precision(fixed a, fixed b)
{
return fromFloat(toFloat(a) + toFloat(b));
}
fixed fxSub_precision(fixed a, fixed b)
{
return fromFloat(toFloat(a) - toFloat(b));
}
fixed fxMul_precision(fixed a, fixed b)
{
long long la = a, lb = b;
return (la * lb) >> FX_Q_NUM;
}
fixed fxDiv_precision(fixed a, fixed b)
{
long long la = a, lb = b;
return (la << FX_Q_NUM) / lb;
}
// fair function
fixed fxAdd_fair(fixed a, fixed b)
{
return fxADD(a,b);
}
fixed fxSub_fair(fixed a, fixed b)
{
return fxSUB(a,b);
}
fixed fxMul_fair(fixed a, fixed b)
{
long long la = a, lb = b;
return ((la >> FX_Q_NUM_HA) * (lb >> FX_Q_NUM_HB)) >> FX_Q_NUM_H;
}
fixed fxDiv_fair(fixed a, fixed b)
{
long long la = a, lb = b;
return ((la << FX_Q_NUM_H) / lb) << FX_Q_NUM_H;
}
// performance function
fixed fxAdd_performance(fixed a, fixed b)
{
return fxADD(a,b);
}
fixed fxSub_performance(fixed a, fixed b)
{
return fxSUB(a,b);
}
fixed fxMul_performance(fixed a, fixed b)
{
long long la = a, lb = b;
return (la>>FX_Q_NUM_H)*(lb>>FX_Q_NUM_H);
}
fixed fxDiv_performance(fixed a, fixed b)
{
long long la = a, lb = b;
return ((la << FX_Q_NUM_H) / (lb >> FX_Q_NUM_H));
}
#include "fx_head.h"
#include <math.h>
#define FX_POINT FX_S17_14
#define FX_Q_NUM (FX_POINT & 0xFF) // 14
// If you want calculate with high precision set 64
#define FX_SYSTEM_INTEGER 64 // 32 or 64
#define FX_SYSTEM_FLOAT 64 // 32 or 64
#define FX_DATA_TYPE signed int
typedef int fx_s17_14;
typedef fx_s17_14 fixed;
#define fromDouble(d) ((fixed)((d)*DOUBLE_Q_VALUE))
#define toDouble(d) ((double)(d)*DOUBLE_1_Q_VALUE)
#define fromFloat(d) ((fixed)((d)*FLOAT_Q_VALUE))
#define toFloat(d) ((float)(d)*FLOAT_1_Q_VALUE)
// CONSTANTS
#define FLOAT_Q_VALUE (float)(1<<FX_Q_NUM)
#define DOUBLE_Q_VALUE (double)(1<<FX_Q_NUM)
#define FLOAT_1_Q_VALUE (float)(1.0f/FLOAT_Q_VALUE)
#define DOUBLE_1_Q_VALUE (double)(1.0f/DOUBLE_Q_VALUE)
#define FX_PI fromDOUBLE(M_PI)
// One of FX_OP_FLOAT, FX_OP_PRECISION, FX_OP_FAIR, FX_OP_PERFORMANCE
#define FX_OP_PREFERENCE FX_OP_FLOAT
extern fixed fxAdd_float(), fxAdd_precision(), fxAdd_fair(), fxAdd_performance();
extern fixed fxSub_float(), fxSub_precision(), fxSub_fair(), fxSub_performance();
extern fixed fxMul_float(), fxMul_precision(), fxMul_fair(), fxMul_performance();
extern fixed fxDiv_float(), fxDiv_precision(), fxDiv_fair(), fxDiv_performance();
#include "fx_s17_14.h"
#include <stdio.h>
/*
int main()
{
printf("%f : %d\n",1.0, fromDouble(1.0));
printf("%f : %f\n",1.3*0.3, toFloat(fxMul_float(fromDouble(1.3),fromDouble(0.3)))) ;
}
*/
int main()
{
double a,b;
int num;
fprintf(stdout, "FX_OP_FLOAT : 1 \n");
fprintf(stdout, "FX_OP_PRECISION : 2 \n");
fprintf(stdout, "FX_OP_FAIR : 3 \n");
fprintf(stdout, "FX_OP_PERFORMANCE : 4 \n");
fprintf(stdout, "QUIT : The others \n\n");
fprintf(stdout, "Choose calculate way you want : ");
fscanf(stdin, "%d", &num);
if(num!=1 && num!=2 && num!=3 && num!=4)
{
fprintf(stdout, "Thank you! \n");
return 0;
}
fprintf(stdout, "Input the First Number : ");
fscanf(stdin, "%lf", &a);
fprintf(stdout, "Input the Second Number : ");
fscanf(stdin, "%lf", &b);
fprintf(stdout, "\n");
switch (num)
{
case 1:
fprintf(stdout, "fxAdd_float : a + b = %.15lf \n", toDouble(fxAdd_float(fromDouble(a), fromDouble(b))));
fprintf(stdout, "fxSub_float : a - b = %.15lf \n", toDouble(fxSub_float(fromDouble(a), fromDouble(b))));
fprintf(stdout, "fxMul_float : a * b = %.15lf \n", toDouble(fxMul_float(fromDouble(a), fromDouble(b))));
fprintf(stdout, "fxDiv_float : a / b = %.15lf \n", toDouble(fxDiv_float(fromDouble(a), fromDouble(b))));
break;
case 2:
fprintf(stdout, "fxAdd_precision : a + b = %.15lf \n", toFloat(fxAdd_precision(fromFloat(a), fromFloat(b))));
fprintf(stdout, "fxSub_precision : a - b = %.15lf \n", toFloat(fxSub_precision(fromFloat(a), fromFloat(b))));
fprintf(stdout, "fxMul_precision : a * b = %.15lf \n", toFloat(fxMul_precision(fromFloat(a), fromFloat(b))));
fprintf(stdout, "fxDiv_precision : a / b = %.15lf \n", toFloat(fxDiv_precision(fromFloat(a), fromFloat(b))));
break;
case 3:
fprintf(stdout, "fxAdd_fair : a + b = %.15lf \n", toFloat(fxAdd_fair(fromFloat(a), fromFloat(b))));
fprintf(stdout, "fxSub_fair : a - b = %.15lf \n", toFloat(fxSub_fair(fromFloat(a), fromFloat(b))));
fprintf(stdout, "fxMul_fair : a * b = %.15lf \n", toFloat(fxMul_fair(fromFloat(a), fromFloat(b))));
fprintf(stdout, "fxDiv_fair : a / b = %.15lf \n", toFloat(fxDiv_fair(fromFloat(a), fromFloat(b))));
break;
case 4:
fprintf(stdout, "fxAdd_performance : a + b = %.15lf \n", toDouble(fxAdd_performance(fromDouble(a), fromDouble(b))));
fprintf(stdout, "fxSub_performance : a - b = %.15lf \n", toDouble(fxSub_performance(fromDouble(a), fromDouble(b))));
fprintf(stdout, "fxMul_performance : a * b = %.15lf \n", toDouble(fxMul_performance(fromDouble(a), fromDouble(b))));
fprintf(stdout, "fxDiv_performance : a / b = %.15lf \n", toDouble(fxDiv_performance(fromDouble(a), fromDouble(b))));
break;
default:
break;
}
return 0;
}
#pragma once
// Fixed with signed long long
#define FX_S28_35 ((1<<16) | (28<<8) | (35))
#define FX_S29_34 ((1<<16) | (29<<8) | (34))
#define FX_S30_33 ((1<<16) | (30<<8) | (33))
#define FX_S31_32 ((1<<16) | (31<<8) | (32))
#define FX_S32_31 ((1<<16) | (32<<8) | (31))
#define FX_S33_30 ((1<<16) | (33<<8) | (30))
#define FX_S34_29 ((1<<16) | (34<<8) | (29))
#define FX_S35_28 ((1<<16) | (35<<8) | (28))
// Fixed with unsigned long long
#define FX_U29_35 ((0<<16) | (29<<8) | (35))
#define FX_U30_34 ((0<<16) | (30<<8) | (34))
#define FX_U31_33 ((0<<16) | (31<<8) | (33))
#define FX_U32_32 ((0<<16) | (32<<8) | (32))
#define FX_U33_31 ((0<<16) | (33<<8) | (31))
#define FX_U34_30 ((0<<16) | (34<<8) | (30))
#define FX_U35_29 ((0<<16) | (35<<8) | (29))
#define FX_U36_28 ((0<<16) | (36<<8) | (28))
// Fixed with signed int
#define FX_S12_19 ((1<<16) | (12<<8) | (19))
#define FX_S13_18 ((1<<16) | (13<<8) | (18))
#define FX_S14_17 ((1<<16) | (14<<8) | (17))
#define FX_S15_16 ((1<<16) | (15<<8) | (16))
#define FX_S16_15 ((1<<16) | (16<<8) | (15))
#define FX_S17_14 ((1<<16) | (17<<8) | (14))
#define FX_S18_13 ((1<<16) | (18<<8) | (13))
#define FX_S19_12 ((1<<16) | (19<<8) | (12))
// Fixed with unsigned int
#define FX_S13_19 ((0<<16) | (13<<8) | (19))
#define FX_S14_18 ((0<<16) | (14<<8) | (18))
#define FX_S15_17 ((0<<16) | (15<<8) | (17))
#define FX_S16_16 ((0<<16) | (16<<8) | (16))
#define FX_S17_15 ((0<<16) | (17<<8) | (15))
#define FX_S18_14 ((0<<16) | (18<<8) | (14))
#define FX_S19_13 ((0<<16) | (19<<8) | (13))
#define FX_S20_12 ((0<<16) | (20<<8) | (12))
// Fixed with signed short
#define FX_S04_11 ((1<<16) | (4<<8) | (11))
#define FX_S05_10 ((1<<16) | (5<<8) | (10))
#define FX_S06_09 ((1<<16) | (6<<8) | (9))
#define FX_S07_08 ((1<<16) | (7<<8) | (8))
#define FX_S08_07 ((1<<16) | (8<<8) | (7))
#define FX_S09_06 ((1<<16) | (9<<8) | (6))
#define FX_S10_05 ((1<<16) | (10<<8) | (5))
#define FX_S11_04 ((1<<16) | (11<<8) | (4))
// Fixed with unsigned short
#define FX_S05_11 ((0<<16) | (5<<8) | (11))
#define FX_S06_10 ((0<<16) | (6<<8) | (10))
#define FX_S07_09 ((0<<16) | (7<<8) | (9))
#define FX_S08_08 ((0<<16) | (8<<8) | (8))
#define FX_S09_07 ((0<<16) | (9<<8) | (7))
#define FX_S10_06 ((0<<16) | (10<<8) | (6))
#define FX_S11_05 ((0<<16) | (11<<8) | (5))
#define FX_S12_04 ((0<<16) | (12<<8) | (4))
// Fixed with signed char
#define FX_S00_07 ((1<<16) | (0<<8) | (7))
#define FX_S01_06 ((1<<16) | (1<<8) | (6))
#define FX_S02_05 ((1<<16) | (2<<8) | (5))
#define FX_S03_04 ((1<<16) | (3<<8) | (4))
#define FX_S04_03 ((1<<16) | (4<<8) | (3))
#define FX_S05_02 ((1<<16) | (5<<8) | (2))
#define FX_S06_01 ((1<<16) | (6<<8) | (1))
// #define FX_S07_00 ((1<<16) | (7<<8) | (0)) // It is same as unsigned char
// Fixed with unsigned char
#define FX_S00_08 ((0<<16) | (0<<8) | (8))
#define FX_S01_07 ((0<<16) | (1<<8) | (7))
#define FX_S02_06 ((0<<16) | (2<<8) | (6))
#define FX_S03_05 ((0<<16) | (3<<8) | (5))
#define FX_S04_04 ((0<<16) | (4<<8) | (4))
#define FX_S05_03 ((0<<16) | (5<<8) | (3))
#define FX_S06_02 ((0<<16) | (6<<8) | (2))
#define FX_S07_01 ((0<<16) | (7<<8) | (1))
// Operation Preference
#define FX_OP_FLOAT 1
#define FX_OP_PRECISION 2
#define FX_OP_FAIR 3
#define FX_OP_PERFORMANCE 4
#include "fx_head.h"
#include "fx_s17_14.h"
#define fxADD(a,b) ((a)+(b))
#define fxSUB(a,b) ((a)-(b))
#define FX_Q_NUM_H (FX_Q_NUM/2)
#define FX_Q_NUM_HA (FX_Q_NUM_H-FX_Q_NUM_H/2)
#define FX_Q_NUM_HB (FX_Q_NUM_H/2)
/*
extern fixed fxAdd_float(), fxAdd_precision(), fxAdd_fair(), fxAdd_performance();
extern fixed fxSub_float(), fxSub_precision(), fxSub_fair(), fxSub_performance();
extern fixed fxMul_float(), fxMul_precision(), fxMul_fair(), fxMul_performance();
extern fixed fxDiv_float(), fxDiv_precision(), fxDiv_fair(), fxDiv_performance();
*/
// float function
fixed fxAdd_float(fixed a, fixed b)
{
return fromDouble(toDouble(a) + toDouble(b));
}
fixed fxSub_float(fixed a, fixed b)
{
return fromDouble(toDouble(a) - toDouble(b));
}
fixed fxMul_float(fixed a, fixed b)
{
return fromDouble(toDouble(a) * toDouble(b));
}
fixed fxDiv_float(fixed a, fixed b)
{
return fromDouble(toDouble(a) / toDouble(b));
}
// precision function
fixed fxAdd_precision(fixed a, fixed b)
{
return fromFloat(toFloat(a) + toFloat(b));
}
fixed fxSub_precision(fixed a, fixed b)
{
return fromFloat(toFloat(a) - toFloat(b));
}
fixed fxMul_precision(fixed a, fixed b)
{
long long la = a, lb = b;
return (la * lb) >> FX_Q_NUM;
}
fixed fxDiv_precision(fixed a, fixed b)
{
long long la = a, lb = b;
return (la << FX_Q_NUM) / lb;
}
// fair function
fixed fxAdd_fair(fixed a, fixed b)
{
return fxADD(a,b);
}
fixed fxSub_fair(fixed a, fixed b)
{
return fxSUB(a,b);
}
fixed fxMul_fair(fixed a, fixed b)
{
long long la = a, lb = b;
return ((la >> FX_Q_NUM_HA) * (lb >> FX_Q_NUM_HB)) >> FX_Q_NUM_H;
}
fixed fxDiv_fair(fixed a, fixed b)
{
long long la = a, lb = b;
return ((la << FX_Q_NUM_H) / lb) << FX_Q_NUM_H;
}
// performance function
fixed fxAdd_performance(fixed a, fixed b)
{
return fxADD(a,b);
}
fixed fxSub_performance(fixed a, fixed b)
{
return fxSUB(a,b);
}
fixed fxMul_performance(fixed a, fixed b)
{
long long la = a, lb = b;
return (la>>FX_Q_NUM_H)*(lb>>FX_Q_NUM_H);
}
fixed fxDiv_performance(fixed a, fixed b)
{
long long la = a, lb = b;
return ((la << FX_Q_NUM_H) / (lb >> FX_Q_NUM_H));
}
#include "fx_head.h"
#include <math.h>
#define FX_POINT FX_S17_14
#define FX_Q_NUM (FX_POINT & 0xFF) // 14
// If you want calculate with high precision set 64
#define FX_SYSTEM_INTEGER 64 // 32 or 64
#define FX_SYSTEM_FLOAT 64 // 32 or 64
#define FX_DATA_TYPE signed int
typedef int fx_s17_14;
typedef fx_s17_14 fixed;
#define fromDouble(d) ((fixed)((d)*DOUBLE_Q_VALUE))
#define toDouble(d) ((double)(d)*DOUBLE_1_Q_VALUE)
#define fromFloat(d) ((fixed)((d)*FLOAT_Q_VALUE))
#define toFloat(d) ((float)(d)*FLOAT_1_Q_VALUE)
// CONSTANTS
#define FLOAT_Q_VALUE (float)(1<<FX_Q_NUM)
#define DOUBLE_Q_VALUE (double)(1<<FX_Q_NUM)
#define FLOAT_1_Q_VALUE (float)(1.0f/FLOAT_Q_VALUE)
#define DOUBLE_1_Q_VALUE (double)(1.0f/DOUBLE_Q_VALUE)
#define FX_PI fromDOUBLE(M_PI)
// One of FX_OP_FLOAT, FX_OP_PRECISION, FX_OP_FAIR, FX_OP_PERFORMANCE
#define FX_OP_PREFERENCE FX_OP_FLOAT
extern fixed fxAdd_float(), fxAdd_precision(), fxAdd_fair(), fxAdd_performance();
extern fixed fxSub_float(), fxSub_precision(), fxSub_fair(), fxSub_performance();
extern fixed fxMul_float(), fxMul_precision(), fxMul_fair(), fxMul_performance();
extern fixed fxDiv_float(), fxDiv_precision(), fxDiv_fair(), fxDiv_performance();
#include "fx_s17_14.h"
#include <stdio.h>
int main()
{
double i;
int mul1, mul2, mul3, mul4;
for ( i = 362; i < 363; i+=0.001)
{
mul1 = fxMul_float(fromDouble(i), fromDouble(i));
mul2 = fxMul_precision(fromDouble(i), fromDouble(i));
mul3 = fxMul_fair(fromDouble(i), fromDouble(i));
mul4 = fxMul_performance(fromDouble(i), fromDouble(i));
fprintf(stdout, "%f * %f = %.15f \n", fromDouble(i), fromDouble(i), toDouble(mul1));
fprintf(stdout, "%f * %f = %.15f \n", fromDouble(i), fromDouble(i), toDouble(mul2));
fprintf(stdout, "%f * %f = %.15f \n", fromDouble(i), fromDouble(i), toDouble(mul3));
fprintf(stdout, "%f * %f = %.15f \n", fromDouble(i), fromDouble(i), toDouble(mul4));
fprintf(stdout, "=============================== \n");
}
return 0;
}
#pragma once
// Fixed with signed long long
#define FX_S28_35 ((1<<16) | (28<<8) | (35))
#define FX_S29_34 ((1<<16) | (29<<8) | (34))
#define FX_S30_33 ((1<<16) | (30<<8) | (33))
#define FX_S31_32 ((1<<16) | (31<<8) | (32))
#define FX_S32_31 ((1<<16) | (32<<8) | (31))
#define FX_S33_30 ((1<<16) | (33<<8) | (30))
#define FX_S34_29 ((1<<16) | (34<<8) | (29))
#define FX_S35_28 ((1<<16) | (35<<8) | (28))
// Fixed with unsigned long long
#define FX_U29_35 ((0<<16) | (29<<8) | (35))
#define FX_U30_34 ((0<<16) | (30<<8) | (34))
#define FX_U31_33 ((0<<16) | (31<<8) | (33))
#define FX_U32_32 ((0<<16) | (32<<8) | (32))
#define FX_U33_31 ((0<<16) | (33<<8) | (31))
#define FX_U34_30 ((0<<16) | (34<<8) | (30))
#define FX_U35_29 ((0<<16) | (35<<8) | (29))
#define FX_U36_28 ((0<<16) | (36<<8) | (28))
// Fixed with signed int
#define FX_S12_19 ((1<<16) | (12<<8) | (19))
#define FX_S13_18 ((1<<16) | (13<<8) | (18))
#define FX_S14_17 ((1<<16) | (14<<8) | (17))
#define FX_S15_16 ((1<<16) | (15<<8) | (16))
#define FX_S16_15 ((1<<16) | (16<<8) | (15))
#define FX_S17_14 ((1<<16) | (17<<8) | (14))
#define FX_S18_13 ((1<<16) | (18<<8) | (13))
#define FX_S19_12 ((1<<16) | (19<<8) | (12))
// Fixed with unsigned int
#define FX_S13_19 ((0<<16) | (13<<8) | (19))
#define FX_S14_18 ((0<<16) | (14<<8) | (18))
#define FX_S15_17 ((0<<16) | (15<<8) | (17))
#define FX_S16_16 ((0<<16) | (16<<8) | (16))
#define FX_S17_15 ((0<<16) | (17<<8) | (15))
#define FX_S18_14 ((0<<16) | (18<<8) | (14))
#define FX_S19_13 ((0<<16) | (19<<8) | (13))
#define FX_S20_12 ((0<<16) | (20<<8) | (12))
// Fixed with signed short
#define FX_S04_11 ((1<<16) | (4<<8) | (11))
#define FX_S05_10 ((1<<16) | (5<<8) | (10))
#define FX_S06_09 ((1<<16) | (6<<8) | (9))
#define FX_S07_08 ((1<<16) | (7<<8) | (8))
#define FX_S08_07 ((1<<16) | (8<<8) | (7))
#define FX_S09_06 ((1<<16) | (9<<8) | (6))
#define FX_S10_05 ((1<<16) | (10<<8) | (5))
#define FX_S11_04 ((1<<16) | (11<<8) | (4))
// Fixed with unsigned short
#define FX_S05_11 ((0<<16) | (5<<8) | (11))
#define FX_S06_10 ((0<<16) | (6<<8) | (10))
#define FX_S07_09 ((0<<16) | (7<<8) | (9))
#define FX_S08_08 ((0<<16) | (8<<8) | (8))
#define FX_S09_07 ((0<<16) | (9<<8) | (7))
#define FX_S10_06 ((0<<16) | (10<<8) | (6))
#define FX_S11_05 ((0<<16) | (11<<8) | (5))
#define FX_S12_04 ((0<<16) | (12<<8) | (4))
// Fixed with signed char
#define FX_S00_07 ((1<<16) | (0<<8) | (7))
#define FX_S01_06 ((1<<16) | (1<<8) | (6))
#define FX_S02_05 ((1<<16) | (2<<8) | (5))
#define FX_S03_04 ((1<<16) | (3<<8) | (4))
#define FX_S04_03 ((1<<16) | (4<<8) | (3))
#define FX_S05_02 ((1<<16) | (5<<8) | (2))
#define FX_S06_01 ((1<<16) | (6<<8) | (1))
// #define FX_S07_00 ((1<<16) | (7<<8) | (0)) // It is same as unsigned char
// Fixed with unsigned char
#define FX_S00_08 ((0<<16) | (0<<8) | (8))
#define FX_S01_07 ((0<<16) | (1<<8) | (7))
#define FX_S02_06 ((0<<16) | (2<<8) | (6))
#define FX_S03_05 ((0<<16) | (3<<8) | (5))
#define FX_S04_04 ((0<<16) | (4<<8) | (4))
#define FX_S05_03 ((0<<16) | (5<<8) | (3))
#define FX_S06_02 ((0<<16) | (6<<8) | (2))
#define FX_S07_01 ((0<<16) | (7<<8) | (1))
// Operation Preference
#define FX_OP_FLOAT 1
#define FX_OP_PRECISION 2
#define FX_OP_FAIR 3
#define FX_OP_PERFORMANCE 4
#include "fx_head.h"
#include "fx_s17_14.h"
#define fxADD(a,b) ((a)+(b))
#define fxSUB(a,b) ((a)-(b))
#define FX_Q_NUM_H (FX_Q_NUM/2)
#define FX_Q_NUM_HA (FX_Q_NUM_H-FX_Q_NUM_H/2)
#define FX_Q_NUM_HB (FX_Q_NUM_H/2)
/*
extern fixed fxAdd_float(), fxAdd_precision(), fxAdd_fair(), fxAdd_performance();
extern fixed fxSub_float(), fxSub_precision(), fxSub_fair(), fxSub_performance();
extern fixed fxMul_float(), fxMul_precision(), fxMul_fair(), fxMul_performance();
extern fixed fxDiv_float(), fxDiv_precision(), fxDiv_fair(), fxDiv_performance();
*/
// float function
fixed fxAdd_float(fixed a, fixed b)
{
return fromDouble(toDouble(a) + toDouble(b));
}
fixed fxSub_float(fixed a, fixed b)
{
return fromDouble(toDouble(a) - toDouble(b));
}
fixed fxMul_float(fixed a, fixed b)
{
return fromDouble(toDouble(a) * toDouble(b));
}
fixed fxDiv_float(fixed a, fixed b)
{
return fromDouble(toDouble(a) / toDouble(b));
}
// precision function
fixed fxAdd_precision(fixed a, fixed b)
{
return fromFloat(toFloat(a) + toFloat(b));
}
fixed fxSub_precision(fixed a, fixed b)
{
return fromFloat(toFloat(a) - toFloat(b));
}
fixed fxMul_precision(fixed a, fixed b)
{
long long la = a, lb = b;
return (la * lb) >> FX_Q_NUM;
}
fixed fxDiv_precision(fixed a, fixed b)
{
long long la = a, lb = b;
return (la << FX_Q_NUM) / lb;
}
// fair function
fixed fxAdd_fair(fixed a, fixed b)
{
return fxADD(a,b);
}
fixed fxSub_fair(fixed a, fixed b)
{
return fxSUB(a,b);
}
fixed fxMul_fair(fixed a, fixed b)
{
long long la = a, lb = b;
return ((la >> FX_Q_NUM_HA) * (lb >> FX_Q_NUM_HB)) >> FX_Q_NUM_H;
}
fixed fxDiv_fair(fixed a, fixed b)
{
long long la = a, lb = b;
return ((la << FX_Q_NUM_H) / lb) << FX_Q_NUM_H;
}
// performance function
fixed fxAdd_performance(fixed a, fixed b)
{
return fxADD(a,b);
}
fixed fxSub_performance(fixed a, fixed b)
{
return fxSUB(a,b);
}
fixed fxMul_performance(fixed a, fixed b)
{
long long la = a, lb = b;
return (la>>FX_Q_NUM_H)*(lb>>FX_Q_NUM_H);
}
fixed fxDiv_performance(fixed a, fixed b)
{
long long la = a, lb = b;
return ((la << FX_Q_NUM_H) / (lb >> FX_Q_NUM_H));
}
#include "fx_head.h"
#include <math.h>
#define FX_POINT FX_S17_14
#define FX_Q_NUM (FX_POINT & 0xFF) // 14
// If you want calculate with high precision set 64
#define FX_SYSTEM_INTEGER 64 // 32 or 64
#define FX_SYSTEM_FLOAT 64 // 32 or 64
#define FX_DATA_TYPE signed int
typedef int fx_s17_14;
typedef fx_s17_14 fixed;
#define fromDouble(d) ((fixed)((d)*DOUBLE_Q_VALUE))
#define toDouble(d) ((double)(d)*DOUBLE_1_Q_VALUE)
#define fromFloat(d) ((fixed)((d)*FLOAT_Q_VALUE))
#define toFloat(d) ((float)(d)*FLOAT_1_Q_VALUE)
// CONSTANTS
#define FLOAT_Q_VALUE (float)(1<<FX_Q_NUM)
#define DOUBLE_Q_VALUE (double)(1<<FX_Q_NUM)
#define FLOAT_1_Q_VALUE (float)(1.0f/FLOAT_Q_VALUE)
#define DOUBLE_1_Q_VALUE (double)(1.0f/DOUBLE_Q_VALUE)
#define FX_PI fromDOUBLE(M_PI)
// One of FX_OP_FLOAT, FX_OP_PRECISION, FX_OP_FAIR, FX_OP_PERFORMANCE
#define FX_OP_PREFERENCE FX_OP_FLOAT
extern fixed fxAdd_float(), fxAdd_precision(), fxAdd_fair(), fxAdd_performance();
extern fixed fxSub_float(), fxSub_precision(), fxSub_fair(), fxSub_performance();
extern fixed fxMul_float(), fxMul_precision(), fxMul_fair(), fxMul_performance();
extern fixed fxDiv_float(), fxDiv_precision(), fxDiv_fair(), fxDiv_performance();
#include "fx_s17_14.h"
#include <stdio.h>
int main()
{
double a = 29.2653, b = 13.4598;
int div1, div2, div3, div4;
div1 = fxDiv_float(fromDouble(a), fromDouble(b));
div2 = fxDiv_precision(fromDouble(a), fromDouble(b));
div3 = fxDiv_fair(fromDouble(a), fromDouble(b));
div4 = fxDiv_performance(fromDouble(a), fromDouble(b));
fprintf(stdout, "================================================================================================= \n");
fprintf(stdout, "value : %.15f fxDiv_float : %.15f diff : %.15f \n", 29.2653 / 13.4598, toDouble(div1), (29.2653 / 13.4598) - toDouble(div1));
fprintf(stdout, "value : %.15f fxDiv_precision : %.15f diff : %.15f \n", 29.2653 / 13.4598, toDouble(div2), (29.2653 / 13.4598) - toDouble(div2));
fprintf(stdout, "value : %.15f fxDiv_fair : %.15f diff : %.15f \n", 29.2653 / 13.4598, toDouble(div3), (29.2653 / 13.4598) - toDouble(div3));
fprintf(stdout, "value : %.15f fxDiv_performance : %.15f diff : %.15f \n", 29.2653 / 13.4598, toDouble(div4), (29.2653 / 13.4598) - toDouble(div4));
fprintf(stdout, "================================================================================================= \n");
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment