This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathsimd_vsx.h
162 lines (132 loc) · 4.69 KB
/
simd_vsx.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DIMSUM_SIMD_VSX_H_
#define DIMSUM_SIMD_VSX_H_
#include <altivec.h>
#include "operations.h"
namespace dimsum {
template <typename T>
Simd128<T> abs(Simd128<T> simd) {
return vec_abs(to_raw(simd));
}
template <>
inline Simd128<float> reciprocal_estimate(Simd128<float> simd) {
return vec_re(to_raw(simd));
}
template <>
inline Simd128<float> sqrt(Simd128<float> simd) {
return vec_sqrt(to_raw(simd));
}
template <>
inline Simd128<double> sqrt(Simd128<double> simd) {
return vec_sqrt(to_raw(simd));
}
template <>
inline Simd128<float> reciprocal_sqrt_estimate(Simd128<float> simd) {
return vec_rsqrte(to_raw(simd));
}
template <typename T>
Simd128<T> add_saturated(Simd128<T> lhs, Simd128<T> rhs) {
return vec_adds(to_raw(lhs), to_raw(rhs));
}
template <typename T>
Simd128<T> sub_saturated(Simd128<T> lhs, Simd128<T> rhs) {
return vec_subs(to_raw(lhs), to_raw(rhs));
}
template <>
inline Simd128<int32> reduce_add<int32, 4>(Simd128<int8> simd) {
return vec_sum4s(to_raw(simd), to_raw(Simd128<int32>(0)));
}
template <>
inline Simd128<uint32> reduce_add<uint32, 4>(Simd128<uint8> simd) {
return vec_sum4s(to_raw(simd), to_raw(Simd128<uint32>(0)));
}
template <>
inline Simd128<int32> reduce_add<int32, 4>(Simd128<int16> simd) {
return vec_sum4s(to_raw(simd), to_raw(Simd128<int32>(0)));
}
template <>
inline Simd128<uint32> reduce_add<uint32, 4>(Simd128<uint16> simd) {
return vec_msum(to_raw(simd), to_raw(Simd128<uint16>(1)),
to_raw(Simd128<uint32>(0)));
}
// Common reduce_add<T, 1> specializations.
// vec_sums(vector int, vector int) (vsumsws) returns saturated sum.
// Note, reduce_add overflow has undefined behavior, so we are free to use this
// saturation semantics.
template <>
inline ResizeTo<Simd128<int32>, 1> reduce_add<int32, 1>(Simd128<int32> simd) {
return vec_sums(to_raw(simd), to_raw(Simd128<int32>(0)))[3];
}
template <>
inline ResizeTo<Simd128<uint32>, 1> reduce_add<uint32, 1>(Simd128<uint8> simd) {
return bit_cast<uint32>(
reduce_add<int32, 1>(bit_cast<int32>(reduce_add<uint32, 4>(simd))));
}
template <>
inline ResizeTo<Simd128<uint64>, 1> reduce_add<uint64, 1>(Simd128<uint8> simd) {
return reduce_add<uint32, 1>(simd)[0];
}
template <>
inline ResizeTo<Simd128<uint32>, 1> reduce_add<uint32, 1>(
Simd128<uint16> simd) {
return bit_cast<uint32>(
reduce_add<int32, 1>(bit_cast<int32>(reduce_add<uint32, 4>(simd))));
}
template <>
inline ResizeTo<Simd128<uint64>, 1> reduce_add<uint64, 1>(
Simd128<uint16> simd) {
return reduce_add<uint32, 1>(simd)[0];
}
template <>
inline Simd128<uint32> mul_sum<uint32>(Simd128<uint8> lhs, Simd128<uint8> rhs,
Simd128<uint32> acc) {
return vec_msum(to_raw(lhs), to_raw(rhs), to_raw(acc));
}
template <>
inline Simd128<int32> mul_sum<int32>(Simd128<int16> lhs, Simd128<int16> rhs,
Simd128<int32> acc) {
return vec_msum(to_raw(lhs), to_raw(rhs), to_raw(acc));
}
template <>
inline Simd128<uint32> mul_sum<uint32>(Simd128<uint16> lhs, Simd128<uint16> rhs,
Simd128<uint32> acc) {
return vec_msum(to_raw(lhs), to_raw(rhs), to_raw(acc));
}
// vec_rint calls XVR[SD]PIC which round floating points according to the
// Rounding Control field RN in FPSCR, which defaults to round-to-even.
template <>
inline Simd128<float> round(Simd128<float> simd) {
return vec_rint(to_raw(simd));
}
template <>
inline Simd128<double> round(Simd128<double> simd) {
return vec_rint(to_raw(simd));
}
// The alnerative implementation of float-to-int conversion on PPC. Note that
// there is no instruction to directly convert a f.p. vector to an integer
// vector. It is implememented by first rounding it to the nearest representable
// floating-point integers, and then truncating them to integers.
template <>
inline Simd128<int32> round_to_integer(Simd128<float> simd) {
return vec_cts(vec_rint(to_raw(simd)), 0);
}
template <typename T>
Simd128<ScaleBy<T, 2>> mul_widened(Simd64<T> lhs, Simd64<T> rhs) {
return simd_cast<ScaleBy<T, 2>>(lhs) * simd_cast<ScaleBy<T, 2>>(rhs);
}
} // namespace dimsum
#endif // DIMSUM_SIMD_VSX_H_