1/*
2 * Copyright 2016, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the GNU General Public License version 2. Note that NO WARRANTY is provided.
8 * See "LICENSE_GPLv2.txt" for details.
9 *
10 * @TAG(DATA61_GPL)
11 */
12#ifndef __MODE_UTIL_H
13#define __MODE_UTIL_H
14
15#include <config.h>
16#include <types.h>
17#include <arch/model/statedata.h>
18
19static inline CONST uint64_t
20div64(uint64_t numerator, uint32_t denominator)
21{
22    uint64_t quotient = 0llu;
23    uint64_t long_denom = (uint64_t) denominator;
24
25    if (unlikely(denominator > numerator)) {
26        return 0;
27    }
28
29    assert(numerator > 0);
30    assert(denominator > 0);
31
32    /* align denominator to numerator */
33    uint64_t c = ((uint64_t) 32u + clzl(denominator)) - clzll(numerator);
34    long_denom = long_denom << c;
35
36    /* perform binary long division */
37    while (c < UINT64_MAX) {
38        if (numerator >= long_denom) {
39            numerator -= long_denom;
40            quotient |= (1llu << c);
41        }
42        c--;
43        long_denom = long_denom >> 1llu;
44    }
45
46    return quotient;
47}
48
49#endif /* __MODE_UTIL_H */
50