1 /*
2 * Copyright 2015 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License, version 2.0 (the
5 * "License"); you may not use this file except in compliance with the License. You may obtain a
6 * copy of the License at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software distributed under the License
11 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing permissions and limitations under
13 * the License.
14 */
15 package io.netty.util.internal;
16
17 /**
18 * Math utility methods.
19 */
20 public final class MathUtil {
21
22 private MathUtil() {
23 }
24
25 /**
26 * Fast method of finding the next power of 2 greater than or equal to the supplied value.
27 *
28 * <p>If the value is {@code <= 0} then 1 will be returned.
29 * This method is not suitable for {@link Integer#MIN_VALUE} or numbers greater than 2^30.
30 *
31 * @param value from which to search for next power of 2
32 * @return The next power of 2 or the value itself if it is a power of 2
33 */
34 public static int findNextPositivePowerOfTwo(final int value) {
35 assert value > Integer.MIN_VALUE && value < 0x40000000;
36 return 1 << (32 - Integer.numberOfLeadingZeros(value - 1));
37 }
38
39 /**
40 * Fast method of finding the next power of 2 greater than or equal to the supplied value.
41 * <p>This method will do runtime bounds checking and call {@link #findNextPositivePowerOfTwo(int)} if within a
42 * valid range.
43 * @param value from which to search for next power of 2
44 * @return The next power of 2 or the value itself if it is a power of 2.
45 * <p>Special cases for return values are as follows:
46 * <ul>
47 * <li>{@code <= 0} -> 1</li>
48 * <li>{@code >= 2^30} -> 2^30</li>
49 * </ul>
50 */
51 public static int safeFindNextPositivePowerOfTwo(final int value) {
52 return value <= 0 ? 1 : value >= 0x40000000 ? 0x40000000 : findNextPositivePowerOfTwo(value);
53 }
54
55 /**
56 * Determine if the requested {@code index} and {@code length} will fit within {@code capacity}.
57 * @param index The starting index.
58 * @param length The length which will be utilized (starting from {@code index}).
59 * @param capacity The capacity that {@code index + length} is allowed to be within.
60 * @return {@code false} if the requested {@code index} and {@code length} will fit within {@code capacity}.
61 * {@code true} if this would result in an index out of bounds exception.
62 */
63 public static boolean isOutOfBounds(int index, int length, int capacity) {
64 return (index | length | capacity | (index + length) | (capacity - (index + length))) < 0;
65 }
66
67 /**
68 * Compares two {@code int} values.
69 *
70 * @param x the first {@code int} to compare
71 * @param y the second {@code int} to compare
72 * @return the value {@code 0} if {@code x == y};
73 * {@code -1} if {@code x < y}; and
74 * {@code 1} if {@code x > y}
75 */
76 public static int compare(final int x, final int y) {
77 // do not subtract for comparison, it could overflow
78 return x < y ? -1 : (x > y ? 1 : 0);
79 }
80
81 /**
82 * Compare two {@code long} values.
83 * @param x the first {@code long} to compare.
84 * @param y the second {@code long} to compare.
85 * @return
86 * <ul>
87 * <li>0 if {@code x == y}</li>
88 * <li>{@code > 0} if {@code x > y}</li>
89 * <li>{@code < 0} if {@code x < y}</li>
90 * </ul>
91 */
92 public static int compare(long x, long y) {
93 return (x < y) ? -1 : (x > y) ? 1 : 0;
94 }
95 }