Cherry pick changes from wip-scritchui which should be mainline.
[SquirrelJME.git] / modules / cldc-compact / src / main / java / cc / squirreljme / runtime / cldc / util / MathUtils.java
blob9c13e1708753aa3794a6b11bf1705dee689657a8
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // Multi-Phasic Applications: SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.runtime.cldc.util;
12 import cc.squirreljme.runtime.cldc.annotation.SquirrelJMEVendorApi;
14 /**
15 * Math utilities.
17 * @since 2024/03/17
19 @SquirrelJMEVendorApi
20 public final class MathUtils
22 /**
23 * Not used.
25 * @since 2024/03/17
27 private MathUtils()
32 /**
33 * Rounds to the nearest power of two.
35 * @param __val The value to round.
36 * @return The resultant rounded value.
37 * @since 2024/03/17
39 public static int nearestPowerOfTwo(int __val)
41 int hi = Math.max(1, Integer.highestOneBit(__val));
42 int mask = Math.max(1, hi - 1);
43 if ((__val & mask) >= (hi >>> 1))
44 return hi << 1;
45 return hi;