Skip to content

Rename FastMath.toMultipleOf to alignToPowerOfTwo and provide generic implementation#2606

Open
Copilot wants to merge 2 commits intomasterfrom
copilot/rename-to-align-to-power-of-two
Open

Rename FastMath.toMultipleOf to alignToPowerOfTwo and provide generic implementation#2606
Copilot wants to merge 2 commits intomasterfrom
copilot/rename-to-align-to-power-of-two

Conversation

Copy link

Copilot AI commented Feb 11, 2026

FastMath.toMultipleOf used bitwise operations that only work when p is a power of two, causing incorrect results otherwise. The method name implied general-purpose functionality.

Changes

New alignToPowerOfTwo(int n, int p) method

  • Renamed implementation with validation that p is a power of two
  • Throws IllegalArgumentException for non-power-of-two values
  • Preserves optimized bitwise operation: ((n - 1) | (p - 1)) + 1

Reimplemented toMultipleOf(int n, int p) method

  • Now works for any positive integer p
  • Uses bitwise operation for power-of-two values (optimization)
  • Uses modulo-based algorithm for other values: n + (p - (n % p))

Updated BufferLayout.align()

  • Changed to call alignToPowerOfTwo (buffer alignments are always powers of two)

Example

// Old behavior (incorrect for non-power-of-two)
FastMath.toMultipleOf(10, 3);  // Returned 11 (wrong)

// New behavior
FastMath.toMultipleOf(10, 3);       // Returns 12 (correct)
FastMath.alignToPowerOfTwo(10, 4);  // Returns 12 (power-of-two only)
FastMath.alignToPowerOfTwo(10, 3);  // Throws IllegalArgumentException

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • dl.google.com
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED -XX:MaxMetaspaceSize=384m -XX:+HeapDumpOnOutOfMemoryError -Xms256m -Xmx512m -Dfile.encoding=UTF-8 -Duser.country -Duser.language=en -Duser.variant -cp /home/REDACTED/.gradle/wrapper/dists/gradle-8.8-bin/dl7vupf4psengwqhwktix4v1/gradle-8.8/lib/gradle-launcher-8.8.jar -javaagent:/home/REDACTED/.gradle/wrapper/dists/gradle-8.8-bin/dl7vupf4psengwqhwktix4v1/gradle-8.8/lib/agents/gradle-instrumentation-agent-8.8.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>FastMath.toMultipleOf should be renamed to FastMath.alignToPowerOfTwo</issue_title>
<issue_description>See: #1782 (comment)

The FastMath.toMultipleOf , used by the ubo implementation, only works for p that is a power of two, this can be confusing for devs looking to use FastMath apis in their code.

It should be renamed to alignToPowerOfTwo, and a generic implementation for toMultipleOf should be provided in place of the current one.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

… implementation

Co-authored-by: riccardobl <4943530+riccardobl@users.noreply.github.com>
Copilot AI changed the title [WIP] Rename FastMath.toMultipleOf to FastMath.alignToPowerOfTwo Rename FastMath.toMultipleOf to alignToPowerOfTwo and provide generic implementation Feb 11, 2026
Copilot AI requested a review from riccardobl February 11, 2026 21:51
@riccardobl riccardobl marked this pull request as ready for review February 11, 2026 21:53
@yaRnMcDonuts yaRnMcDonuts added this to the v3.10.0 milestone Feb 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FastMath.toMultipleOf should be renamed to FastMath.alignToPowerOfTwo

3 participants