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.
min Minimum value.
max Maximum value.
Authors:
Andrei Alexandrescu (original std.* modules), Ilya Yaroshenko
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 (isIntegral!I);
Return type for extMul;
I low;
Lower I.sizeof * 8 bits
I high;
Higher I.sizeof * 8 bits
pure nothrow @nogc @safe ExtMulResult!U extMul(U)(in U a, in U b)
if (isUnsigned!U && (U.sizeof >= (ulong).sizeof));
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:
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);