Skip to content

Conversation

SwuduSusuwu
Copy link
Contributor

@SwuduSusuwu SwuduSusuwu commented May 16, 2025

  • +ml_dtypes/include/float8.h:MostSignificantBit; constexpr version of clz opcode.
  • *ml_dtypes/include/float8.h:NextPowerOfTwo; replace long list of hardcoded values with 2 << MostSignificantBit<Size>, which extends NextPowerOfTwo (was limited to integer sizes, now is less code plus is general use).

Copy link

google-cla bot commented May 16, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Contributor Author

@SwuduSusuwu SwuduSusuwu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original NextPowerOfTwo function (before this pull request) just allows sizes from: 0 through sizeof(int64_t), which can break extended long long and intmax_t types (on architectures where those have more than 8 bytes, which standard C++ does allow).
Now allows sizes from: 0 through LLONG_MAX (so that NextPowerOfTwo has more possible future uses; it is common practice to allow the most possible future uses, as long as to do so does not introduce excessive lines of code (but this pull request reduces the total lines of code)).

Copy link
Contributor Author

@SwuduSusuwu SwuduSusuwu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, NextPowerOfTwo<Size>::value just equals the input value if the input value is a power of two. If the input value is not a power of 2, ::value equals the next power of 2.
Test shows that for all supported input values of the original function, that this new version of the function gives similar output values:

int main() {
  for(int input = 1; 9 > input; ++input) {
    std::cout << "NextPowerOfTwo<" << std::to_string(input) << "> == " << NextPowerOfTwo<1>::value << std::endl;
  }
  return 0;
}

before and after, which now shows similar values for the original versus new.

+`ml_dtypes/include/float8.h:MostSignificantBit`; `constexpr` version of `clz` opcode.
*`ml_dtypes/include/float8.h:NextPowerOfTwo`; replace long list of hardcoded values with `2 << MostSignificantBit<Size>`, which extends `NextPowerOfTwo` (was limited to integer sizes, now is less code plus is general use).
`MostSignificantBit`; `s/unsigned int/long long/`
`NextPowerOfTwo`; `s/int/long long/`
Ensures that those functions allowed the most possible amount of values.
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.

2 participants