|
27 | 27 | namespace eprosima { |
28 | 28 | namespace utils { |
29 | 29 |
|
| 30 | +/** |
| 31 | + * @brief Optimize % 2 operation |
| 32 | + * |
| 33 | + * @param number to calculate whether it is even |
| 34 | + * |
| 35 | + * @return whether \c number is even |
| 36 | + */ |
| 37 | +CPP_UTILS_DllAPI bool is_even( |
| 38 | + unsigned int number) noexcept; |
| 39 | + |
| 40 | +/** |
| 41 | + * @brief Calculate whether the argument is a power of 2 value. |
| 42 | + * |
| 43 | + * is_power_of_2(x) <=> E(n) : 2^n = x |
| 44 | + * |
| 45 | + * @param number to calculate whether it is a power of 2 |
| 46 | + * |
| 47 | + * @return whether \c number is power of 2 |
| 48 | + */ |
| 49 | +CPP_UTILS_DllAPI bool is_power_of_2( |
| 50 | + unsigned int number) noexcept; |
| 51 | + |
30 | 52 | /** |
31 | 53 | * @brief Module (%) operation with performance optimization |
32 | 54 | * |
33 | 55 | * This function optimizes the % operation, that executes a division, by optimizing these cases: |
34 | | - * - If the dividend is smaller or equal than the divisor, the result is the dividend |
| 56 | + * - If the dividend is smaller than the divisor, the result is the dividend |
| 57 | + * - If the dividend is equal than the divisor, the result is 0 |
35 | 58 | * - If the divisor is 2, the result is the dividend % 2 calculated by a logic AND operation |
36 | 59 | * - If the divisor is a power of 2, the result is calculated by a logic AND operation |
| 60 | + * - Otherwise uses % operation |
37 | 61 | * |
38 | 62 | * @param dividend Dividend |
39 | 63 | * @param divisor Divisor (must be greater than 0 so the operation make sense) |
40 | 64 | * |
41 | | - * @attention if divisor is 0, the result is \c dividend |
| 65 | + * @pre \c divisor must not be 0 |
| 66 | + * |
| 67 | + * @return The result of the operation % |
42 | 68 | * |
43 | | - * @return The result of the operation |
| 69 | + * @attention Do only use this function with non literal values. Literal values are optimized by compiler. |
44 | 70 | */ |
45 | | -CPP_UTILS_DllAPI uint32_t fast_module( |
46 | | - uint32_t dividend, |
47 | | - uint32_t divisor) noexcept; |
| 71 | +CPP_UTILS_DllAPI unsigned int fast_module( |
| 72 | + unsigned int dividend, |
| 73 | + unsigned int divisor) noexcept; |
48 | 74 |
|
49 | | -} /* namespace utils */ |
50 | | -} /* namespace eprosima */ |
| 75 | +/** |
| 76 | + * @brief Integer Division (/) operation with performance optimization |
| 77 | + * |
| 78 | + * This function optimizes the / operation by optimizing these cases: |
| 79 | + * - If \c dividend is smaller or equal than the \c, the result is \c dividend |
| 80 | + * - If the \c is 2, the result is \c dividend % 2 calculated by a logic AND operation |
| 81 | + * - If the \c is a power of 2, the result is calculated by a logic AND operation |
| 82 | + * - Otherwise uses / operation |
| 83 | + * |
| 84 | + * @param dividend Dividend |
| 85 | + * @param divisor Divisor |
| 86 | + * |
| 87 | + * @pre \c divisor must not be 0 |
| 88 | + * |
| 89 | + * @return The result of the operation / |
| 90 | + * |
| 91 | + * @attention Do only use this function with non literal values. Literal values are optimized by compiler. |
| 92 | + */ |
| 93 | +CPP_UTILS_DllAPI unsigned int fast_division( |
| 94 | + unsigned int dividend, |
| 95 | + unsigned int divisor) noexcept; |
51 | 96 |
|
| 97 | +/** |
| 98 | + * @brief Calculate the sum of an arithmetic progression from an initial to a final number. |
| 99 | + * |
| 100 | + * This function uses the fast operation to calculate an arithmetic sum: |
| 101 | + * S = ((a1 + an) / 2) * (n) |
| 102 | + * |
| 103 | + * @pre \c interval must be greater than 0 |
| 104 | + * @pre \c steps must be greater than 0 |
| 105 | + * |
| 106 | + * @param lowest lowest element of the arithmetic progression |
| 107 | + * @param interval interval between two elements of the arithmetic progression |
| 108 | + * @param steps number of steps of the arithmetic progression |
| 109 | + * |
| 110 | + * @return The result of the sum |
| 111 | + * |
| 112 | + * EXAMPLE OF USE |
| 113 | + * 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = arithmetic_progression_sum(1, 1, 10) |
| 114 | + * 0 + 2 + 4 + 6 + 8 = arithmetic_progression_sum(0, 2, 5) |
| 115 | + */ |
| 116 | +CPP_UTILS_DllAPI unsigned int arithmetic_progression_sum( |
| 117 | + unsigned int lowest, |
| 118 | + unsigned int interval, |
| 119 | + unsigned int steps) noexcept; |
52 | 120 |
|
| 121 | +} /* namespace utils */ |
| 122 | +} /* namespace eprosima */ |
0 commit comments