Download the PHP package adept-digital/int-utils without Composer
On this page you can find all versions of the php package adept-digital/int-utils. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download adept-digital/int-utils
More information about adept-digital/int-utils
Files in adept-digital/int-utils
Package int-utils
Short Description Utilities for emulating WebAssembly 32-bit and 64-bit integer operations in PHP.
License LGPL-3.0-or-later
Informations about the package int-utils
Integer Utilities
Utilities for emulating WebAssembly 32-bit and 64-bit integer operations in PHP. These may be useful for porting an application or library from C, C++, or other languages.
Requirements
This package requires a 64-bit build of PHP 8.4 or later.
There is no dependency on external Composer packages or PHP extensions.
Installation
Install via Composer.
Usage
There are two classes with near-identical interfaces:
\AdeptDigital\IntUtils\Int32\AdeptDigital\IntUtils\Int64
The following operations are provided:
add(int $a, int $b): int: Integer addition.sub(int $a, int $b): int: Integer subtraction.mul(int $a, int $b): int: Integer multiplication.div_s(int $a, int $b): int: Signed integer division. [^1]div_u(int $a, int $b): int: Unsigned integer division.rem_s(int $a, int $b): int: Signed integer modulo. [^1]rem_u(int $a, int $b): int: Unsigned integer modulo.eq(int $a, int $b): int: Equal comparison. [^2]eqz(int $a): int: Equals zero test. [^2]ne(int $a, int $b): int: Not equal comparison. [^2]lt_s(int $a, int $b): int: Signed less than comparison. [^2]lt_u(int $a, int $b): int: Unsigned less than comparison.le_s(int $a, int $b): int: Signed less than or equal comparison. [^2]le_u(int $a, int $b): int: Unsigned less than or equal comparison.gt_s(int $a, int $b): int: Signed greater than comparison. [^2]gt_u(int $a, int $b): int: Unsigned greater than comparison.ge_s(int $a, int $b): int: Signed greater than or equal comparison. [^2]ge_u(int $a, int $b): int: Unsigned greater than or equal comparison.and(int $a, int $b): int: Bitwise AND. [^1]or(int $a, int $b): int: Bitwise OR. [^1]xor(int $a, int $b): int: Bitwise XOR. [^1]shl(int $a, int $b): int: Shift left. [^2]shr_s(int $a, int $b): int: Signed (arithmetic) shift right. [^2]shr_u(int $a, int $b): int: Unsigned (logical) shift right.rotl(int $a, int $b): int: Rotate left.rotr(int $a, int $b): int: Rotate right.clz(int $a): int: Count leading zeros.ctz(int $a): int: Count trailing zeros.popcnt(int $a): int: Population count.extend8_s(int $a): int: Extend the sign of the least significant 8 bits.extend16_s(int $a): int: Extend the sign of the least significant 16 bits.extend32_s(int $a): int: Extend the sign of the least significant 32 bits. [^3]
[^1]: Method is only provided for completeness. It does nothing different when compared to the equivalent PHP operator.
[^2]: Method only provides a trivial transformation on inputs or output.
[^3]: Only available on the \AdeptDigital\IntUtils\Int64 class.
32-bit representation
PHP has no 32-bit integer type, so 32-bit numbers are represented in 64-bit, but are limited to the range of a signed
32-bit integer (-2,147,483,648 to 2,147,483,647). For example, 4,294,967,295 (2**32-1) should be represented as
-1. Using numbers outside this range will cause a RangeException to be thrown, unless assertions are disabled, in
which case the result is undefined.
To correctly format an unsigned 32-bit integer, unset the most significant 32 bits:
64-bit representation
PHP has no unsigned integer type, so numbers larger than PHP_INT_MAX will appear to be negative.
To correctly format an unsigned integer, use printf() or sprintf():
Contributing
All contributions are welcome. If you would like to work on a new feature, consider creating an issue before starting.
TODO
- Add benchmarks and benchmark on different machines.
- Add alternative implementations (eg: GMP, FFI, custom extension).
- Add feature detection for alternative implementations.
License
Copyright 2025 David Gallagher [email protected].
The package is licensed under the GNU Lesser General Public License 3.0 or later.