mpctool

include/fixedpoint.h

00001 
00005 #ifndef FIXEDPOINT_H
00006 #define FIXEDPOINT_H
00007 
00008 #include "mc04types.h"
00009 #include "mpc_const.h"
00010 
00011 enum {
00012         BITS_IN_BYTE = 8,
00013         ARCH_BITS = sizeof(float32_t) * BITS_IN_BYTE,
00014         FILTER = 0xffffffff >> (ARCH_BITS - FRAC_BITS),
00015 
00016 };
00017 
00018 //typedef long long unsigned int uint64_t;
00019 //typedef long long int int64_t;
00020 
00021 static inline int32_t fip_mul(int32_t a, int32_t b)
00022 {
00023         int64_t c;
00024         c = (uint64_t) a *(uint64_t) b;
00025 //      asm ("smull %0,%H0,%1,%2" : "=&r"(c) : "r"(a), "r"(b));
00026 
00027         return (int32_t) (c >> FRAC_BITS);
00028 }
00029 
00030 static inline int32_t fip_add(int32_t a, int32_t b)
00031 {
00032         return a + b;
00033 }
00034 
00035 static inline int32_t fip_sub(int32_t a, int32_t b)
00036 {
00037         return a - b;
00038 }
00039 
00040 static inline int32_t fip_div(int32_t n, int32_t d)
00041 {
00042         int64_t cl;
00043         int64_t nl = (int64_t) n;
00044         int64_t dl = (int64_t) d;
00045 
00046         cl = (nl << ARCH_BITS) / dl;
00047         return (int32_t) (cl >> (ARCH_BITS - FRAC_BITS));
00048 }
00049 
00050 static inline float32_t fip_fip2real(int32_t a)
00051 {
00052         const float32_t factor = (float32_t) (1 << FRAC_BITS);
00053         int32_t ci = a >> FRAC_BITS;
00054         float32_t cd = (float32_t) (a & FILTER) / factor;
00055         return ci + cd;
00056 }
00057 
00058 static inline int32_t fip_real2fip(float32_t a)
00059 {
00060         const float32_t factor = (float32_t) (1 << FRAC_BITS);
00061         int32_t ci = (int32_t) a;
00062         int32_t cd = (int32_t) ((a - (float32_t) ci) * factor);
00063         return (ci << FRAC_BITS) + cd;
00064 }
00065 
00066 #endif
 All Data Structures Files Functions Variables Typedefs