1 /** 2 Numem math helpers. 3 4 Copyright: 5 Copyright © 2023-2025, Kitsunebi Games 6 Copyright © 2023-2025, Inochi2D Project 7 8 License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) 9 Authors: Luna Nielsen 10 */ 11 module numem.core.math; 12 13 /** 14 Aligns the given value up to the given alignment. 15 16 Params: 17 value = The value to align, in bytes. 18 alignment = The byte alignment, in bytes. 19 20 Returns: 21 The value aligned to the given alignment. 22 */ 23 pragma(inline, true) 24 T nu_alignup(T)(T value, T alignment) @nogc if (__traits(isIntegral, T)) { 25 return alignment > 0 ? value + (alignment - (value % alignment)) : value; 26 } 27 28 /** 29 Aligns the given value down the given alignment. 30 31 Params: 32 value = The value to align, in bytes. 33 alignment = The byte alignment, in bytes. 34 35 Returns: 36 The value aligned to the given alignment. 37 */ 38 pragma(inline, true) 39 T nu_aligndown(T)(T value, T alignment) @nogc if (__traits(isIntegral, T)) { 40 return alignment > 0 ? value - (value % alignment) : value; 41 } 42 43 /** 44 Gets whether $(D value) is aligned to $(D alignment) 45 46 Params: 47 value = Value to check 48 alignment = The alignment to compare the pointer to. 49 50 Returns: 51 Whether $(D value) is aligned to $(D alignment) 52 */ 53 bool nu_is_aligned(T)(T value, size_t alignment) nothrow @nogc @trusted pure { 54 return (cast(size_t)value & (alignment-1)) == 0; 55 } 56 57 /** 58 Gets the lowest value between $(D a) and $(D b) 59 60 Params: 61 a = The first parameter 62 b = The second parameter 63 64 Returns: 65 The lowest value of the 2 given. 66 */ 67 auto ref T nu_min(T)(auto ref T a, auto ref T b) @nogc nothrow @trusted pure if (is(typeof(() => T.init < T.init))) { 68 return (a < b) ? a : b; 69 } 70 71 /** 72 Gets the largest value between $(D a) and $(D b) 73 74 Params: 75 a = The first parameter 76 b = The second parameter 77 78 Returns: 79 The largest value of the 2 given. 80 */ 81 auto ref T nu_max(T)(auto ref T a, auto ref T b) @nogc nothrow @trusted pure if (is(typeof(() => T.init > T.init))) { 82 return (a > b) ? a : b; 83 }