xapian-core  1.4.25
Functions
overflow.h File Reference

Arithmetic operations with overflow checks. More...

#include <type_traits>
+ Include dependency graph for overflow.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

template<typename T1 , typename T2 , typename R >
std::enable_if< std::is_unsigned< T1 >::value &&std::is_unsigned< T2 >::value &&std::is_unsigned< R >::value, bool >::type add_overflows (T1 a, T2 b, R &res)
 Addition with overflow checking. More...
 
template<typename T1 , typename T2 , typename R >
std::enable_if< std::is_unsigned< T1 >::value &&std::is_unsigned< T2 >::value &&std::is_unsigned< R >::value, bool >::type sub_overflows (T1 a, T2 b, R &res)
 Subtraction with overflow checking. More...
 
template<typename T1 , typename T2 , typename R >
std::enable_if< std::is_unsigned< T1 >::value &&std::is_unsigned< T2 >::value &&std::is_unsigned< R >::value, bool >::type mul_overflows (T1 a, T2 b, R &res)
 Multiplication with overflow checking. More...
 

Detailed Description

Arithmetic operations with overflow checks.

The operations are implemented with compiler builtins or equivalent where possible, so the overflow check will typically just require a check of the processor's overflow or carry flag.

Definition in file overflow.h.

Function Documentation

◆ add_overflows()

template<typename T1 , typename T2 , typename R >
std::enable_if<std::is_unsigned<T1>::value && std::is_unsigned<T2>::value && std::is_unsigned<R>::value, bool>::type add_overflows ( T1  a,
T2  b,
R &  res 
)

Addition with overflow checking.

Add a and b in infinite precision, and store the result in res.

Where possible, compiler built-ins or intrinsics are used to try to ensure minimal overhead from the overflow check.

Currently only supported when types involved are unsigned.

Returns
true if the result can be represented exactly in res, false otherwise.

Definition at line 58 of file overflow.h.

Referenced by parse_unsigned(), test_addoverflows1(), and throw_timeout().

◆ mul_overflows()

template<typename T1 , typename T2 , typename R >
std::enable_if<std::is_unsigned<T1>::value && std::is_unsigned<T2>::value && std::is_unsigned<R>::value, bool>::type mul_overflows ( T1  a,
T2  b,
R &  res 
)

Multiplication with overflow checking.

Multiply a and b in infinite precision, and store the result in res.

Where possible, compiler built-ins or intrinsics are used to try to ensure minimal overhead from the overflow check.

Currently only supported when types involved are unsigned.

Returns
true if the result can be represented exactly in res, false otherwise.

Definition at line 188 of file overflow.h.

Referenced by parse_unsigned(), Xapian::Document::Internal::remove_postings(), and test_muloverflows1().

◆ sub_overflows()

template<typename T1 , typename T2 , typename R >
std::enable_if<std::is_unsigned<T1>::value && std::is_unsigned<T2>::value && std::is_unsigned<R>::value, bool>::type sub_overflows ( T1  a,
T2  b,
R &  res 
)

Subtraction with overflow checking.

Subtract b from a in infinite precision, and store the result in res.

Where possible, compiler built-ins or intrinsics are used to try to ensure minimal overhead from the overflow check.

Currently only supported when types involved are unsigned.

Returns
true if the result can be represented exactly in res, false otherwise.

Definition at line 125 of file overflow.h.

Referenced by test_suboverflows1().