|
| 1 | +#ifndef FINUFFT_INCLUDE_CUFINUFFT_CONTRIB_HELPER_MATH_H |
| 2 | +#define FINUFFT_INCLUDE_CUFINUFFT_CONTRIB_HELPER_MATH_H |
| 3 | + |
| 4 | +#include <cuComplex.h> |
| 5 | + |
| 6 | +// This header provides some helper functions for cuComplex types. |
| 7 | +// It mainly wraps existing CUDA implementations to provide operator overloads |
| 8 | +// e.g. cuAdd, cuSub, cuMul, cuDiv, cuCreal, cuCimag, cuCabs, cuCarg, cuConj are all |
| 9 | +// provided by CUDA |
| 10 | + |
| 11 | +// Addition for cuDoubleComplex (double) with cuDoubleComplex (double) |
| 12 | +__host__ __device__ __forceinline__ cuDoubleComplex operator+( |
| 13 | + const cuDoubleComplex &a, const cuDoubleComplex &b) noexcept { |
| 14 | + return cuCadd(a, b); |
| 15 | +} |
| 16 | + |
| 17 | +// Subtraction for cuDoubleComplex (double) with cuDoubleComplex (double) |
| 18 | +__host__ __device__ __forceinline__ cuDoubleComplex operator-( |
| 19 | + const cuDoubleComplex &a, const cuDoubleComplex &b) noexcept { |
| 20 | + return cuCsub(a, b); |
| 21 | +} |
| 22 | + |
| 23 | +// Multiplication for cuDoubleComplex (double) with cuDoubleComplex (double) |
| 24 | +__host__ __device__ __forceinline__ cuDoubleComplex operator*( |
| 25 | + const cuDoubleComplex &a, const cuDoubleComplex &b) noexcept { |
| 26 | + return cuCmul(a, b); |
| 27 | +} |
| 28 | + |
| 29 | +// Division for cuDoubleComplex (double) with cuDoubleComplex (double) |
| 30 | +__host__ __device__ __forceinline__ cuDoubleComplex operator/( |
| 31 | + const cuDoubleComplex &a, const cuDoubleComplex &b) noexcept { |
| 32 | + return cuCdiv(a, b); |
| 33 | +} |
| 34 | + |
| 35 | +// Equality for cuDoubleComplex (double) with cuDoubleComplex (double) |
| 36 | +__host__ __device__ __forceinline__ bool operator==(const cuDoubleComplex &a, |
| 37 | + const cuDoubleComplex &b) noexcept { |
| 38 | + return cuCreal(a) == cuCreal(b) && cuCimag(a) == cuCimag(b); |
| 39 | +} |
| 40 | + |
| 41 | +// Inequality for cuDoubleComplex (double) with cuDoubleComplex (double) |
| 42 | +__host__ __device__ __forceinline__ bool operator!=(const cuDoubleComplex &a, |
| 43 | + const cuDoubleComplex &b) noexcept { |
| 44 | + return !(a == b); |
| 45 | +} |
| 46 | + |
| 47 | +// Addition for cuDoubleComplex (double) with double |
| 48 | +__host__ __device__ __forceinline__ cuDoubleComplex operator+(const cuDoubleComplex &a, |
| 49 | + double b) noexcept { |
| 50 | + return make_cuDoubleComplex(cuCreal(a) + b, cuCimag(a)); |
| 51 | +} |
| 52 | + |
| 53 | +__host__ __device__ __forceinline__ cuDoubleComplex operator+( |
| 54 | + double a, const cuDoubleComplex &b) noexcept { |
| 55 | + return make_cuDoubleComplex(a + cuCreal(b), cuCimag(b)); |
| 56 | +} |
| 57 | + |
| 58 | +// Subtraction for cuDoubleComplex (double) with double |
| 59 | +__host__ __device__ __forceinline__ cuDoubleComplex operator-(const cuDoubleComplex &a, |
| 60 | + double b) noexcept { |
| 61 | + return make_cuDoubleComplex(cuCreal(a) - b, cuCimag(a)); |
| 62 | +} |
| 63 | + |
| 64 | +__host__ __device__ __forceinline__ cuDoubleComplex operator-( |
| 65 | + double a, const cuDoubleComplex &b) noexcept { |
| 66 | + return make_cuDoubleComplex(a - cuCreal(b), -cuCimag(b)); |
| 67 | +} |
| 68 | + |
| 69 | +// Multiplication for cuDoubleComplex (double) with double |
| 70 | +__host__ __device__ __forceinline__ cuDoubleComplex operator*(const cuDoubleComplex &a, |
| 71 | + double b) noexcept { |
| 72 | + return make_cuDoubleComplex(cuCreal(a) * b, cuCimag(a) * b); |
| 73 | +} |
| 74 | + |
| 75 | +__host__ __device__ __forceinline__ cuDoubleComplex operator*( |
| 76 | + double a, const cuDoubleComplex &b) noexcept { |
| 77 | + return make_cuDoubleComplex(a * cuCreal(b), a * cuCimag(b)); |
| 78 | +} |
| 79 | + |
| 80 | +// Division for cuDoubleComplex (double) with double |
| 81 | +__host__ __device__ __forceinline__ cuDoubleComplex operator/(const cuDoubleComplex &a, |
| 82 | + double b) noexcept { |
| 83 | + return make_cuDoubleComplex(cuCreal(a) / b, cuCimag(a) / b); |
| 84 | +} |
| 85 | + |
| 86 | +__host__ __device__ __forceinline__ cuDoubleComplex operator/( |
| 87 | + double a, const cuDoubleComplex &b) noexcept { |
| 88 | + double denom = cuCreal(b) * cuCreal(b) + cuCimag(b) * cuCimag(b); |
| 89 | + return make_cuDoubleComplex((a * cuCreal(b)) / denom, (-a * cuCimag(b)) / denom); |
| 90 | +} |
| 91 | + |
| 92 | +// Addition for cuFloatComplex (float) with cuFloatComplex (float) |
| 93 | +__host__ __device__ __forceinline__ cuFloatComplex operator+( |
| 94 | + const cuFloatComplex &a, const cuFloatComplex &b) noexcept { |
| 95 | + return cuCaddf(a, b); |
| 96 | +} |
| 97 | + |
| 98 | +// Subtraction for cuFloatComplex (float) with cuFloatComplex (float) |
| 99 | +__host__ __device__ __forceinline__ cuFloatComplex operator-( |
| 100 | + const cuFloatComplex &a, const cuFloatComplex &b) noexcept { |
| 101 | + return cuCsubf(a, b); |
| 102 | +} |
| 103 | + |
| 104 | +// Multiplication for cuFloatComplex (float) with cuFloatComplex (float) |
| 105 | +__host__ __device__ __forceinline__ cuFloatComplex operator*( |
| 106 | + const cuFloatComplex &a, const cuFloatComplex &b) noexcept { |
| 107 | + return cuCmulf(a, b); |
| 108 | +} |
| 109 | + |
| 110 | +// Division for cuFloatComplex (float) with cuFloatComplex (float) |
| 111 | +__host__ __device__ __forceinline__ cuFloatComplex operator/( |
| 112 | + const cuFloatComplex &a, const cuFloatComplex &b) noexcept { |
| 113 | + return cuCdivf(a, b); |
| 114 | +} |
| 115 | + |
| 116 | +// Equality for cuFloatComplex (float) with cuFloatComplex (float) |
| 117 | +__host__ __device__ __forceinline__ bool operator==(const cuFloatComplex &a, |
| 118 | + const cuFloatComplex &b) noexcept { |
| 119 | + return cuCrealf(a) == cuCrealf(b) && cuCimagf(a) == cuCimagf(b); |
| 120 | +} |
| 121 | + |
| 122 | +// Inequality for cuFloatComplex (float) with cuFloatComplex (float) |
| 123 | +__host__ __device__ __forceinline__ bool operator!=(const cuFloatComplex &a, |
| 124 | + const cuFloatComplex &b) noexcept { |
| 125 | + return !(a == b); |
| 126 | +} |
| 127 | + |
| 128 | +// Addition for cuFloatComplex (float) with float |
| 129 | +__host__ __device__ __forceinline__ cuFloatComplex operator+(const cuFloatComplex &a, |
| 130 | + float b) noexcept { |
| 131 | + return make_cuFloatComplex(cuCrealf(a) + b, cuCimagf(a)); |
| 132 | +} |
| 133 | + |
| 134 | +__host__ __device__ __forceinline__ cuFloatComplex operator+( |
| 135 | + float a, const cuFloatComplex &b) noexcept { |
| 136 | + return make_cuFloatComplex(a + cuCrealf(b), cuCimagf(b)); |
| 137 | +} |
| 138 | + |
| 139 | +// Subtraction for cuFloatComplex (float) with float |
| 140 | +__host__ __device__ __forceinline__ cuFloatComplex operator-(const cuFloatComplex &a, |
| 141 | + float b) noexcept { |
| 142 | + return make_cuFloatComplex(cuCrealf(a) - b, cuCimagf(a)); |
| 143 | +} |
| 144 | + |
| 145 | +__host__ __device__ __forceinline__ cuFloatComplex operator-( |
| 146 | + float a, const cuFloatComplex &b) noexcept { |
| 147 | + return make_cuFloatComplex(a - cuCrealf(b), -cuCimagf(b)); |
| 148 | +} |
| 149 | + |
| 150 | +// Multiplication for cuFloatComplex (float) with float |
| 151 | +__host__ __device__ __forceinline__ cuFloatComplex operator*(const cuFloatComplex &a, |
| 152 | + float b) noexcept { |
| 153 | + return make_cuFloatComplex(cuCrealf(a) * b, cuCimagf(a) * b); |
| 154 | +} |
| 155 | + |
| 156 | +__host__ __device__ __forceinline__ cuFloatComplex operator*( |
| 157 | + float a, const cuFloatComplex &b) noexcept { |
| 158 | + return make_cuFloatComplex(a * cuCrealf(b), a * cuCimagf(b)); |
| 159 | +} |
| 160 | + |
| 161 | +// Division for cuFloatComplex (float) with float |
| 162 | +__host__ __device__ __forceinline__ cuFloatComplex operator/(const cuFloatComplex &a, |
| 163 | + float b) noexcept { |
| 164 | + return make_cuFloatComplex(cuCrealf(a) / b, cuCimagf(a) / b); |
| 165 | +} |
| 166 | + |
| 167 | +__host__ __device__ __forceinline__ cuFloatComplex operator/( |
| 168 | + float a, const cuFloatComplex &b) noexcept { |
| 169 | + float denom = cuCrealf(b) * cuCrealf(b) + cuCimagf(b) * cuCimagf(b); |
| 170 | + return make_cuFloatComplex((a * cuCrealf(b)) / denom, (-a * cuCimagf(b)) / denom); |
| 171 | +} |
| 172 | + |
| 173 | +#endif // FINUFFT_INCLUDE_CUFINUFFT_CONTRIB_HELPER_MATH_H |
0 commit comments