math

Overview

This module contains overflow safe functions for mathematical base operations.

Code Generation Macros

ABS_C_

#define ABS_C_(                                                                \
   FuncName, Type, EdgeValue                                                   \
)

Generates a function that determines overflow safe the absolute value of val and writes the result into a pointer parameter.

FuncName

the name of the generated function

Type

the type of the arguments

EdgeValue

the negative value that would cause an overflow

If a the operation would causes a overflow returns the function false, otherwise returns the function true. The generated function has the following common signature:

bool FuncName( Type val, Type res[static 1] );

ADD_C_

#define ADD_C_(                                                                \
   FuncName, Type, MinValue, MaxValue                                          \
)

Generates a function that performs a overflow safe addition of two values and writes the result into a pointer parameter.

FuncName

the name of the generated function

Type

the type of the arguments

MinValue

the smallest possible value of the argument

MaxValue

the largest possible value of the argument

If a the operation would causes a overflow returns the function false, otherwise returns the function true. The generated function has the following common signature:

bool FuncName( Type a, Type b, Type res[static 1] );

CONV_C_

#define CONV_C_(                                                               \
   FuncName, FromType, ToType, MinValue, MaxValue                              \
)

Generates a function that converts a large integer type(FromType) into a smaller integer type(ToType).

FuncName

the name of the generated function

FromType

the type of the larger source type

ToType

the type of the smaller destination type

MinValue

the smallest possible value of the destination type

MaxValue

the largest possible value of the destination type

The function returns true if a convertion is possible, otherwise reuturns the function false. The generated function has the following common signature:

bool FuncName( FromType src, ToType dst[static 1] );

MAX_C_

#define MAX_C_(                                                                \
   FuncName, Type                                                              \
)

Generates a function that returns the larger of its arguments.

FuncName

the name of the generated functions

Type

the type of the arguments

The generated function has the following common signature:

Type FuncName( Type a, Type b );

MIN_C_

#define MIN_C_(                                                                \
   FuncName, Type                                                              \
)

Generates a function that returns the smaller of its arguments.

FuncName

the name of the generated function

Type

the type of the arguments

The generated function has the following common signature:

Type FuncName( Type a, Type b );

MUL_C_

#define MUL_C_(                                                                \
   FuncName, Type, TmpType, MinValue, MaxValue                                 \
)

Generates a function that performs a overflow safe multiplication of two values and writes the result into as pointer parameter.

FuncName

the name of the generated function

Type

the type of the arguments

TmpType

larger type that can hold all possible values that the arguments can generate

MinValue

the smallest possible value of the arguments

MaxValue

the largest possible value of the arguments

If a the operation would causes a overflow returns the function false, otherwise returns the function true. The genereted function has the following common signature:

bool FuncName( Type a, Type b, Type res[static 1] );

SUB_C_

#define SUB_C_(                                                                \
   FuncName, Type, MinValue, MaxValue                                          \
)

Generates a function that performs a overflow safe subtraction of two values and writes the result into a pointer paramter.

FuncName

the name of the generated function

Type

the type of the arguments

MinValue

the smallest possible value of the arguments

MaxValue

the largest possible value of the arguments

If a the operation would causes a overflow returns the function false, otherwise reutrns the function true. The generated function has the following common signature:

bool FuncName( Type a, Type b, Type res[static 1] );

Functions

abs

abs_c_

#define abs_c_( Val, Res )

Generic macro to determine the absolute value for signed integers.

iabs8_c

bool iabs8_c( int8_t val, int8_t res[static 1] );

Via the macro ABS_C_ implemented function for int8_t values. If val is INT16_MIN returns the function false, all other int8_t values return true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   int8_t res = 0;

   expect_c_( iabs8_c( -67, &res ) );
   expect_c_( res == 67 );

   expect_c_( iabs8_c( 93, &res ) );
   expect_c_( res == 93 );

   expect_c_( not iabs8_c( INT8_MIN, &res ) );
   expect_c_( res == 93 );

   return finish_tap_c_();
}

iabs16_c

bool iabs16_c( int16_t val, int16_t res[static 1] );

Via the macro ABS_C_ implemented function for int16_t values. If val is INT16_MIN returns the function false, all other int16_t values return true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   int16_t res = 0;

   expect_c_( iabs16_c( -23789, &res ) );
   expect_c_( res == 23789 );

   expect_c_( iabs16_c( 17420, &res ) );
   expect_c_( res == 17420 );

   expect_c_( not iabs16_c( INT16_MIN, &res ) );
   expect_c_( res == 17420 );

   return finish_tap_c_();
}

iabs32_c

bool iabs32_c( int32_t val, int32_t res[static 1] );

Via the macro ABS_C_ implemented function for int32_t values. If val is INT32_MIN returns the function false, all other int32_t values return true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   int32_t res = 0;

   expect_c_( iabs32_c( -1234567890, &res ) );
   expect_c_( res == 1234567890 );

   expect_c_( iabs32_c( 2111999555, &res ) );
   expect_c_( res == 2111999555 );

   expect_c_( not iabs32_c( INT32_MIN, &res ) );
   expect_c_( res == 2111999555 );

   return finish_tap_c_();
}

iabs64_c

bool iabs64_c( int64_t val, int64_t res[static 1] );

Via the macro ABS_C_ implemented function for int64_t values. If val is INT64_MIN returns the function false, all other int64_t values return true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   int64_t res = 0;

   expect_c_( iabs64_c( -98765432101, &res ) );
   expect_c_( res == 98765432101 );

   expect_c_( iabs64_c( 123123123123, &res ) );
   expect_c_( res == 123123123123 );

   expect_c_( not iabs64_c( INT64_MIN, &res ) );
   expect_c_( res == 123123123123 );

   return finish_tap_c_();
}

add

add_c_

#define add_c_( A, B, Res )

Generic macro to perform a overflow safe addition for signed and unsigned integers.

iadd8_c

bool iadd8_c( int8_t a, int8_t b, int8_t res[static 1] );

Via the macro ADD_C_ implemented function for int8_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   int8_t res = 0;

   expect_c_( iadd8_c( 63, 64, &res ) );
   expect_c_( res == 127 );

   expect_c_( iadd8_c( 127, -26, &res ) );
   expect_c_( res == 101 );

   expect_c_( not iadd8_c( INT8_MAX, 1, &res ) );
   expect_c_( res == 101 );

   expect_c_( not iadd8_c( INT8_MIN, -1, &res ) );
   expect_c_( res == 101 );

   return finish_tap_c_();
}

iadd16_c

bool iadd16_c( int16_t a, int16_t b, int16_t res[static 1] );

Via the macro ADD_C_ implemented function for int16_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   int16_t res = 0;

   expect_c_( iadd16_c( 16789, 12345, &res ) );
   expect_c_( res == 29134 );

   expect_c_( iadd16_c( 25491, -5491, &res ) );
   expect_c_( res == 20000 );

   expect_c_( not iadd16_c( INT16_MAX, 1, &res ) );
   expect_c_( res == 20000 );

   expect_c_( not iadd16_c( INT16_MIN, -1, &res ) );
   expect_c_( res == 20000 );

   return finish_tap_c_();
}

iadd32_c

bool iadd32_c( int32_t a, int32_t b, int32_t res[static 1] );

Via the macro ADD_C_ implemented function for int32_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   int32_t res = 0;

   expect_c_( iadd32_c( 65535, 123456789, &res ) );
   expect_c_( res == 123522324 );

   expect_c_( iadd32_c( -16156565, 546648, &res ) );
   expect_c_( res == -15609917 );

   expect_c_( not iadd32_c( INT32_MAX, 1, &res ) );
   expect_c_( res == -15609917 );

   expect_c_( not iadd32_c( INT32_MIN, -1, &res ) );
   expect_c_( res == -15609917 );

   return finish_tap_c_();
}

iadd64_c

bool iadd64_c( int64_t a, int64_t b, int64_t res[static 1] );

Via the macro ADD_C_ implemented function for int64_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   int64_t res = 0;

   expect_c_( iadd64_c( 123123123123, 123123123123, &res ) );
   expect_c_( res == 246246246246 );

   expect_c_( iadd64_c( 123123123123, -321321321321, &res ) );
   expect_c_( res == -198198198198 );

   expect_c_( not iadd64_c( INT64_MAX, 1, &res ) );
   expect_c_( res == -198198198198 );

   expect_c_( not iadd64_c( INT64_MIN, -1, &res ) );
   expect_c_( res == -198198198198 );

   return finish_tap_c_();
}

uadd8_c

bool uadd8_c( uint8_t a, uint8_t b, uint8_t res[static 1] );

Via the macro ADD_C_ implemented function for uint8_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   uint8_t res = 0;

   expect_c_( uadd8_c( 213, 37, &res ) );
   expect_c_( res == 250 );

   expect_c_( not uadd8_c( UINT8_MAX, 1, &res ) );
   expect_c_( res == 250 );

   return finish_tap_c_();
}

uadd16_c

bool uadd16_c( uint16_t a, uint16_t b, uint16_t res[static 1] );

Via the macro ADD_C_ implemented function for uint16_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   uint16_t res = 0;

   expect_c_( uadd16_c( 32767, 12345, &res ) );
   expect_c_( res == 45112 );

   expect_c_( not uadd16_c( UINT16_MAX, 1, &res ) );
   expect_c_( res == 45112 );

   return finish_tap_c_();
}

uadd32_c

bool uadd32_c( uint32_t a, uint32_t b, uint32_t res[static 1] );

Via the macro ADD_C_ implemented function for uint32_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   uint32_t res = 0;

   expect_c_( uadd32_c( 2147483647, 1234567890, &res ) );
   expect_c_( res == 3382051537 );

   expect_c_( not uadd32_c( UINT32_MAX, 1, &res ) );
   expect_c_( res == 3382051537 );

   return finish_tap_c_();
}

uadd64_c

bool uadd64_c( uint64_t a, uint64_t b, uint64_t res[static 1] );

Via the macro ADD_C_ implemented function for uint64_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   uint64_t res = 0;

   expect_c_( uadd64_c( 9223372036854775807, 1234567890123456789, &res ) );
   expect_c_( res == 10457939926978232596ULL );

   expect_c_( not uadd64_c( UINT64_MAX, 1, &res ) );
   expect_c_( res == 10457939926978232596ULL );

   return finish_tap_c_();
}

conv

int64_to_size_c

bool int64_to_size_c( int64_t src, size_t dst[static 1] );

Returns true if the int64_t value can be represented in a size_t variable, otherwise false.

uint64_to_size_c

bool uint64_to_size_c( uint64_t src, size_t dst[static 1] );

Returns true if the uint64_t value can be representd in a size_t variable, otherwise false.

max

imax8_c

int8_t imax8_c( int8_t a, int8_t b );

Returns the larger of its int8_t arguments: either a or b.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   expect_c_( imax8_c( 93, -67 ) == 93 );

   return finish_tap_c_();
}

imax16_c

int16_t imax16_c( int16_t a, int16_t b );

Returns the larger of its int16_t arguments: either a or b.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   expect_c_( imax16_c( -23789, 17420 ) == 17420 );

   return finish_tap_c_();
}

imax32_c

int32_t imax32_c( int32_t a, int32_t b );

Returns the larger of its int32_t arguments: either a or b.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   expect_c_( imax32_c( 2111999555, -1234567890 ) == 2111999555 );

   return finish_tap_c_();
}

imax64_c

int64_t imax64_c( int64_t a, int64_t b );

Returns the larger of its int64_t arguments: either a or b.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   expect_c_( imax64_c( -98765432101, 123123123123 ) == 123123123123 );

   return finish_tap_c_();
}

max_c_

#define max_c_( A, B )

Generic macro function to determine the larger value for signed integers, unsigned integers and float numbers.

umax8_c

uint8_t umax8_c( uint8_t a, uint8_t b );

Returns the larger of its uint8_t arguments: either a or b.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   expect_c_( umax8_c( 37, 213 ) == 213 );

   return finish_tap_c_();
}

umax16_c

uint16_t umax16_c( uint16_t a, uint16_t b );

Returns the larger of its uint16_t arguments: either a or b.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   expect_c_( umax16_c( 32767, 12345 ) == 32767 );

   return finish_tap_c_();
}

umax32_c

uint32_t umax32_c( int32_t a, uint32_t b );

Returns the larger of its uint32_t arguments: either a or b.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   expect_c_( umax32_c( 1234567890, 2147483647 ) == 2147483647 );

   return finish_tap_c_();
}

umax64_c

uint64_t umax64_c( uint64_t a, uint64_t b );

Returns the larger of its uint64_t arguments: either a or b.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   expect_c_( umax64_c( 10457939926978232596ULL,
                         9223372036854775807 )
              == 10457939926978232596ULL );

   return finish_tap_c_();
}

min

imin8_c

int8_t imin8_c( int8_t a, int8_t b );

Returns the smaller of its int8_t arguments: either a or b.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   expect_c_( imin8_c( 93, -67 ) == -67 );

   return finish_tap_c_();
}

imin16_c

int16_t imin16_c( int16_t a, int16_t b );

Returns the smaller of its int16_t arguments: either a or b.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   expect_c_( imin16_c( -23789, 17420 ) == -23789 );

   return finish_tap_c_();
}

imin32_c

int32_t imin32_c( int32_t a, int32_t b );

Returns the smaller of its int32_t arguments: either a or b.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   expect_c_( imin32_c( 2111999555, -1234567890 ) == -1234567890 );

   return finish_tap_c_();
}

imin64_c

int64_t imin64_c( int64_t a, int64_t b );

Returns the smaller of its int64_t arguments: either a or b.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   expect_c_( imin64_c( -98765432101, 123123123123 ) == -98765432101 );

   return finish_tap_c_();
}

min_c_

#define min_c_( A, B )

Generic macro function to determine the smaller value for signed integers, unsigned integers and float numbers.

umin8_c

uint8_t umin8_c( uint8_t a, uint8_t b );

Returns the smaller of its uint8_t arguments: either a or b.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   expect_c_( umin8_c( 37, 213 ) == 37 );

   return finish_tap_c_();
}

umin16_c

uint16_t umin16_c( uint16_t a, uint16_t b );

Returns the smaller of its uint16_t arguments: either a or b.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   expect_c_( umin16_c( 32767, 12345 ) == 12345 );

   return finish_tap_c_();
}

umin32_c

uint32_t umin32_c( uint32_t a, uint32_t b );

Returns the smaller of its uint32_t arguments: either a or b.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   expect_c_( min_c_( 2.0, 3 ) == 2.0 );
   expect_c_( min_c_( 3.0, 2 ) == 2 );
   expect_c_( min_c_( NAN, 100 ) == 100 );

   return finish_tap_c_();
}

umin64_c

uint64_t umin64_c( uint64_t a, uint64_t b );

Returns the smaller of its uint64_t arguments: either a or b.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   expect_c_( umin64_c( 10457939926978232596ULL,
                         9223372036854775807 )
              == 9223372036854775807 );

   return finish_tap_c_();
}

mul

imul8_c

bool imul8_c( int8_t a, int8_t b, int8_t res[static 1] );

Via the macro MUL_C_ implemented function for int8_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   int8_t res = 0;

   expect_c_( imul8_c( 9, 14, &res ) );
   expect_c_( res == 126 );

   expect_c_( imul8_c( -31, 4, &res ) );
   expect_c_( res == -124 );

   // 128 > INT8_MAX( 127 )
   expect_c_( not imul8_c( 16, 8, &res ) );
   expect_c_( res == -124 );

   // 132 < INT8_MIN( -128 )
   expect_c_( not imul8_c( -12, 11, &res ) );
   expect_c_( res == -124 );

   return finish_tap_c_();
}

imul16_c

bool imul16_c( int16_t a, int16_t b, int16_t res[static 1] );

Via the macro MUL_C_ implemented function for int16_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   int16_t res = 0;

   expect_c_( imul16_c( 127, 127, &res ) );
   expect_c_( res == 16129 );

   expect_c_( imul16_c( -248, 127, &res ) );
   expect_c_( res == -31496 );

   // 32.768 > INT16_MAX( 32.767 )
   expect_c_( not imul16_c( 256, 128, &res ) );
   expect_c_( res == -31496 );

   // -32.896 < INT16_MIN( -32.768 )
   expect_c_( not imul16_c( 257, -128, &res ) );
   expect_c_( res == -31496 );

   return finish_tap_c_();
}

imul32_c

bool imul32_c( int32_t a, int32_t b, int32_t res[static 1] );

Via the macro MUL_C_ implemented function for int32_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   int32_t res = 0;

   expect_c_( imul32_c( 32767, 65535, &res ) );
   expect_c_( res == 2147385345 );

   expect_c_( imul32_c( 32768, -65536, &res ) );
   expect_c_( res == -2147483648 );

   // 2.147.483.648 > INT32_MAX( 2.147.483.647 )
   expect_c_( not imul32_c( 32768, 65536, &res ) );
   expect_c_( res == -2147483648 );

   // -2.147.549.184 < INT32_MIN( -2.147.483.648 )
   expect_c_( not imul32_c( -32769, 65536, &res ) );
   expect_c_( res == -2147483648 );

   return finish_tap_c_();
}

imul64_c

bool imul64_c( int64_t a, int64_t b, int64_t res[static 1] );

Function that allows to multiply two int64_t values when no overflow occurs. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   int64_t res = 0;

   expect_c_( imul64_c( 32768, 65536, &res ) );
   expect_c_( res == 2147483648 );

   expect_c_( imul64_c( -32769, 65536, &res ) );
   expect_c_( res == -2147549184 );

   // 9.223.372.036.854.775.808 > INT64_MAX( 9.223.372.036.854.775.807 )
   expect_c_( not imul64_c( 2147483648, 4294967296, &res ) );
   expect_c_( res == -2147549184 );

   // -9.223.372.039.002.259.456 < INT64_MIN( −9.223.372.036.854.775.808 )
   expect_c_( not imul64_c( -2147483648, 4294967297, &res ) );
   expect_c_( res == -2147549184 );

   return finish_tap_c_();
}

mul_c_

#define mul_c_( A, B, Res )

Generic macro to perform a overflow safe multiplication for signed and unsigned integers.

umul8_c

bool umul8_c( uint8_t a, uint8_t b, uint8_t res[static 1] );

Via the macro MUL_C_ implemented function for uint8_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   uint8_t res = 0;

   expect_c_( umul8_c( 12, 11, &res ) );
   expect_c_( res == 132 );

   // 256 > UINT8_MAX( 255 )
   expect_c_( not umul8_c( 16, 16, &res ) );
   expect_c_( res == 132 );

   return finish_tap_c_();
}

umul16_c

bool umul16_c( uint16_t a, uint16_t b, uint16_t res[static 1] );

Via the macro MUL_C_ implemented function for uint16_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   uint16_t res = 0;

   expect_c_( umul16_c( 257, 128, &res ) );
   expect_c_( res == 32896 );

   // 65.536 > UINT16_MAX( 65.535 )
   expect_c_( not umul16_c( 256, 256, &res ) );
   expect_c_( res == 32896 );

   return finish_tap_c_();
}

umul32_c

bool umul32_c( uint32_t a, uint32_t b, uint32_t res[static 1] );

Via the macro MUL_C_ implemented function for uint32_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   uint32_t res = 0;

   expect_c_( umul32_c( 32769, 65536, &res ) );
   expect_c_( res == 2147549184 );

   // 4.294.967.296 > UINT32_MAX( 4.294.967.295 )
   expect_c_( not umul32_c( 65536, 65536, &res ) );
   expect_c_( res == 2147549184 );

   return finish_tap_c_();
}

umul64_c

bool umul64_c( uint64_t a, uint64_t b, uint64_t res[static 1] );

Function that allows to multiply two uint64_t values when no overflow occurs. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   uint64_t res = 0;

   expect_c_( umul64_c( 2147483648, 4294967297, &res ) );
   expect_c_( res == 9223372039002259456ULL );

   // 18.446.744.073.709.551.616 > UINT64_MAX( 18.446.744.073.709.551.615 )
   expect_c_( not umul64_c( 4294967296, 4294967296, &res ) );
   expect_c_( res == 9223372039002259456ULL );

   return finish_tap_c_();
}

sub

isub8_c

bool isub8_c( int8_t a, int8_t b, int8_t res[static 1] );

Via the macro SUB_C_ implemented function for int8_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   int8_t res = 0;

   expect_c_( isub8_c( 63, 64, &res ) );
   expect_c_( res == -1 );

   expect_c_( isub8_c( 101, -26, &res ) );
   expect_c_( res == 127 );

   expect_c_( not isub8_c( INT8_MAX, -1, &res ) );
   expect_c_( res == 127 );

   expect_c_( not isub8_c( INT8_MIN, 1, &res ) );
   expect_c_( res == 127 );

   return finish_tap_c_();
}

isub16_c

bool isub16_c( int16_t a, int16_t b, int16_t res[static 1] );

Via the macro SUB_C_ implemented function for int16_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   int16_t res = 0;

   expect_c_( isub16_c( 16789, 12345, &res ) );
   expect_c_( res == 4444 );

   expect_c_( isub16_c( 25491, -5491, &res ) );
   expect_c_( res == 30982 );

   expect_c_( not isub16_c( INT16_MAX, -1, &res ) );
   expect_c_( res == 30982 );

   expect_c_( not isub16_c( INT16_MIN, 1, &res ) );
   expect_c_( res == 30982 );

   return finish_tap_c_();
}

isub32_c

bool isub32_c( int32_t a, int32_t b, int32_t res[static 1] );

Via the macro SUB_C_ implemented function for int32_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   int32_t res = 0;

   expect_c_( isub32_c( 65535, 123456789, &res ) );
   expect_c_( res == -123391254 );

   expect_c_( isub32_c( -16156565, 546648, &res ) );
   expect_c_( res == -16703213 );

   expect_c_( not isub32_c( INT32_MAX, -1, &res ) );
   expect_c_( res == -16703213 );

   expect_c_( not isub32_c( INT32_MIN, 1, &res ) );
   expect_c_( res == -16703213 );

   return finish_tap_c_();
}

isub64_c

bool isub64_c( int64_t a, int64_t b, int64_t res[static 1] );

Via the macro SUB_C_ implemented function for int64_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   int64_t res = 0;

   expect_c_( isub64_c( 246246246246, 123123123123, &res ) );
   expect_c_( res == 123123123123 );

   expect_c_( isub64_c( 123123123123, -198198198198, &res ) );
   expect_c_( res == 321321321321 );

   expect_c_( not isub64_c( INT64_MAX, -1, &res ) );
   expect_c_( res == 321321321321 );

   expect_c_( not isub64_c( INT64_MIN, 1, &res ) );
   expect_c_( res == 321321321321 );

   return finish_tap_c_();
}

sub_c_

#define sub_c_( A, B, Res )

Generic macro to perform a overflow safe subtraction for signed and unsigned integers.

usub8_c

bool usub8_c( uint8_t a, uint8_t b, uint8_t res[static 1] );

Via the macro SUB_C_ implemented function for uint8_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   uint8_t res = 0;

   expect_c_( usub8_c( 213, 37, &res ) );
   expect_c_( res == 176 );

   expect_c_( not usub8_c( 10, 11, &res ) );
   expect_c_( res == 176 );

   return finish_tap_c_();
}

usub16_c

bool usub16_c( uint16_t a, uint16_t b, uint16_t res[static 1] );

Via the macro SUB_C_ implemented function for uint16_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   uint16_t res = 0;

   expect_c_( usub16_c( 45112, 12345, &res ) );
   expect_c_( res == 32767 );

   expect_c_( not usub16_c( 127, 128, &res ) );
   expect_c_( res == 32767 );

   return finish_tap_c_();
}

usub32_c

bool usub32_c( uint32_t a, uint32_t b, uint32_t res[static 1] );

Via the macro SUB_C_ implemented function for uint32_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   uint32_t res = 0;

   expect_c_( usub32_c( 2147483647, 1234567890, &res ) );
   expect_c_( res == 912915757 );

   expect_c_( not usub32_c( 0, 1, &res ) );
   expect_c_( res == 912915757 );

   return finish_tap_c_();
}

usub64_c

bool usub64_c( uint64_t a, uint64_t b, uint64_t res[static 1] );

Via the macro SUB_C_ implemented function for uint64_t values. If a the operation would causes a overflow returns the function false, otherwise returns the function true.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/math.h"

int main( void )
{
   init_tap_c_();

   uint64_t res = 0;

   expect_c_( usub64_c( 9223372036854775807, 1234567890123456789, &res ) );
   expect_c_( res == 7988804146731319018ULL );

   expect_c_( not usub64_c( 9223372036854775807, UINT64_MAX, &res ) );
   expect_c_( res == 7988804146731319018ULL );

   return finish_tap_c_();
}