openMSX
Classes | Functions
DivModByConstPrivate Namespace Reference

Utility class to optimize 64-bit divide/module by a 32-bit constant. More...

Classes

struct  Reduce0Result
 
struct  Reduce1Result
 
struct  Reduce2Result
 

Functions

constexpr Reduce0Result reduce0 (uint32_t divisor)
 
constexpr Reduce1Result reduce1 (uint64_t m, uint32_t s)
 
constexpr Reduce2Result reduce2 (uint128 m_low, uint128 m_high, uint32_t l)
 
template<uint32_t DIVISOR>
constexpr auto getAlgorithm ()
 

Detailed Description

Utility class to optimize 64-bit divide/module by a 32-bit constant.

For 32-bit by 32-bit gcc already does this optimization (on 64-bit CPUs gcc also does it for 64-bit operands). This optimization especially helps on CPU without a HW division instruction (like ARM).

Usage: DivModByConst<123> dm; uint32_t d = dm.div(x); // equivalent to d = x / 123; uint32_t m = dm.mod(x); // equivalent to d = x % 123;

Function Documentation

◆ getAlgorithm()

template<uint32_t DIVISOR>
constexpr auto DivModByConstPrivate::getAlgorithm ( )
constexpr

Definition at line 80 of file DivModByConst.hh.

References reduce0(), reduce1(), and reduce2().

◆ reduce0()

constexpr Reduce0Result DivModByConstPrivate::reduce0 ( uint32_t  divisor)
constexpr

Definition at line 25 of file DivModByConst.hh.

Referenced by getAlgorithm().

◆ reduce1()

constexpr Reduce1Result DivModByConstPrivate::reduce1 ( uint64_t  m,
uint32_t  s 
)
constexpr

Definition at line 39 of file DivModByConst.hh.

Referenced by getAlgorithm().

◆ reduce2()

constexpr Reduce2Result DivModByConstPrivate::reduce2 ( uint128  m_low,
uint128  m_high,
uint32_t  l 
)
constexpr

Definition at line 52 of file DivModByConst.hh.

Referenced by getAlgorithm().