Skip to content

Make endian.h more constexpr #1198

@eklitzke

Description

@eklitzke

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions