| 1 |
/* { dg-do run { target rv64 } } */ |
| 2 |
/* { dg-require-effective-target riscv_v_ok } */ |
| 3 |
/* { dg-options "-O3 -march=rv64gcv -mabi=lp64d --param=logical-op-non-short-circuit=0" } */ |
| 4 |
|
| 5 |
#include <riscv_vector.h> |
| 6 |
|
| 7 |
using v_uint8 = vuint8m2_t; |
| 8 |
using v_int8 = vint8m2_t; |
| 9 |
using v_uint16 = vuint16m2_t; |
| 10 |
using v_int16 = vint16m2_t; |
| 11 |
using v_uint32 = vuint32m2_t; |
| 12 |
using v_int32 = vint32m2_t; |
| 13 |
using v_uint64 = vuint64m2_t; |
| 14 |
using v_int64 = vint64m2_t; |
| 15 |
using v_float32 = vfloat32m2_t; |
| 16 |
using v_float64 = vfloat64m2_t; |
| 17 |
|
| 18 |
using uchar = unsigned char; |
| 19 |
using schar = signed char; |
| 20 |
using ushort = unsigned short; |
| 21 |
using uint = unsigned int; |
| 22 |
using uint64 = unsigned long int; |
| 23 |
using int64 = long int; |
| 24 |
|
| 25 |
struct Size |
| 26 |
{ |
| 27 |
int width; |
| 28 |
int height; |
| 29 |
}; |
| 30 |
|
| 31 |
template <class T> struct VTraits; |
| 32 |
|
| 33 |
template <> struct VTraits<vint32m1_t> |
| 34 |
{ |
| 35 |
static inline int vlanes () { return __riscv_vsetvlmax_e32m1 (); } |
| 36 |
using lane_type = int32_t; |
| 37 |
static const int max_nlanes = 1024 / 32 * 2; |
| 38 |
}; |
| 39 |
template <> struct VTraits<vint32m2_t> |
| 40 |
{ |
| 41 |
static inline int vlanes () { return __riscv_vsetvlmax_e32m2 (); } |
| 42 |
using lane_type = int32_t; |
| 43 |
static const int max_nlanes = 1024 / 32 * 2; |
| 44 |
}; |
| 45 |
template <> struct VTraits<vint32m4_t> |
| 46 |
{ |
| 47 |
static inline int vlanes () { return __riscv_vsetvlmax_e32m4 (); } |
| 48 |
using lane_type = int32_t; |
| 49 |
static const int max_nlanes = 1024 / 32 * 2; |
| 50 |
}; |
| 51 |
template <> struct VTraits<vint32m8_t> |
| 52 |
{ |
| 53 |
static inline int vlanes () { return __riscv_vsetvlmax_e32m8 (); } |
| 54 |
using lane_type = int32_t; |
| 55 |
static const int max_nlanes = 1024 / 32 * 2; |
| 56 |
}; |
| 57 |
|
| 58 |
template <> struct VTraits<vfloat64m1_t> |
| 59 |
{ |
| 60 |
static inline int vlanes () { return __riscv_vsetvlmax_e64m1 (); } |
| 61 |
using lane_type = double; |
| 62 |
static const int max_nlanes = 1024 / 64 * 2; |
| 63 |
}; |
| 64 |
template <> struct VTraits<vfloat64m2_t> |
| 65 |
{ |
| 66 |
static inline int vlanes () { return __riscv_vsetvlmax_e64m2 (); } |
| 67 |
using lane_type = double; |
| 68 |
static const int max_nlanes = 1024 / 64 * 2; |
| 69 |
}; |
| 70 |
template <> struct VTraits<vfloat64m4_t> |
| 71 |
{ |
| 72 |
static inline int vlanes () { return __riscv_vsetvlmax_e64m4 (); } |
| 73 |
using lane_type = double; |
| 74 |
static const int max_nlanes = 1024 / 64 * 2; |
| 75 |
}; |
| 76 |
template <> struct VTraits<vfloat64m8_t> |
| 77 |
{ |
| 78 |
static inline int vlanes () { return __riscv_vsetvlmax_e64m8 (); } |
| 79 |
using lane_type = double; |
| 80 |
static const int max_nlanes = 1024 / 64 * 2; |
| 81 |
}; |
| 82 |
|
| 83 |
static inline v_float64 |
| 84 |
v_setall_f64 (double v) |
| 85 |
{ |
| 86 |
return __riscv_vfmv_v_f_f64m2 (v, VTraits<v_float64>::vlanes ()); |
| 87 |
} |
| 88 |
static inline v_float64 |
| 89 |
vx_setall_f64 (double v) |
| 90 |
{ |
| 91 |
return v_setall_f64 (v); |
| 92 |
} |
| 93 |
|
| 94 |
inline v_int32 |
| 95 |
v_load_expand_q (const schar *ptr) |
| 96 |
{ |
| 97 |
return __riscv_vwcvt_x ( |
| 98 |
__riscv_vwcvt_x (__riscv_vle8_v_i8mf2 (ptr, VTraits<v_int32>::vlanes ()), |
| 99 |
VTraits<v_int32>::vlanes ()), |
| 100 |
VTraits<v_int32>::vlanes ()); |
| 101 |
} |
| 102 |
|
| 103 |
static inline v_int32 |
| 104 |
vx_load_expand_q (const schar *ptr) |
| 105 |
{ |
| 106 |
return v_load_expand_q (ptr); |
| 107 |
} |
| 108 |
|
| 109 |
inline v_float64 |
| 110 |
v_cvt_f64 (const v_int32 &a) |
| 111 |
{ |
| 112 |
return __riscv_vget_f64m2 (__riscv_vfwcvt_f (a, VTraits<v_int32>::vlanes ()), |
| 113 |
0); |
| 114 |
} |
| 115 |
|
| 116 |
inline v_float64 |
| 117 |
v_cvt_f64_high (const v_int32 &a) |
| 118 |
{ |
| 119 |
return __riscv_vget_f64m2 (__riscv_vfwcvt_f (a, VTraits<v_int32>::vlanes ()), |
| 120 |
1); |
| 121 |
} |
| 122 |
|
| 123 |
inline void |
| 124 |
v_store (double *ptr, const v_float64 &a) |
| 125 |
{ |
| 126 |
__riscv_vse64 (ptr, a, VTraits<v_float64>::vlanes ()); |
| 127 |
} |
| 128 |
|
| 129 |
static inline void |
| 130 |
v_store_pair_as (double *ptr, const v_float64 &a, const v_float64 &b) |
| 131 |
{ |
| 132 |
v_store (ptr, a); |
| 133 |
v_store (ptr + VTraits<v_float64>::vlanes (), b); |
| 134 |
} |
| 135 |
|
| 136 |
static inline void |
| 137 |
vx_load_pair_as (const schar *ptr, v_float64 &a, v_float64 &b) |
| 138 |
{ |
| 139 |
v_int32 v0 = vx_load_expand_q (ptr); |
| 140 |
a = v_cvt_f64 (v0); |
| 141 |
b = v_cvt_f64_high (v0); |
| 142 |
} |
| 143 |
|
| 144 |
inline v_float64 |
| 145 |
v_fma (const v_float64 &a, const v_float64 &b, const v_float64 &c) |
| 146 |
{ |
| 147 |
return __riscv_vfmacc_vv_f64m2 (c, a, b, VTraits<v_float64>::vlanes ()); |
| 148 |
} |
| 149 |
|
| 150 |
template <typename _Tp> |
| 151 |
static inline _Tp |
| 152 |
saturate_cast (double v) |
| 153 |
{ |
| 154 |
return _Tp (v); |
| 155 |
} |
| 156 |
|
| 157 |
template <typename _Ts, typename _Td> |
| 158 |
__attribute__ ((noipa)) void |
| 159 |
cvt_64f (const _Ts *src, size_t sstep, _Td *dst, size_t dstep, Size size, |
| 160 |
double a, double b) |
| 161 |
{ |
| 162 |
v_float64 va = vx_setall_f64 (a), vb = vx_setall_f64 (b); |
| 163 |
const int VECSZ = VTraits<v_float64>::vlanes () * 2; |
| 164 |
|
| 165 |
sstep /= sizeof (src[0]); |
| 166 |
dstep /= sizeof (dst[0]); |
| 167 |
|
| 168 |
for (int i = 0; i < size.height; i++, src += sstep, dst += dstep) |
| 169 |
{ |
| 170 |
int j = 0; |
| 171 |
|
| 172 |
for (; j < size.width; j += VECSZ) |
| 173 |
{ |
| 174 |
if (j > size.width - VECSZ) |
| 175 |
{ |
| 176 |
if (j == 0 || src == (_Ts *) dst) |
| 177 |
break; |
| 178 |
j = size.width - VECSZ; |
| 179 |
} |
| 180 |
v_float64 v0, v1; |
| 181 |
vx_load_pair_as (src + j, v0, v1); |
| 182 |
v0 = v_fma (v0, va, vb); |
| 183 |
v1 = v_fma (v1, va, vb); |
| 184 |
v_store_pair_as (dst + j, v0, v1); |
| 185 |
} |
| 186 |
|
| 187 |
for (; j < size.width; j++) |
| 188 |
dst[j] = saturate_cast<_Td> (src[j] * a + b); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
void |
| 193 |
__attribute__ ((noipa)) |
| 194 |
cvtScale8s64f (const uchar *src_, size_t sstep, const uchar *, size_t, |
| 195 |
uchar *dst_, size_t dstep, Size size, void *scale_) |
| 196 |
{ |
| 197 |
const schar *src = (const schar *) src_; |
| 198 |
double *dst = (double *) dst_; |
| 199 |
double *scale = (double *) scale_; |
| 200 |
cvt_64f (src, sstep, dst, dstep, size, (double) scale[0], (double) scale[1]); |
| 201 |
} |
| 202 |
|
| 203 |
int main () |
| 204 |
{ |
| 205 |
uchar src[1024]; |
| 206 |
uchar dst[1024]; |
| 207 |
|
| 208 |
double scale[2] = {2.0, 3.0}; |
| 209 |
Size size {4, 1}; |
| 210 |
|
| 211 |
cvtScale8s64f (src, 4, NULL, 0, dst, 32, size, (void *)scale); |
| 212 |
} |