cpp : Removed redundant keywords and replaced macros with constant expressions

This commit is contained in:
saundersp
2025-08-21 01:38:41 +02:00
parent c55dd14a89
commit 014326b1e3
8 changed files with 92 additions and 90 deletions

View File

@@ -4,11 +4,11 @@
#include <cmath>
#include <cassert>
#include <functional>
#include <stdint.h>
#include <cstdint>
#include "config.hpp"
#define BUFFER_SIZE 256
#define STRING_INT_SIZE 8 // Length of a number in log10 (including '-')
constexpr size_t BUFFER_SIZE = 256;
constexpr size_t STRING_INT_SIZE = 8; // Length of a number in log10 (including '-')
#ifndef __CUDACC__
#define __host__
@@ -20,7 +20,7 @@ typedef double float64_t;
typedef long double float128_t;
namespace np {
constexpr const float64_t inf = std::numeric_limits<float64_t>::infinity();
constexpr float64_t inf = std::numeric_limits<float64_t>::infinity();
typedef struct Slice {
size_t x = 0, y = 0, z = 0;
@@ -35,14 +35,14 @@ namespace np {
#endif
__host__ __device__
Shape(void) noexcept {
constexpr Shape(void) noexcept {
#if __DEBUG
printf("Shape created (default)\n");
#endif
}
__host__ __device__
Shape(const size_t& length, size_t* const data) noexcept : length(length), data(data), refcount(new size_t(1)) {
inline Shape(const size_t& length, size_t* const data) noexcept : length(length), data(data), refcount(new size_t(1)) {
#if __DEBUG
printf("Shape created (raw)\n");
for(size_t i = 0; i < length; ++i)
@@ -51,7 +51,7 @@ namespace np {
}
__host__ __device__
Shape(const std::initializer_list<size_t>& dims) noexcept : length(dims.size()), data(new size_t[dims.size()]), refcount(new size_t(1)) {
inline Shape(const std::initializer_list<size_t>& dims) noexcept : length(dims.size()), data(new size_t[dims.size()]), refcount(new size_t(1)) {
#if __DEBUG
printf("Shape created (initializer)\n");
#endif
@@ -65,7 +65,7 @@ namespace np {
}
__host__ __device__
Shape(const Shape& shape) noexcept {
constexpr Shape(const Shape& shape) noexcept {
#if __DEBUG
printf("Shape created (copy)\n");
#endif
@@ -97,7 +97,7 @@ namespace np {
}
__host__ __device__
Shape(Shape&& shape) noexcept {
constexpr Shape(Shape&& shape) noexcept {
#if __DEBUG
printf("Shape created (move)\n");
#endif
@@ -127,7 +127,7 @@ namespace np {
}
__host__ __device__
~Shape(void) noexcept {
inline ~Shape(void) noexcept {
if(refcount == nullptr){
#if __DEBUG
printf("Shape refcount freed more than once\n");
@@ -159,7 +159,7 @@ namespace np {
}
__host__ __device__
Shape& operator=(const Shape& shape) noexcept {
constexpr Shape& operator=(const Shape& shape) noexcept {
#if __DEBUG
printf("Shape created (assign copy)\n");
#endif
@@ -190,7 +190,7 @@ namespace np {
}
__host__ __device__
Shape& operator=(Shape&& shape) noexcept {
constexpr Shape& operator=(Shape&& shape) noexcept {
#if __DEBUG
printf("Shape created (assign move)\n");
#endif
@@ -221,7 +221,6 @@ namespace np {
return *this;
}
__host__ __device__
constexpr size_t& operator[](const size_t& i) const {
#if __DEBUG
@@ -263,42 +262,42 @@ namespace np {
size_t* refcount = nullptr;
__host__ __device__
Array(void) noexcept {
constexpr Array(void) noexcept {
#if __DEBUG
printf("Array created (default)\n");
#endif
}
__host__ __device__
Array(const Shape& shape, T* const data) noexcept : shape(shape), data(data), refcount(new size_t(1)) {
constexpr Array(const Shape& shape, T* const data) noexcept : shape(shape), data(data), refcount(new size_t(1)) {
#if __DEBUG
printf("Array created (raw, copy shape)\n");
#endif
}
__host__ __device__
Array(const Shape& shape) noexcept : shape(shape), data(new T[np::prod(shape)]), refcount(new size_t(1)) {
constexpr Array(const Shape& _shape) noexcept : shape(_shape), data(new T[np::prod(shape)]), refcount(new size_t(1)) {
#if __DEBUG
printf("Array created (raw empty, copy shape)\n");
#endif
}
__host__ __device__
Array(Shape&& shape, T* const data) noexcept : shape(shape), data(data), refcount(new size_t(1)) {
constexpr Array(Shape&& shape, T* const data) noexcept : shape(shape), data(data), refcount(new size_t(1)) {
#if __DEBUG
printf("Array created (raw, move shape)\n");
#endif
}
__host__ __device__
Array(Shape&& shape) noexcept : shape(shape), data(new T[np::prod(shape)]), refcount(new size_t(1)) {
constexpr Array(Shape&& shape) noexcept : shape(shape), data(new T[np::prod(shape)]), refcount(new size_t(1)) {
#if __DEBUG
printf("Array created (raw empty, move shape)\n");
#endif
}
__host__ __device__
Array(const Array& array) noexcept : shape(array.shape) {
constexpr Array(const Array& array) noexcept : shape(array.shape) {
#if __DEBUG
printf("Array created (copy)\n");
#endif
@@ -326,7 +325,7 @@ namespace np {
}
__host__ __device__
Array(Array&& array) noexcept : shape(std::move(array.shape)) {
constexpr Array(Array&& array) noexcept : shape(std::move(array.shape)) {
#if __DEBUG
printf("Array created (move)\n");
#endif
@@ -350,7 +349,7 @@ namespace np {
}
__host__ __device__
~Array(void) noexcept {
inline ~Array(void) noexcept {
if(refcount == nullptr){
#if __DEBUG
printf("Array refcount freed more than once\n");
@@ -379,7 +378,7 @@ namespace np {
}
__host__ __device__
Array& operator=(const Array& array) noexcept {
constexpr Array& operator=(const Array& array) noexcept {
#if __DEBUG
printf("Array created (assign copy)\n");
#endif
@@ -409,7 +408,7 @@ namespace np {
}
__host__ __device__
Array& operator=(Array&& array) noexcept {
constexpr Array& operator=(Array&& array) noexcept {
#if __DEBUG
printf("Array created (assign move)\n");
#endif
@@ -456,36 +455,36 @@ namespace np {
};
template<typename T>
inline Array<T> empty(Shape&& shape) noexcept {
constexpr Array<T> empty(Shape&& shape) noexcept {
return Array<T>(shape);
}
template<typename T>
inline Array<T> empty(const Shape& shape) noexcept {
constexpr Array<T> empty(const Shape& shape) noexcept {
return Array<T>(shape);
}
template<typename T>
inline Array<T> empty(const std::initializer_list<size_t>& dims) noexcept {
constexpr Array<T> empty(const std::initializer_list<size_t>& dims) noexcept {
return Array<T>(dims);
}
template<typename T>
Array<T> zeros(Shape&& shape) noexcept {
constexpr Array<T> zeros(Shape&& shape) noexcept {
Array<T> res(shape);
memset(res.data, 0, sizeof(T) * np::prod(res.shape));
return res;
}
template<typename T>
Array<T> zeros(const Shape& shape) noexcept {
constexpr Array<T> zeros(const Shape& shape) noexcept {
Array<T> res(shape);
memset(res.data, 0, sizeof(T) * np::prod(res.shape));
return res;
}
template<typename T>
Array<T> zeros(const std::initializer_list<size_t>& dims) noexcept {
constexpr Array<T> zeros(const std::initializer_list<size_t>& dims) noexcept {
Array<T> res(dims);
memset(res.data, 0, sizeof(T) * np::prod(res.shape));
return res;
@@ -599,7 +598,7 @@ namespace np {
template<typename T>
template<typename F>
Array<T> Array<T>::operator-(const F& other) const {
constexpr Array<T> Array<T>::operator-(const F& other) const noexcept {
np::Array<T> res = np::empty<T>(shape);
const size_t total = prod(shape);
for(size_t i = 0; i < total; ++i)
@@ -609,7 +608,7 @@ namespace np {
template<typename T>
template<typename F>
Array<T> Array<T>::operator-(const np::Array<F>& other) const {
constexpr Array<T> Array<T>::operator-(const np::Array<F>& other) const {
#if __DEBUG
if (shape != other.shape){
printf("Incompatible shapes\n");
@@ -665,7 +664,7 @@ namespace np {
}
template<typename T, typename F>
np::Array<T> pow(const F& k, const Array<T>& array) noexcept {
constexpr np::Array<T> pow(const F& k, const Array<T>& array) noexcept {
np::Array<T> result = np::empty<T>(array.shape);
const size_t total = prod(array.shape);
for(size_t i = 0; i < total; ++i)
@@ -692,7 +691,7 @@ namespace np {
//}
template<typename T, typename F>
Array<T> astype(const Array<F>& array) noexcept {
constexpr Array<T> astype(const Array<F>& array) noexcept {
Array<T> res = empty<T>(array.shape);
const size_t total = prod(array.shape);
for(size_t i = 0; i < total; ++i)
@@ -701,7 +700,7 @@ namespace np {
}
template<typename T>
Array<T> operator-(const T& k, const Array<T>& other) noexcept {
constexpr Array<T> operator-(const T& k, const Array<T>& other) noexcept {
np::Array<T> res = empty<T>(other.shape);
const size_t total = prod(other.shape);
for(size_t i = 0; i < total; ++i)
@@ -748,7 +747,7 @@ constexpr np::Array<T>& map(np::Array<T>& a, const std::function<T(const size_t&
template<typename T>
__host__ __device__
constexpr inline static void swap(T* const a, T* const b) noexcept {
constexpr void swap(T* const a, T* const b) noexcept {
if (a == b) return;
const T temp = *a;
*a = *b;
@@ -793,7 +792,7 @@ std::array<np::Array<uint8_t>, 4> load_datasets(void);
void print_error_file(const char* const) noexcept;
template<typename T>
void save(const np::Array<T>& d, const char* const filename) {
constexpr void save(const np::Array<T>& d, const char* const filename) {
FILE* const output = fopen(filename, "wb");
if (output == NULL) {
print_error_file(filename);
@@ -808,7 +807,7 @@ void save(const np::Array<T>& d, const char* const filename) {
}
template<typename T>
np::Array<T> load(const char* const filename) {
constexpr np::Array<T> load(const char* const filename) {
FILE* const input = fopen(filename, "rb");
if (input == NULL) {
print_error_file(filename);
@@ -838,7 +837,7 @@ np::Array<T> load(const char* const filename) {
#ifdef __CUDACC__
template<typename T>
np::Array<T> copyToDevice(const char* const name, const np::Array<T>& array) noexcept {
constexpr np::Array<T> copyToDevice(const char* const name, const np::Array<T>& array) noexcept {
const size_t array_size = np::prod(array.shape) * sizeof(T);
const size_t shape_size = array.shape.length * sizeof(size_t);
np::Array<T> d_array;
@@ -869,7 +868,7 @@ constexpr void cudaFree(const char* const name, np::Array<T>& array) noexcept {
array.shape.data = nullptr;
}
constexpr inline void _print_cuda_error_(const char* const name, const cudaError_t& err) noexcept {
constexpr void _print_cuda_error_(const char* const name, const cudaError_t& err) noexcept {
if (err != cudaSuccess) fprintf(stderr, "Error: %s = %d : %s\n", name, err, cudaGetErrorString(err));
}
#endif