/* * Copyright (C) 2018-2020 Intel Corporation * * SPDX-License-Identifier: MIT * */ #pragma once #include "shared/source/helpers/constants.h" #include namespace NEO { template struct StorageType; template <> struct StorageType<8> { using Type = uint8_t; }; template <> struct StorageType<16> { using Type = uint16_t; }; template <> struct StorageType<32> { using Type = uint32_t; }; template <> struct StorageType<64> { using Type = uint64_t; }; template struct StorageType { using Type = typename StorageType::Type; }; template using StorageTypeT = typename StorageType::Type; template struct UnsignedFixedPointValue { UnsignedFixedPointValue(float v) { fromFloatingPoint(v); } StorageTypeT &getRawAccess() { return storage; } static constexpr float getMaxRepresentableFloat() { return getMaxRepresentableFloatingPointValue(); } float asFloat() { return asFloatPointType(); } protected: template static constexpr FloatingType getMaxRepresentableFloatingPointValue() { return static_cast( static_cast(maxNBitValue(IntegerBits)) + (static_cast(maxNBitValue(FractionalBits)) / (1U << FractionalBits))); } template void fromFloatingPoint(FloatingType val) { auto maxFloatVal = getMaxRepresentableFloatingPointValue(); // clamp to [0, maxFloatVal] val = (val < FloatingType{0}) ? FloatingType{0} : val; val = (val > maxFloatVal) ? maxFloatVal : val; // scale to fixed point representation this->storage = static_cast>(val * (1U << FractionalBits)); } template FloatingType asFloatPointType() { return static_cast(storage) / (1U << FractionalBits); } StorageTypeT storage = 0; }; using FixedU4D8 = UnsignedFixedPointValue<4, 8>; } // namespace NEO