| /* |
| * Copyright 2018 Google, LLC |
| * |
| * This program is free software; you can redistribute it and/or modify |
| * it under the terms of the GNU General Public License as published by |
| * the Free Software Foundation; either version 2 of the License, or |
| * (at your option) any later version. |
| * |
| * This program is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| * GNU General Public License for more details. |
| */ |
| |
| #ifndef QMATH_H_ |
| #define QMATH_H_ |
| |
| #include <linux/types.h> |
| |
| typedef s32 qnum_t; |
| typedef s64 qnumd_t; |
| typedef u32 qnumu_t; |
| typedef s64 qnumud_t; |
| |
| #define QNUM_BITS (sizeof(qnum_t)*8) |
| /* integer part */ |
| #define QNUM_IBITS 8 |
| /* fractional part and mask */ |
| #define QNUM_FBITS (QNUM_BITS - QNUM_IBITS) |
| #define QNUM_FMASK (((qnum_t)1 << QNUM_FBITS) - 1) |
| |
| #define qnum_rconst(R) \ |
| ((qnum_t)((R) * (((qnumd_t)1 << QNUM_FBITS)\ |
| + ((R) >= 0 ? 0.5 : -0.5)))) |
| |
| #define qnum_fromint(I) ((qnumd_t)(I) << QNUM_FBITS) |
| /* battery raw capacity is in Q8_8 */ |
| #define qnum_from_q8_8(Q8_8) ((qnumd_t)(Q8_8) << (QNUM_FBITS-8)) |
| |
| /* truncate */ |
| #define qnum_toint(F) ((int)((F) >> QNUM_FBITS)) |
| /* round the number */ |
| #define qnum_roundint(F, P) \ |
| qnum_toint(F + qnum_rconst(P)) |
| |
| |
| static inline qnum_t qnum_mul(qnum_t A, qnum_t B) |
| { |
| return (((qnumd_t)A * (qnumd_t)B) >> QNUM_FBITS); |
| } |
| |
| static inline qnum_t qnum_div(qnum_t A, qnum_t B) |
| { |
| return (((qnumd_t)A << QNUM_FBITS) / (qnumd_t)B); |
| } |
| |
| |
| /* 1, 2, 3, and 4 digits */ |
| #define qnum_fracpart(A) ((qnum_t)(A) & QNUM_FMASK) |
| |
| #define QNUM_FRACBITS(A) \ |
| (((qnum_fracpart(A) << QNUM_IBITS)) & (((qnumud_t)1 << QNUM_BITS)-1)) |
| |
| #define QNUM_FRACn(A, n) \ |
| (((QNUM_FRACBITS(A) * n) >> QNUM_BITS) % n) |
| |
| #define QNUM_FRAC1(A) QNUM_FRACn(A, 10) |
| #define QNUM_FRAC2(A) QNUM_FRACn(A, 100) |
| #define QNUM_FRAC3(A) QNUM_FRACn(A, 1000) |
| #define QNUM_FRAC4(A) QNUM_FRACn(A, 10000) |
| |
| |
| #define _PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__ |
| |
| /* print as %d.%0<num>d */ |
| #define _STR_(x) # x |
| #define QNUM_FDGT_NUM 2 |
| #define QNUM_CSTR_FMT "%d.%02d" |
| #define QNUM_CSTR_SZ (4 + 1 + QNUM_FDGT_NUM + 1) |
| #define qnum_nfracdgt(A, n) _PRIMITIVE_CAT(QNUM_FRAC, n)(A) |
| #define qnum_fracdgt(A) ((int)qnum_nfracdgt(A, QNUM_FDGT_NUM)) |
| #endif /* QMATH_H_ */ |