### 4.1 Routines for integer arithmetic ¶

The integer arithmetic routines are used on platforms that don’t provide hardware support for arithmetic operations on some modes.

#### 4.1.1 Arithmetic functions ¶

Runtime Function: `int` __ashlsi3 `(int a, int b)`
Runtime Function: `long` __ashldi3 `(long a, int b)`
Runtime Function: `long long` __ashlti3 `(long long a, int b)`

These functions return the result of shifting a left by b bits.

Runtime Function: `int` __ashrsi3 `(int a, int b)`
Runtime Function: `long` __ashrdi3 `(long a, int b)`
Runtime Function: `long long` __ashrti3 `(long long a, int b)`

These functions return the result of arithmetically shifting a right by b bits.

Runtime Function: `int` __divsi3 `(int a, int b)`
Runtime Function: `long` __divdi3 `(long a, long b)`
Runtime Function: `long long` __divti3 `(long long a, long long b)`

These functions return the quotient of the signed division of a and b.

Runtime Function: `int` __lshrsi3 `(int a, int b)`
Runtime Function: `long` __lshrdi3 `(long a, int b)`
Runtime Function: `long long` __lshrti3 `(long long a, int b)`

These functions return the result of logically shifting a right by b bits.

Runtime Function: `int` __modsi3 `(int a, int b)`
Runtime Function: `long` __moddi3 `(long a, long b)`
Runtime Function: `long long` __modti3 `(long long a, long long b)`

These functions return the remainder of the signed division of a and b.

Runtime Function: `int` __mulsi3 `(int a, int b)`
Runtime Function: `long` __muldi3 `(long a, long b)`
Runtime Function: `long long` __multi3 `(long long a, long long b)`

These functions return the product of a and b.

Runtime Function: `long` __negdi2 `(long a)`
Runtime Function: `long long` __negti2 `(long long a)`

These functions return the negation of a.

Runtime Function: `unsigned int` __udivsi3 `(unsigned int a, unsigned int b)`
Runtime Function: `unsigned long` __udivdi3 `(unsigned long a, unsigned long b)`
Runtime Function: `unsigned long long` __udivti3 `(unsigned long long a, unsigned long long b)`

These functions return the quotient of the unsigned division of a and b.

Runtime Function: `unsigned long` __udivmoddi4 `(unsigned long a, unsigned long b, unsigned long *c)`
Runtime Function: `unsigned long long` __udivmodti4 `(unsigned long long a, unsigned long long b, unsigned long long *c)`

These functions calculate both the quotient and remainder of the unsigned division of a and b. The return value is the quotient, and the remainder is placed in variable pointed to by c.

Runtime Function: `unsigned int` __umodsi3 `(unsigned int a, unsigned int b)`
Runtime Function: `unsigned long` __umoddi3 `(unsigned long a, unsigned long b)`
Runtime Function: `unsigned long long` __umodti3 `(unsigned long long a, unsigned long long b)`

These functions return the remainder of the unsigned division of a and b.

#### 4.1.2 Comparison functions ¶

The following functions implement integral comparisons. These functions implement a low-level compare, upon which the higher level comparison operators (such as less than and greater than or equal to) can be constructed. The returned values lie in the range zero to two, to allow the high-level operators to be implemented by testing the returned result using either signed or unsigned comparison.

Runtime Function: `int` __cmpdi2 `(long a, long b)`
Runtime Function: `int` __cmpti2 `(long long a, long long b)`

These functions perform a signed comparison of a and b. If a is less than b, they return 0; if a is greater than b, they return 2; and if a and b are equal they return 1.

Runtime Function: `int` __ucmpdi2 `(unsigned long a, unsigned long b)`
Runtime Function: `int` __ucmpti2 `(unsigned long long a, unsigned long long b)`

These functions perform an unsigned comparison of a and b. If a is less than b, they return 0; if a is greater than b, they return 2; and if a and b are equal they return 1.

#### 4.1.3 Trapping arithmetic functions ¶

The following functions implement trapping arithmetic. These functions call the libc function `abort` upon signed arithmetic overflow.

Runtime Function: `int` __absvsi2 `(int a)`
Runtime Function: `long` __absvdi2 `(long a)`

These functions return the absolute value of a.

Runtime Function: `int` __addvsi3 `(int a, int b)`
Runtime Function: `long` __addvdi3 `(long a, long b)`

These functions return the sum of a and b; that is `a + b`.

Runtime Function: `int` __mulvsi3 `(int a, int b)`
Runtime Function: `long` __mulvdi3 `(long a, long b)`

The functions return the product of a and b; that is `a * b`.

Runtime Function: `int` __negvsi2 `(int a)`
Runtime Function: `long` __negvdi2 `(long a)`

These functions return the negation of a; that is `-a`.

Runtime Function: `int` __subvsi3 `(int a, int b)`
Runtime Function: `long` __subvdi3 `(long a, long b)`

These functions return the difference between b and a; that is `a - b`.

#### 4.1.4 Bit operations ¶

Runtime Function: `int` __clzsi2 `(unsigned int a)`
Runtime Function: `int` __clzdi2 `(unsigned long a)`
Runtime Function: `int` __clzti2 `(unsigned long long a)`

These functions return the number of leading 0-bits in a, starting at the most significant bit position. If a is zero, the result is undefined.

Runtime Function: `int` __ctzsi2 `(unsigned int a)`
Runtime Function: `int` __ctzdi2 `(unsigned long a)`
Runtime Function: `int` __ctzti2 `(unsigned long long a)`

These functions return the number of trailing 0-bits in a, starting at the least significant bit position. If a is zero, the result is undefined.

Runtime Function: `int` __ffsdi2 `(unsigned long a)`
Runtime Function: `int` __ffsti2 `(unsigned long long a)`

These functions return the index of the least significant 1-bit in a, or the value zero if a is zero. The least significant bit is index one.

Runtime Function: `int` __paritysi2 `(unsigned int a)`
Runtime Function: `int` __paritydi2 `(unsigned long a)`
Runtime Function: `int` __parityti2 `(unsigned long long a)`

These functions return the value zero if the number of bits set in a is even, and the value one otherwise.

Runtime Function: `int` __popcountsi2 `(unsigned int a)`
Runtime Function: `int` __popcountdi2 `(unsigned long a)`
Runtime Function: `int` __popcountti2 `(unsigned long long a)`

These functions return the number of bits set in a.

Runtime Function: `int32_t` __bswapsi2 `(int32_t a)`
Runtime Function: `int64_t` __bswapdi2 `(int64_t a)`

These functions return the a byteswapped.

#### 4.1.5 Bit-precise integer arithmetic functions ¶

`_BitInt(n)` library functions operate on arrays of limbs, where each limb has `__LIBGCC_BITINT_LIMB_WIDTH__` bits and the limbs are ordered according to `__LIBGCC_BITINT_ORDER__` ordering. The most significant limb if n is not divisible by `__LIBGCC_BITINT_LIMB_WIDTH__` contains padding bits which should be ignored on read (sign or zero extended), but extended on write. For the library functions, all bit-precise integers regardless of n are represented like that, even when the target ABI says that for some small n they should be represented differently in memory. A pointer to the array of limbs argument is always accompanied with a bit size argument. If that argument is positive, it is number of bits and the number is assumed to be zero-extended to infinite precision, if that argument is negative, it is negated number of bits above which all bits are assumed to be sign-extended to infinite precision. These number of bits arguments don’t need to match actual n for the operation used in the source, they could be lowered because of sign or zero extensions on the input or because value-range optimization figures value will need certain lower number of bits. For big-endian ordering of limbs, when lowering the bit size argument the pointer argument needs to be adjusted as well. Negative bit size argument should be always smaller or equal to `-2`, because `signed _BitInt(1)` is not valid. For output arguments, either the corresponding bit size argument should be always positive (for multiplication and division), or is negative when the output of conversion from floating-point value is signed and positive when unsigned. The arrays of limbs output arguments point to should not overlap any inputs, while input arrays of limbs can overlap. `UBILtype` below stands for unsigned integer type with `__LIBGCC_BITINT_LIMB_WIDTH__` bit precision.

Runtime Function: `void` __mulbitint3 `(UBILtype *ret, int32_t retprec, const UBILtype *u, int32_t uprec, const UBILtype *v, int32_t vprec)`

This function multiplies bit-precise integer operands u and v and stores result into retprec precision bit-precise integer result ret.

Runtime Function: `void` __divmodbitint4 `(UBILtype *q, int32_t qprec, UBILtype *r, int32_t rprec, const UBILtype *u, int32_t uprec, const UBILtype *v, int32_t vprec)`

This function divides bit-precise integer operands u and v and stores quotient into qprec precision bit-precise integer result q (unless q is `NULL` and qprec is 0, in that case quotient is not stored anywhere) and remainder into rprec precision bit-precise integer result r (similarly, unless r is `NULL` and rprec is 0).