/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2025 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef MIGRAPHX_GUARD_BYTE_HPP
#define MIGRAPHX_GUARD_BYTE_HPP

#include <migraphx/config.hpp>
#include <migraphx/requires.hpp>
#include <cstdint>
#include <type_traits>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

/**
 * Implementation of std::byte for MIGraphX.
 * Created to have a custom stream operator so that it prints as an unsigned int.
 * This type is essentially a limited unsigned_char to prevent things like trying to add two bytes.
 */
enum class byte : unsigned char
{
};

template <class IntType,
          MIGRAPHX_REQUIRES(std::is_integral<IntType>{} and std::is_unsigned<IntType>{})>
constexpr byte operator<<(byte b, IntType shift) noexcept
{
    return static_cast<byte>(static_cast<uint8_t>(b) << shift);
};

template <class IntType,
          MIGRAPHX_REQUIRES(std::is_integral<IntType>{} and std::is_unsigned<IntType>{})>
constexpr byte operator>>(byte b, IntType shift) noexcept
{
    return static_cast<byte>(static_cast<uint8_t>(b) >> shift);
};

template <class IntType,
          MIGRAPHX_REQUIRES(std::is_integral<IntType>{} and std::is_unsigned<IntType>{})>
constexpr byte& operator>>=(byte& b, IntType shift) noexcept
{
    b = b >> shift;
    return b;
};

template <class IntType,
          MIGRAPHX_REQUIRES(std::is_integral<IntType>{} and std::is_unsigned<IntType>{})>
constexpr byte& operator<<=(byte& b, IntType shift) noexcept
{
    b = b << shift;
    return b;
};

constexpr byte operator|(byte l, byte r) noexcept
{
    return static_cast<byte>(static_cast<uint8_t>(l) | static_cast<uint8_t>(r));
}

constexpr byte& operator|=(byte& l, byte r) noexcept
{
    l = l | r;
    return l;
}

constexpr byte operator&(byte l, byte r) noexcept
{
    return static_cast<byte>(static_cast<uint8_t>(l) & static_cast<uint8_t>(r));
}

constexpr byte& operator&=(byte& l, byte r) noexcept
{
    l = l & r;
    return l;
}

constexpr byte operator^(byte l, byte r) noexcept
{
    return static_cast<byte>(static_cast<uint8_t>(l) ^ static_cast<uint8_t>(r));
}

constexpr byte& operator^=(byte& l, byte r) noexcept
{
    l = l ^ r;
    return l;
}

constexpr byte operator~(byte b) noexcept { return static_cast<byte>(~static_cast<uint8_t>(b)); }

template <class IntType,
          MIGRAPHX_REQUIRES(std::is_integral<IntType>{} and std::is_unsigned<IntType>{})>
constexpr IntType to_integer(byte b) noexcept
{
    return static_cast<IntType>(b);
}

template <class Stream>
Stream& operator<<(Stream& os, const byte& b)
{
    os << static_cast<unsigned>(b);
    return os;
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx

#endif
