0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026 #ifndef BW_FIXED_H_
0027 #define BW_FIXED_H_
0028
0029 #define BW_FIXED_BITS_PER_FRACTIONAL_PART 24
0030
0031 #define BW_FIXED_GET_INTEGER_PART(x) ((x) >> BW_FIXED_BITS_PER_FRACTIONAL_PART)
0032 struct bw_fixed {
0033 int64_t value;
0034 };
0035
0036 #define BW_FIXED_MIN_I32 \
0037 (int64_t)(-(1LL << (63 - BW_FIXED_BITS_PER_FRACTIONAL_PART)))
0038
0039 #define BW_FIXED_MAX_I32 \
0040 (int64_t)((1ULL << (63 - BW_FIXED_BITS_PER_FRACTIONAL_PART)) - 1)
0041
0042 static inline struct bw_fixed bw_min2(const struct bw_fixed arg1,
0043 const struct bw_fixed arg2)
0044 {
0045 return (arg1.value <= arg2.value) ? arg1 : arg2;
0046 }
0047
0048 static inline struct bw_fixed bw_max2(const struct bw_fixed arg1,
0049 const struct bw_fixed arg2)
0050 {
0051 return (arg2.value <= arg1.value) ? arg1 : arg2;
0052 }
0053
0054 static inline struct bw_fixed bw_min3(struct bw_fixed v1,
0055 struct bw_fixed v2,
0056 struct bw_fixed v3)
0057 {
0058 return bw_min2(bw_min2(v1, v2), v3);
0059 }
0060
0061 static inline struct bw_fixed bw_max3(struct bw_fixed v1,
0062 struct bw_fixed v2,
0063 struct bw_fixed v3)
0064 {
0065 return bw_max2(bw_max2(v1, v2), v3);
0066 }
0067
0068 struct bw_fixed bw_int_to_fixed_nonconst(int64_t value);
0069 static inline struct bw_fixed bw_int_to_fixed(int64_t value)
0070 {
0071 if (__builtin_constant_p(value)) {
0072 struct bw_fixed res;
0073 BUILD_BUG_ON(value > BW_FIXED_MAX_I32 || value < BW_FIXED_MIN_I32);
0074 res.value = value << BW_FIXED_BITS_PER_FRACTIONAL_PART;
0075 return res;
0076 } else
0077 return bw_int_to_fixed_nonconst(value);
0078 }
0079
0080 static inline int32_t bw_fixed_to_int(struct bw_fixed value)
0081 {
0082 return BW_FIXED_GET_INTEGER_PART(value.value);
0083 }
0084
0085 struct bw_fixed bw_frc_to_fixed(int64_t num, int64_t denum);
0086
0087 static inline struct bw_fixed fixed31_32_to_bw_fixed(int64_t raw)
0088 {
0089 struct bw_fixed result = { 0 };
0090
0091 if (raw < 0) {
0092 raw = -raw;
0093 result.value = -(raw >> (32 - BW_FIXED_BITS_PER_FRACTIONAL_PART));
0094 } else {
0095 result.value = raw >> (32 - BW_FIXED_BITS_PER_FRACTIONAL_PART);
0096 }
0097
0098 return result;
0099 }
0100
0101 static inline struct bw_fixed bw_add(const struct bw_fixed arg1,
0102 const struct bw_fixed arg2)
0103 {
0104 struct bw_fixed res;
0105
0106 res.value = arg1.value + arg2.value;
0107
0108 return res;
0109 }
0110
0111 static inline struct bw_fixed bw_sub(const struct bw_fixed arg1, const struct bw_fixed arg2)
0112 {
0113 struct bw_fixed res;
0114
0115 res.value = arg1.value - arg2.value;
0116
0117 return res;
0118 }
0119
0120 struct bw_fixed bw_mul(const struct bw_fixed arg1, const struct bw_fixed arg2);
0121 static inline struct bw_fixed bw_div(const struct bw_fixed arg1, const struct bw_fixed arg2)
0122 {
0123 return bw_frc_to_fixed(arg1.value, arg2.value);
0124 }
0125
0126 static inline struct bw_fixed bw_mod(const struct bw_fixed arg1, const struct bw_fixed arg2)
0127 {
0128 struct bw_fixed res;
0129 div64_u64_rem(arg1.value, arg2.value, (uint64_t *)&res.value);
0130 return res;
0131 }
0132
0133 struct bw_fixed bw_floor2(const struct bw_fixed arg, const struct bw_fixed significance);
0134 struct bw_fixed bw_ceil2(const struct bw_fixed arg, const struct bw_fixed significance);
0135
0136 static inline bool bw_equ(const struct bw_fixed arg1, const struct bw_fixed arg2)
0137 {
0138 return arg1.value == arg2.value;
0139 }
0140
0141 static inline bool bw_neq(const struct bw_fixed arg1, const struct bw_fixed arg2)
0142 {
0143 return arg1.value != arg2.value;
0144 }
0145
0146 static inline bool bw_leq(const struct bw_fixed arg1, const struct bw_fixed arg2)
0147 {
0148 return arg1.value <= arg2.value;
0149 }
0150
0151 static inline bool bw_meq(const struct bw_fixed arg1, const struct bw_fixed arg2)
0152 {
0153 return arg1.value >= arg2.value;
0154 }
0155
0156 static inline bool bw_ltn(const struct bw_fixed arg1, const struct bw_fixed arg2)
0157 {
0158 return arg1.value < arg2.value;
0159 }
0160
0161 static inline bool bw_mtn(const struct bw_fixed arg1, const struct bw_fixed arg2)
0162 {
0163 return arg1.value > arg2.value;
0164 }
0165
0166 #endif