Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

mir.utility

Generic utilities.
Cheat Sheet
Function Name Description
swap Swaps two values.
extMul Extended unsigned multiplications.
min Minimum value.
max Maximum value.
License:
Authors:
Ilia Ki, Andrei Alexandrescu (original std.* modules),
public import ldc.intrinsics : _expect = llvm_expect;
auto min(T...)(T args)
if (T.length >= 2);
Iterates the passed arguments and returns the minimum value.
Parameters:
T args The values to select the minimum from. At least two arguments must be passed, and they must be comparable with <.
Returns:
The minimum of the passed-in values.
Examples:
min is not defined for arguments of mixed signedness because of security reasons. Please unify type or use a Phobos analog.
int a = -10;
uint b = 10;
static assert(!is(typeof(min(a, b))));
auto max(T...)(T args)
if (T.length >= 2);
Iterates the passed arguments and returns the minimum value.
Parameters:
T args The values to select the minimum from. At least two arguments must be passed, and they must be comparable with <.
Returns:
The minimum of the passed-in values.
Examples:
int a = 5;
short b = 6;
double c = 2;
auto d = max(a, b);
static assert(is(typeof(d) == int));
assert(d == 6);
auto e = min(a, b, c);
static assert(is(typeof(e) == double));
assert(e == 2);
Examples:
max is not defined for arguments of mixed signedness because of security reasons. Please unify type or use a Phobos analog.
int a = -10;
uint b = 10;
static assert(!is(typeof(max(a, b))));
struct ExtMulResult(I) if (isUnsigned!I);
Return type for extMul;
The payload order of low and high parts depends on the endianness.
I low;
Lower I.sizeof * 8 bits
I high;
Higher I.sizeof * 8 bits
pure nothrow @nogc @trusted ExtMulResult!U extMul(U)(in U a, in U b)
if (isUnsigned!U);
Extended unsigned multiplications. Performs U x U multiplication and returns ExtMulResult!U that contains extended result.
Parameters:
U a unsigned integer
U b unsigned integer
Returns:
128bit result if U is ulong or 256bit result if U is ucent.

Optimization Algorithm is optimized for LDC (LLVM IR, any target) and for DMD (X86_64).

Examples:
64bit x 64bit -> 128bit
immutable a = 0x93_8d_28_00_0f_50_a5_56;
immutable b = 0x54_c3_2f_e8_cc_a5_97_10;
enum c = extMul(a, b);     // Compile time algorithm
assert(extMul(a, b) == c); // Fast runtime algorithm
static assert(c.high == 0x30_da_d1_42_95_4a_50_78);
static assert(c.low == 0x27_9b_4b_b4_9e_fe_0f_60);
Examples:
32bit x 32bit -> 64bit
immutable a = 0x0f_50_a5_56;
immutable b = 0xcc_a5_97_10;
static assert(cast(ulong)extMul(a, b) == ulong(a) * b);
Examples:
immutable ushort a = 0xa5_56;
immutable ushort b = 0x97_10;
static assert(cast(uint)extMul(a, b) == a * b);
Examples:
immutable ubyte a = 0x56;
immutable ubyte b = 0x10;
static assert(cast(ushort)extMul(a, b) == a * b);
template simpleSort(alias cmp = "a < b")
Simple sort algorithm usefull for CTFE code.
T[] simpleSort(T)(return T[] array);