File: division.h

package info (click to toggle)
crust-firmware 0.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,900 kB
  • sloc: ansic: 19,341; yacc: 596; lex: 479; makefile: 334; asm: 215; sh: 136; python: 42
file content (31 lines) | stat: -rw-r--r-- 661 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
 * Copyright © 2017-2022 The Crust Firmware Authors.
 * SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only
 */

#ifndef LIB_DIVISION_H
#define LIB_DIVISION_H

#include <stdint.h>

#define UDIV_ROUND(dividend, divisor) \
	(((dividend) + (divisor) / 2) / (divisor))

/**
 * Perform correctly-rounded unsigned division.
 */
static inline uint32_t
udiv_round(uint32_t dividend, uint32_t divisor)
{
	return (dividend + divisor / 2) / divisor;
}

/**
 * Perform unsigned division.
 *
 * This function replaces the dividend with the quotient and returns the
 * remainder.
 */
uint32_t udivmod(uint32_t *dividend, uint32_t divisor);

#endif /* LIB_DIVISION_H */