//===----------------------------------------------------------------------===//
//
// Part of libcu++, the C++ Standard Library for your entire system,
// under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

// MIT License
//
// Modifications Copyright (C) 2026 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 _CUDA_STD_CSTDINT
#define _CUDA_STD_CSTDINT

#include <cuda/std/detail/__config>

#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
#  pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
#  pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
#  pragma system_header
#endif // no system header

#include <cuda/std/version>

// NOTE(HIP/AMD): HIPRTC needs custom integer type definitions (not system <cstdint>).
#if _CCCL_HOSTED()
#  include <cstdint>
// NOTE(HIP/AMD): mirror the NVRTC custom-defines branch for HIPRTC -- HIPRTC also
// lacks a usable system <cstdint> (see the detailed notes below), so it must take
// this fallback path rather than the hosted <cstdint> include above.
#elif _CCCL_COMPILER(NVRTC) || defined(_CCCL_COMPILER_HIPRTC)
#  include <cuda/std/climits>

// NOTE(HIP/AMD): Under HIPRTC with -nostdinc++, the host C++
// standard library is not on the include path, so <cstdint> is not found.
// However, when HIPRTC is used WITHOUT -nostdinc++ (e.g., lit tests), the system
// <cstdint> IS available and should be used to avoid redefinition errors.
//
// Strategy: Check if <cstdint> is available. If yes, use it; if no (nostdinc++),
// provide our own fixed-width integer type definitions.
//
// NOTE(HIP/AMD): Under HIPRTC the fixed-width integer types must match HIP's
// device types (<hip/amd_detail/host_defines.h> __hip_internal::), where the
// 64-bit types are 'long long' / 'unsigned long long' -- NOT glibc's 'long'.
// cudf (cudf/types.hpp) establishes '::int64_t = __hip_internal::int64_t'
// (= long long) at global scope before including this header, so we must use
// 'long long' for the 64-bit aliases here too; pulling in host <stdint.h>
// (which typedefs int64_t = long via <bits/stdint-intn.h>) would clash with
// that, so we deliberately do NOT include it under HIPRTC. The smaller widths
// come from the compiler builtins (__INT*_TYPE__), which agree with HIP for
// 8/16/32-bit. See libhipcxx issue #104 / #304 and commit 49afdcfeb5 for the
// related __hip_internal-based workaround in the atomic code.
#  if defined(_CCCL_COMPILER_HIPRTC)
// NOTE(HIP/AMD): Under HIPRTC define the fixed-width aliases locally (rationale
// above): the 64-bit ones as 'long long' to match HIP's __hip_internal:: device
// types, and deliberately no host <stdint.h> (which would typedef int64_t = long
// and clash with the 'long long' consumers predefining ::int64_t expect).
using int8_t   = signed char;
using int16_t  = signed short;
using int32_t  = signed int;
using int64_t  = signed long long;
using uint8_t  = unsigned char;
using uint16_t = unsigned short;
using uint32_t = unsigned int;
using uint64_t = unsigned long long;
#  elif defined(__INT8_TYPE__) && defined(__INT16_TYPE__) && defined(__INT32_TYPE__) \
   && defined(__INT64_TYPE__) && defined(__UINT8_TYPE__) && defined(__UINT16_TYPE__) \
   && defined(__UINT32_TYPE__) && defined(__UINT64_TYPE__)
// NOTE(HIP/AMD): For NVRTC, prefer the compiler-provided __INT*_TYPE__ /
// __UINT*_TYPE__ builtins for the fixed-width integer types (e.g. on
// Linux/glibc x86_64, __INT64_TYPE__ is "long int").
using int8_t   = __INT8_TYPE__;
using int16_t  = __INT16_TYPE__;
using int32_t  = __INT32_TYPE__;
using int64_t  = __INT64_TYPE__;
using uint8_t  = __UINT8_TYPE__;
using uint16_t = __UINT16_TYPE__;
using uint32_t = __UINT32_TYPE__;
using uint64_t = __UINT64_TYPE__;
#  else
using int8_t   = signed char;
using int16_t  = signed short;
using int32_t  = signed int;
using int64_t  = signed long long;
using uint8_t  = unsigned char;
using uint16_t = unsigned short;
using uint32_t = unsigned int;
using uint64_t = unsigned long long;
#  endif

// NOTE(HIP/AMD): Only define _fast and _least types if system <stdint.h>
// hasn't been included (checked via _STDINT_H guard). This allows system
// headers pulled in transitively (e.g., via libstdc++ wrappers) to define
// these types first with their preferred underlying types, avoiding
// "type alias redefinition" conflicts. The compiler's __INT_FAST*_TYPE__
// builtins don't always match what glibc actually uses (e.g., builtin says
// short for int_fast16_t, but glibc uses long), so we can't rely on them.
#  if !defined(_STDINT_H)
using int_fast8_t = int8_t;
#    if defined(_WIN32) && defined(__HIP_PLATFORM_AMD__)
// NOTE(HIP/AMD): Windows defines int_fast16_t as int, not int16_t (short)
using int_fast16_t = int32_t;
#    else
using int_fast16_t = int16_t;
#    endif
using int_fast32_t = int32_t;
using int_fast64_t = int64_t;
using uint_fast8_t = uint8_t;
#    if defined(_WIN32) && defined(__HIP_PLATFORM_AMD__)
// NOTE(HIP/AMD): Windows defines uint_fast16_t as unsigned int, not uint16_t (unsigned short)
using uint_fast16_t = uint32_t;
#    else
using uint_fast16_t = uint16_t;
#    endif
using uint_fast32_t = uint32_t;
using uint_fast64_t = uint64_t;

using int_least8_t   = int8_t;
using int_least16_t  = int16_t;
using int_least32_t  = int32_t;
using int_least64_t  = int64_t;
using uint_least8_t  = uint8_t;
using uint_least16_t = uint16_t;
using uint_least32_t = uint32_t;
using uint_least64_t = uint64_t;

using intmax_t  = int64_t;
using uintmax_t = uint64_t;
#  endif // !defined(_STDINT_H)

// NOTE(HIP/AMD): Use the compiler-provided pointer-sized integer type
// builtins (matches the host C library, e.g. "long int" on Linux x86_64
// glibc) to avoid type-alias redefinition conflicts with <stdint.h>'s
// intptr_t/uintptr_t when system headers are pulled in transitively.
#  if defined(__INTPTR_TYPE__) && defined(__UINTPTR_TYPE__)
using intptr_t  = __INTPTR_TYPE__;
using uintptr_t = __UINTPTR_TYPE__;
#  else
using intptr_t  = int64_t;
using uintptr_t = uint64_t;
#  endif

#  define INT8_MIN   SCHAR_MIN
#  define INT16_MIN  SHRT_MIN
#  define INT32_MIN  INT_MIN
#  define INT64_MIN  LLONG_MIN
#  define INT8_MAX   SCHAR_MAX
#  define INT16_MAX  SHRT_MAX
#  define INT32_MAX  INT_MAX
#  define INT64_MAX  LLONG_MAX
#  define UINT8_MAX  UCHAR_MAX
#  define UINT16_MAX USHRT_MAX
#  define UINT32_MAX UINT_MAX
#  define UINT64_MAX ULLONG_MAX

#  define INT_FAST8_MIN   INT8_MIN
#  define INT_FAST16_MIN  INT16_MIN
#  define INT_FAST32_MIN  INT32_MIN
#  define INT_FAST64_MIN  INT64_MIN
#  define INT_FAST8_MAX   INT8_MAX
#  define INT_FAST16_MAX  INT16_MAX
#  define INT_FAST32_MAX  INT32_MAX
#  define INT_FAST64_MAX  INT64_MAX
#  define UINT_FAST8_MAX  UINT8_MAX
#  define UINT_FAST16_MAX UINT16_MAX
#  define UINT_FAST32_MAX UINT32_MAX
#  define UINT_FAST64_MAX UINT64_MAX

#  define INT_LEAST8_MIN   INT8_MIN
#  define INT_LEAST16_MIN  INT16_MIN
#  define INT_LEAST32_MIN  INT32_MIN
#  define INT_LEAST64_MIN  INT64_MIN
#  define INT_LEAST8_MAX   INT8_MAX
#  define INT_LEAST16_MAX  INT16_MAX
#  define INT_LEAST32_MAX  INT32_MAX
#  define INT_LEAST64_MAX  INT64_MAX
#  define UINT_LEAST8_MAX  UINT8_MAX
#  define UINT_LEAST16_MAX UINT16_MAX
#  define UINT_LEAST32_MAX UINT32_MAX
#  define UINT_LEAST64_MAX UINT64_MAX

#  define INTPTR_MIN  INT64_MIN
#  define INTPTR_MAX  INT64_MAX
#  define UINTPTR_MAX UINT64_MAX

#  define INTMAX_MIN  INT64_MIN
#  define INTMAX_MAX  INT64_MAX
#  define UINTMAX_MAX UINT64_MAX

#  define PTRDIFF_MIN INT64_MIN
#  define PTRDIFF_MAX INT64_MAX

#  define SIZE_MAX UINT64_MAX

#  define INT8_C(X)   ((::int_least8_t) (X))
#  define INT16_C(X)  ((::int_least16_t) (X))
#  define INT32_C(X)  ((::int_least32_t) (X))
#  define INT64_C(X)  ((::int_least64_t) (X))
#  define UINT8_C(X)  ((::uint_least8_t) (X))
#  define UINT16_C(X) ((::uint_least16_t) (X))
#  define UINT32_C(X) ((::uint_least32_t) (X))
#  define UINT64_C(X) ((::uint_least64_t) (X))

#  define INTMAX_C(X)  ((::intmax_t) (X))
#  define UINTMAX_C(X) ((::uintmax_t) (X))
#endif // ^^^ _CCCL_COMPILER(NVRTC) || _CCCL_COMPILER_HIPRTC

#include <cuda/std/__cccl/prologue.h>

_CCCL_BEGIN_NAMESPACE_CUDA_STD

using ::int16_t;
using ::int32_t;
using ::int64_t;
using ::int8_t;
using ::uint16_t;
using ::uint32_t;
using ::uint64_t;
using ::uint8_t;

using ::int_fast16_t;
using ::int_fast32_t;
using ::int_fast64_t;
using ::int_fast8_t;
using ::uint_fast16_t;
using ::uint_fast32_t;
using ::uint_fast64_t;
using ::uint_fast8_t;

using ::int_least16_t;
using ::int_least32_t;
using ::int_least64_t;
using ::int_least8_t;
using ::uint_least16_t;
using ::uint_least32_t;
using ::uint_least64_t;
using ::uint_least8_t;

using ::intptr_t;
using ::uintptr_t;

using ::intmax_t;
using ::uintmax_t;

#if _CCCL_HAS_INT128()
using __cccl_intmax_t  = __int128_t;
using __cccl_uintmax_t = __uint128_t;
#else // ^^^ _CCCL_HAS_INT128() ^^^ / vvv !_CCCL_HAS_INT128() vvv
using __cccl_intmax_t  = long long;
using __cccl_uintmax_t = unsigned long long;
#endif // !_CCCL_HAS_INT128()

_CCCL_END_NAMESPACE_CUDA_STD

#include <cuda/std/__cccl/epilogue.h>

#endif // _CUDA_STD_CSTDINT
