cupva_vector_ptr_cast.h#

Fully qualified name: src/device/vpu_runtime/include/cupva_device/cupva_vector_ptr_cast.h

File members: src/device/vpu_runtime/include/cupva_device/cupva_vector_ptr_cast.h

/*
 * Copyright (c) 2026 NVIDIA Corporation.  All rights reserved.
 *
 * NVIDIA Corporation and its licensors retain all intellectual property
 * and proprietary rights in and to this software, related documentation
 * and any modifications thereto.  Any use, reproduction, disclosure or
 * distribution of this software and related documentation without an express
 * license agreement from NVIDIA Corporation is strictly prohibited.
 */

#ifndef CUPVA_VECTOR_PTR_CAST_H
#define CUPVA_VECTOR_PTR_CAST_H

#include <cupva_device/impl/cpp_utils.h>
#include <stdint.h>

/*
 * Scalar-to-vector pointer casts -- INTENTIONAL CERT EXP39-C, pending RFD approval.
 *
 * VPU code routinely reinterprets a scalar buffer (uint32_t[], int16_t[], uint8_t[],
 * ...) as a pointer to the matching vector element type in order to issue vectorized
 * loads/stores. The vector element width equals the scalar storage width
 * (int<->vint/dvint/vuint/dvuint, short<->vshort/dvshort, char<->vchar/vuchar), so no
 * value is misinterpreted, but the static analyzer cannot see the two types as
 * compatible: it reports the downstream access as CERT EXP39-C and the cast itself as
 * a stricter-alignment cast (CERT EXP36-C).
 *
 * These helpers are the single audited home for that conversion. Each asserts the one
 * property the type system cannot express -- that the address meets the vector element's
 * alignment (4 bytes for 32-bit elements, 2 bytes for 16-bit, none for 8-bit). Coverity
 * attributes EXP39-C to each downstream access, so uses are still reported individually
 * by design; the deviation is filed against these helpers.
 *
 * The scalar source type is erased at the entry points, whose parameter is void *, so the
 * conversion these helpers actually perform is void * to vector pointer. pointerCast
 * expresses that with static_cast, which is well-formed; it therefore engages MISRA
 * C++-2023 Rule 8.2.6 (cast from void * to an object pointer) and not Rule 8.2.5
 * (reinterpret_cast). The argument that no value can be misinterpreted -- vector element
 * width equals scalar storage width -- can no longer be read off the cast expression and
 * holds by construction at the call site, where each width-specific entry point below is
 * handed a buffer of the matching scalar type.
 *
 * Each function is overloaded for const and non-const pointers so callers
 * preserve the source pointer's cv-qualification.
 */

namespace cupva { namespace device { namespace priv {
template<typename T>
inline constexpr void vpuAssert(T const &predicate);

template<typename Vector, std::uintptr_t AlignmentMask>
inline Vector *vectorPointerCast(void *p)
{
    vpuAssert(((asUintPtr(p) & AlignmentMask) == 0U));
    return pointerCast<Vector>(p);
}

template<typename Vector, std::uintptr_t AlignmentMask>
inline Vector const *vectorPointerCast(void const *p)
{
    vpuAssert(((asUintPtr(p) & AlignmentMask) == 0U));
    return pointerCast<Vector>(p);
}
}}} // namespace cupva::device::priv

// 32-bit element types: 4-byte alignment.
inline vint *cupvaVintPointerCast(void *p)
{
    return cupva::device::priv::vectorPointerCast<vint, 3U>(p);
}
inline vint const *cupvaVintPointerCast(void const *p)
{
    return cupva::device::priv::vectorPointerCast<vint, 3U>(p);
}
inline dvint *cupvaDvintPointerCast(void *p)
{
    return cupva::device::priv::vectorPointerCast<dvint, 3U>(p);
}
inline dvint const *cupvaDvintPointerCast(void const *p)
{
    return cupva::device::priv::vectorPointerCast<dvint, 3U>(p);
}
inline vuint *cupvaVuintPointerCast(void *p)
{
    return cupva::device::priv::vectorPointerCast<vuint, 3U>(p);
}
inline vuint const *cupvaVuintPointerCast(void const *p)
{
    return cupva::device::priv::vectorPointerCast<vuint, 3U>(p);
}
inline dvuint *cupvaDvuintPointerCast(void *p)
{
    return cupva::device::priv::vectorPointerCast<dvuint, 3U>(p);
}
inline dvuint const *cupvaDvuintPointerCast(void const *p)
{
    return cupva::device::priv::vectorPointerCast<dvuint, 3U>(p);
}

// 16-bit element types: 2-byte alignment.
inline vshort *cupvaVshortPointerCast(void *p)
{
    return cupva::device::priv::vectorPointerCast<vshort, 1U>(p);
}
inline vshort const *cupvaVshortPointerCast(void const *p)
{
    return cupva::device::priv::vectorPointerCast<vshort, 1U>(p);
}
inline dvshort *cupvaDvshortPointerCast(void *p)
{
    return cupva::device::priv::vectorPointerCast<dvshort, 1U>(p);
}
inline dvshort const *cupvaDvshortPointerCast(void const *p)
{
    return cupva::device::priv::vectorPointerCast<dvshort, 1U>(p);
}

// 8-bit element types: no alignment constraint.
inline vchar *cupvaVcharPointerCast(void *p)
{
    return cupva::device::priv::pointerCast<vchar>(p);
}
inline vchar const *cupvaVcharPointerCast(void const *p)
{
    return cupva::device::priv::pointerCast<vchar>(p);
}
inline vuchar *cupvaVucharPointerCast(void *p)
{
    return cupva::device::priv::pointerCast<vuchar>(p);
}
inline vuchar const *cupvaVucharPointerCast(void const *p)
{
    return cupva::device::priv::pointerCast<vuchar>(p);
}

#endif // CUPVA_VECTOR_PTR_CAST_H