abseil / abseil-cpp

Abseil Common Libraries (C++)

Home Page:https://abseil.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Make endian.h more constexpr

eklitzke opened this issue · comments

This is a suggestion to make absl/base/internal/endian.h more constexpr. If the feature test macro __cpp_lib_byteswap is present then std::byteswap (which is already constexpr) can be used instead of the compiler builtins (e.g. __builtin_bswap64 and whatnot). If __cpp_lib_byteswap is not present then it's still possible to implement a constexpr byte swap using only C++11 features (and Clang/GCC should still emit code that use the bswap instruction for runtime calls), see https://stackoverflow.com/a/36937049/7897084 . By doing this all the gbswap* functions can be marked constexpr.

Likewise the other utility functions defined in endian.h that just call byte swap routines (e.g. the ToHost* and FromHost* functions) can also be marked constexpr.

Tangentially related, absl/base/config.h should consider using std::endian from C++20 if available (feature test macro __cpp_lib_endian) to check for system endianness instead of the compiler builtins.

Thank you for the suggestions.

absl/base/internal/endian.h is an internal implementation detail of Abseil, which means it is not meant to be used from outside of Abseil. Off the top of my head I can't think of any place where we would benefit from a constexpr endian operation. If we ever open up the endian API we will very likely look into implementing your suggestions.