codekingpro/portable-devtools
114k
1/*
2 * SPDX-FileCopyrightText: Copyright (c) 2020-2025 NVIDIA CORPORATION & AFFILIATES.
3 * All rights reserved. SPDX-License-Identifier: LicenseRef-NvidiaProprietary
4 *
5 * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
6 * property and proprietary rights in and to this material, related
7 * documentation and any modifications thereto. Any use, reproduction,
8 * disclosure or distribution of this material and related documentation
9 * without an express license agreement from NVIDIA CORPORATION or
10 * its affiliates is strictly prohibited.
11*/
12
13#pragma once
14
15#include <cstdint>
16#include <stdexcept>
17#include <string>
18
19#include "nvcomp.h"
20
21namespace nvcomp {
22
23/**
24 * @brief nvCOMP supported compression formats.
25*/
26enum class nvcompFormatType_t : uint8_t {
27 LZ4 = 0,
28 Snappy = 1,
29 ANS = 2,
30 GDeflate = 3,
31 Cascaded = 4,
32 Bitcomp = 5,
33 Zstd = 6,
34 Deflate = 7,
35 Gzip = 8,
36 NotSupportedError = 255
37};
38
39/**
40 * @brief The top-level exception thrown by nvcomp C++ methods.
41 */
42class NVCompException : public std::runtime_error
43{
44public:
45 /**
46 * @brief Create a new NVCompException.
47 *
48 * @param[in] err The error associated with the exception.
49 * @param[in] msg The error message.
50 */
51 NVCompException(nvcompStatus_t err, const std::string& msg) :
52 std::runtime_error(msg + " : code=" + std::to_string(err) + "."),
53 m_err(err)
54 {}
55
56 nvcompStatus_t get_error() const noexcept
57 {
58 return m_err;
59 }
60
61private:
62 nvcompStatus_t m_err;
63};
64
65#ifndef DOXYGEN_SHOULD_SKIP_THIS
66
67/**
68 * @brief Retrieve the applicable nvCOMP type for a standard type.
69 *
70 * @tparam T A standard C/C++ type.
71 *
72 * @return The applicable nvCOMP type.
73 */
74template <typename T>
75__device__ __host__ constexpr nvcompType_t TypeOfConst() noexcept
76{
77 return std::is_same<T, int8_t>::value ?
78 NVCOMP_TYPE_CHAR : (
79 std::is_same<T, uint8_t>::value ?
80 NVCOMP_TYPE_UCHAR : (
81 std::is_same<T, int16_t>::value ?
82 NVCOMP_TYPE_SHORT : (
83 std::is_same<T, uint16_t>::value ?
84 NVCOMP_TYPE_USHORT : (
85 std::is_same<T, int32_t>::value ?
86 NVCOMP_TYPE_INT : (
87 std::is_same<T, uint32_t>::value ?
88 NVCOMP_TYPE_UINT : (
89 std::is_same<T, int64_t>::value ?
90 NVCOMP_TYPE_LONGLONG : (
91 std::is_same<T, uint64_t>::value ?
92 NVCOMP_TYPE_ULONGLONG : (
93 NVCOMP_TYPE_BITS
94 ))))))));
95}
96
97/**
98 * @brief Retrieve the applicable nvCOMP type for a standard type with checks.
99 *
100 * @tparam T A standard C/C++ type.
101 *
102 * @return The applicable nvCOMP type.
103 */
104template <typename T>
105inline nvcompType_t TypeOf()
106{
107 auto type = TypeOfConst<T>();
108 if (type != NVCOMP_TYPE_BITS) {
109 return type;
110 }
111 throw NVCompException(
112 nvcompErrorNotSupported, "nvCOMP does not support the given type.");
113}
114
115#endif // DOXYGEN_SHOULD_SKIP_THIS
116
117} // namespace nvcomp
118 