KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
udiv.h File Reference

Fast unsigned integer division. More...

#include <kos/cdefs.h>
#include <kos/intmath.h>
#include <kos/regfield.h>

Go to the source code of this file.

Data Structures

struct  udiv_t
 Pre-processed unsigned integer divider value. More...
 

Functions

static udiv_t udiv_set_divider (unsigned int div)
 Create a udiv_t from an unsigned int divider.
 
static unsigned int udiv_divide_fast (unsigned int val, udiv_t udiv)
 Perform a division using the udiv_t, without bound checking.
 
static unsigned int udiv_divide (unsigned int val, udiv_t udiv)
 Perform a division using the udiv_t, with bound checking.
 

Detailed Description

Fast unsigned integer division.

This file contains an API that can be used to pre-process an unsigned value into an udiv_t struct. This struct can later be used to divide another unsigned value much faster than by doing an actual division.

This API is very useful when a rarely-changing divider is used often.

Author
Paul Cercueil

Function Documentation

◆ udiv_divide()

static unsigned int udiv_divide ( unsigned int val,
udiv_t udiv )
inlinestatic

Perform a division using the udiv_t, with bound checking.

This function will perform a division using the provided udiv_t. The pre-processed divider can correspond to any non-zero integer value.

Parameters
valThe dividend
udivThe divider, as a udiv_t
Returns
The result of the division operation

References udiv_t::m, udiv_t::p, and udiv_divide_fast().

◆ udiv_divide_fast()

static unsigned int udiv_divide_fast ( unsigned int val,
udiv_t udiv )
inlinestatic

Perform a division using the udiv_t, without bound checking.

This function is similar to udiv_divide(), except that it only works for values 1 < div < 0x80000001.

Parameters
valThe dividend
udivThe divider, as a udiv_t
Returns
The result of the division operation

References udiv_t::m, and udiv_t::p.

Referenced by udiv_divide().

◆ udiv_set_divider()

static udiv_t udiv_set_divider ( unsigned int div)
inlinestatic

Create a udiv_t from an unsigned int divider.

Use this function to create the preleminary udiv_t object, that can then be passed to udiv_divide() or udiv_divide_fast().

Parameters
divThe unsigned integer divider value
Returns
A pre-processed divider as a udiv_t

References BITLL, and is_power_of_two().