The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
div64.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * 64-bit by 32-bit division. the freestanding 32-bit build has no
5 * __udivdi3, so 64-bit "/" and "%" do not link; use this instead.
6 */
7
8#include "common/stdint.h"
9
10/**
11 * divide a 64-bit number by a 32-bit one (shift and subtract).
12 * @param dividend the number to divide.
13 * @param divisor the divisor (not 0).
14 * @param remainder_out receives the remainder (may be NULL).
15 * @return the quotient.
16 */
17static inline uint64_t udiv64_32(uint64_t dividend, uint32_t divisor, uint32_t *remainder_out)
18{
19 uint64_t quotient = 0;
20 uint64_t remainder = 0;
21
22 for (int bit = 63; bit >= 0; bit--)
23 {
24 remainder = (remainder << 1) | ((dividend >> bit) & 1ull);
25 if (remainder >= divisor)
26 {
27 remainder -= divisor;
28 quotient |= 1ull << bit;
29 }
30 }
31
32 if (remainder_out)
33 {
34 *remainder_out = (uint32_t)remainder;
35 }
36 return quotient;
37}
unsigned int uint32_t
Definition fb.h:17
__UINT64_TYPE__ uint64_t
Definition stdint.h:17