Path: blob/main/contrib/llvm-project/libc/src/__support/FPUtil/bfloat16.h
213799 views
//===-- Definition of bfloat16 data type. -----------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_BFLOAT16_H9#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_BFLOAT16_H1011#include "src/__support/CPP/bit.h"12#include "src/__support/CPP/type_traits.h"13#include "src/__support/FPUtil/cast.h"14#include "src/__support/FPUtil/dyadic_float.h"15#include "src/__support/macros/config.h"16#include "src/__support/macros/properties/types.h"1718#include <stdint.h>1920namespace LIBC_NAMESPACE_DECL {21namespace fputil {2223struct BFloat16 {24uint16_t bits;2526LIBC_INLINE BFloat16() = default;2728LIBC_INLINE constexpr explicit BFloat16(uint16_t bits) : bits(bits) {}2930template <typename T> LIBC_INLINE constexpr explicit BFloat16(T value) {31if constexpr (cpp::is_floating_point_v<T>) {32bits = fputil::cast<bfloat16>(value).bits;33} else if constexpr (cpp::is_integral_v<T>) {34Sign sign = Sign::POS;3536if constexpr (cpp::is_signed_v<T>) {37if (value < 0) {38sign = Sign::NEG;39value = -value;40}41}4243fputil::DyadicFloat<cpp::numeric_limits<cpp::make_unsigned_t<T>>::digits>44xd(sign, 0, value);45bits = xd.template as<bfloat16, /*ShouldSignalExceptions=*/true>().bits;4647} else {48bits = fputil::cast<bfloat16>(static_cast<float>(value)).bits;49}50}5152template <cpp::enable_if_t<fputil::get_fp_type<float>() ==53fputil::FPType::IEEE754_Binary32,54int> = 0>55LIBC_INLINE constexpr operator float() const {56uint32_t x_bits = static_cast<uint32_t>(bits) << 16U;57return cpp::bit_cast<float>(x_bits);58}59}; // struct BFloat166061} // namespace fputil62} // namespace LIBC_NAMESPACE_DECL6364#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_BFLOAT16_H656667