frac.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2018 Eistec AB
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
40 #include <stdint.h>
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
49 typedef struct {
50  uint32_t frac;
51  uint8_t shift;
52 } frac_t;
53 
62 uint32_t gcd32(uint32_t u, uint32_t v);
63 
77 void frac_init(frac_t *frac, uint32_t num, uint32_t den);
78 
87 static inline uint32_t frac_scale(const frac_t *frac, uint32_t x)
88 {
89  uint32_t scaled = ((uint64_t)frac->frac * x) >> frac->shift;
90  return scaled;
91 }
92 
93 #ifdef __cplusplus
94 }
95 #endif
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:87
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:49
uint32_t frac
fraction
Definition: frac.h:50
uint8_t shift
exponent
Definition: frac.h:51