Loading...
Searching...
No Matches
frac.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018 Eistec AB
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
41#ifndef FRAC_H
42#define FRAC_H
43
44#include <stdint.h>
45
46#ifdef __cplusplus
47extern "C" {
48#endif
49
53typedef struct {
54 uint32_t frac;
55 uint8_t shift;
56} frac_t;
57
66uint32_t gcd32(uint32_t u, uint32_t v);
67
81void frac_init(frac_t *frac, uint32_t num, uint32_t den);
82
91static inline uint32_t frac_scale(const frac_t *frac, uint32_t x)
92{
93 uint32_t scaled = ((uint64_t)frac->frac * x) >> frac->shift;
94 return scaled;
95}
96
97#ifdef __cplusplus
98}
99#endif
101#endif /* FRAC_H */
void frac_init(frac_t *frac, uint32_t num, uint32_t den)
Initialize frac_t struct.
static uint32_t frac_scale(const frac_t *frac, uint32_t x)
Scale a 32 bit integer by a 32/32 rational number.
Definition frac.h:91
uint32_t gcd32(uint32_t u, uint32_t v)
Compute greatest common divisor of u and v.
frac descriptor for fraction consisting of two 32 bit integers
Definition frac.h:53
uint32_t frac
fraction
Definition frac.h:54
uint8_t shift
exponent
Definition frac.h:55