cpp_utils.h#
Fully qualified name: src/device/vpu_runtime/include/cupva_device/impl/cpp_utils.h
File members: src/device/vpu_runtime/include/cupva_device/impl/cpp_utils.h
/*
* SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: LicenseRef-NvidiaProprietary
*
* NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
* property and proprietary rights in and to this material, related
* documentation and any modifications thereto. Any use, reproduction,
* disclosure or distribution of this material and related documentation
* without an express license agreement from NVIDIA CORPORATION or
* its affiliates is strictly prohibited.
*/
#ifndef CUPVA_DEVICE_IMPL_CPP_UTILS_H
#define CUPVA_DEVICE_IMPL_CPP_UTILS_H
#include <cstdint>
#if CUPVA_DEVICE != 1
# error "cupva_device/impl/cpp_utils.h should only be used in device code."
#endif
#if !defined(__cplusplus)
# error "cupva_device/impl/cpp_utils.h requires C++"
#endif
#if CUPVA_BUILD_MODE == CUPVA_NATIVE
# define chess_storage(x)
#endif
// signed variant of CUPVA_FIELD_SHIFT_UNSIGNED, useful for vector type shifted by scalar type
#define CUPVA_FIELD_SHIFT(x) (static_cast<int32_t>((false?x)%32))
namespace cupva { namespace device { namespace priv {
using ArraySize = decltype(sizeof(0U));
template<typename U, ArraySize N>
using Array = U[N];
// Minimal header-local static_cast detector. Device code includes no <type_traits>
// anywhere; the house style is hand-rolled aliases (see ArraySize).
//
// True when the conversion from U* to T* is expressible with static_cast -- typically
// when U is (cv-qualified) void, when T and U name the same type, or when the types are
// otherwise related as the language already permits. Detected rather than enumerated, so
// any pair static_cast accepts is picked up without a list to maintain.
template<typename T, typename U, typename = void>
struct IsStaticCastable
{
static constexpr bool value = false;
};
template<typename T, typename U>
struct IsStaticCastable<T, U, decltype(static_cast<void>(static_cast<T *>(static_cast<U *>(nullptr))))>
{
static constexpr bool value = true;
};
// The single audited home for the conversion. Prefers static_cast whenever the language
// permits it; reinterpret_cast is taken only for pairs no weaker cast can express.
// if constexpr discards the unused branch, so the ill-formed cast is never instantiated.
template<typename T, typename U>
inline T *castPointer(U *const value)
{
if constexpr (IsStaticCastable<T, U>::value)
{
return static_cast<T *>(value);
}
else
{
return reinterpret_cast<T *>(value);
}
}
template<typename T>
constexpr std::uintptr_t asUintPtr(T const *const value)
{
return reinterpret_cast<std::uintptr_t>(static_cast<void const volatile *>(value));
}
template<typename T>
constexpr std::intptr_t asIntPtr(T const *value)
{
return reinterpret_cast<std::intptr_t>(value);
}
// vpuAssert is defined in cupva_device/device_core.h. It is forward-declared here so the
// pointerCast templates can assert the destination alignment; the definition must be
// visible in every translation unit that instantiates pointerCast (device translation
// units include device_core.h).
template<typename T>
inline constexpr void vpuAssert(T const &predicate);
// Asserts that the address satisfies alignof(T), so a pointer to T formed from it is
// well-formed for the (vectorized) accesses that follow.
//
// The alignof(T) > 1U guard belongs at the call sites, not here: a 1-byte destination gives a
// zero mask and an always-true assert, and holding the guard inside this function would leave
// `value` unused in every such instantiation (MISRA C++-2023 Rule 0.2.2). Guarded at the call
// site, those instantiations are never formed.
//
// alignof(T) is bound to a uintptr_t constant before the mask is formed: MISRA C++-2023
// Rule 7.0.5 treats a sizeof/alignof-derived value as its own type category, and an explicit
// cast does not clear that while the cast sits in operand position -- even here, where size_t
// and uintptr_t are the same type. Hoisting into a typed constant is the house fix; see
// gsdf_impl_swseq_internal.h and getDescAttrAddr in device_core.h.
template<typename T, typename P>
inline void assertPointerAlignment(P *const value)
{
constexpr std::uintptr_t alignBytes = static_cast<std::uintptr_t>(alignof(T));
constexpr std::uintptr_t alignMask = alignBytes - 1U;
vpuAssert(((asUintPtr(value) & alignMask) == 0U));
}
// View a pointer or array as T*. Asserts the destination alignment, then defers to
// castPointer, which reinterprets only where no weaker cast can express the conversion. The
// Array<U, N> overloads bind the array by reference, so no array-to-pointer decay happens at
// the call site (MISRA C++-2023 Rule 7.11.2); the &value[0] is formed here in one audited
// place. The const overloads pass T const explicitly so U deduces to U const and the
// destination cv-qualification is preserved.
template<typename T, typename U>
inline T *pointerCast(U *const &value)
{
if constexpr (alignof(T) > 1U)
{
assertPointerAlignment<T>(value);
}
return castPointer<T>(value);
}
template<typename T, typename U>
inline T const *pointerCast(U const *const &value)
{
if constexpr (alignof(T) > 1U)
{
assertPointerAlignment<T>(value);
}
return castPointer<T const>(value);
}
template<typename T, typename U, ArraySize N>
inline T *pointerCast(Array<U, N> &value)
{
if constexpr (alignof(T) > 1U)
{
assertPointerAlignment<T>(&value[0]);
}
return castPointer<T>(&value[0]);
}
template<typename T, typename U, ArraySize N>
inline T const *pointerCast(Array<U, N> const &value)
{
if constexpr (alignof(T) > 1U)
{
assertPointerAlignment<T>(&value[0]);
}
return castPointer<T const>(&value[0]);
}
enum class VmemBank
{
A,
B,
C,
D
};
template<VmemBank Bank, typename T>
struct BankPtr;
template<typename T>
struct BankPtr<VmemBank::A, T>
{
using QualifiedPtr = T chess_storage(RAM_Ab) *;
};
template<typename T>
struct BankPtr<VmemBank::B, T>
{
using QualifiedPtr = T chess_storage(RAM_Bb) *;
};
template<typename T>
struct BankPtr<VmemBank::C, T>
{
using QualifiedPtr = T chess_storage(RAM_Cb) *;
};
// Bank D (RAM_Db) exists only on the 4-bank gen3 (Thor) VPU; the gen2 (Orin) ASIP
// model has no RAM_Db, so the specialization is gen3-only. Using VmemBank::D on
// gen2 is a compile error (incomplete BankPtr), which is the intended guard.
#if CUPVA_PVA_GEN_NUMBER > 2
template<typename T>
struct BankPtr<VmemBank::D, T>
{
using QualifiedPtr = T chess_storage(RAM_Db) *;
};
#endif
template<VmemBank Bank, typename T>
inline typename BankPtr<Bank, T>::QualifiedPtr bankPtrCast(T *ptr)
{
return (typename BankPtr<Bank, T>::QualifiedPtr)(ptr);
}
}}} // namespace cupva::device::priv
#endif // CUPVA_DEVICE_IMPL_CPP_UTILS_H