@@ -12,27 +12,75 @@ macro_rules! impl_integer_reductions {
12
12
LaneCount <LANES >: SupportedLaneCount ,
13
13
{
14
14
/// Reducing wrapping add. Returns the sum of the lanes of the vector, with wrapping addition.
15
+ ///
16
+ /// # Examples
17
+ ///
18
+ /// ```
19
+ /// # #![feature(portable_simd)]
20
+ /// # use core::simd::Simd;
21
+ #[ doc = concat!( "# use core::simd::" , stringify!( $scalar) , "x4;" ) ]
22
+ #[ doc = concat!( "let v = " , stringify!( $scalar) , "x4::from_array([1, 2, 3, 4]);" ) ]
23
+ /// assert_eq!(v.reduce_sum(), 10);
24
+ ///
25
+ /// // SIMD integer addition is always wrapping
26
+ #[ doc = concat!( "let v = " , stringify!( $scalar) , "x4::from_array([" , stringify!( $scalar) , "::MAX, 1, 0, 0]);" ) ]
27
+ #[ doc = concat!( "assert_eq!(v.reduce_sum(), " , stringify!( $scalar) , "::MIN);" ) ]
28
+ /// ```
15
29
#[ inline]
16
30
pub fn reduce_sum( self ) -> $scalar {
17
31
// Safety: `self` is an integer vector
18
32
unsafe { simd_reduce_add_ordered( self , 0 ) }
19
33
}
20
34
21
- /// Reducing wrapping multiply. Returns the product of the lanes of the vector, with wrapping multiplication.
35
+ /// Reducing wrapping multiply. Returns the product of the lanes of the vector, with wrapping multiplication.
36
+ ///
37
+ /// # Examples
38
+ ///
39
+ /// ```
40
+ /// # #![feature(portable_simd)]
41
+ /// # use core::simd::Simd;
42
+ #[ doc = concat!( "# use core::simd::" , stringify!( $scalar) , "x4;" ) ]
43
+ #[ doc = concat!( "let v = " , stringify!( $scalar) , "x4::from_array([1, 2, 3, 4]);" ) ]
44
+ /// assert_eq!(v.reduce_product(), 24);
45
+ ///
46
+ /// // SIMD integer multiplication is always wrapping
47
+ #[ doc = concat!( "let v = " , stringify!( $scalar) , "x4::from_array([" , stringify!( $scalar) , "::MAX, 2, 1, 1]);" ) ]
48
+ #[ doc = concat!( "assert!(v.reduce_product() < " , stringify!( $scalar) , "::MAX);" ) ]
49
+ /// ```
22
50
#[ inline]
23
51
pub fn reduce_product( self ) -> $scalar {
24
52
// Safety: `self` is an integer vector
25
53
unsafe { simd_reduce_mul_ordered( self , 1 ) }
26
54
}
27
55
28
56
/// Reducing maximum. Returns the maximum lane in the vector.
57
+ ///
58
+ /// # Examples
59
+ ///
60
+ /// ```
61
+ /// # #![feature(portable_simd)]
62
+ /// # use core::simd::Simd;
63
+ #[ doc = concat!( "# use core::simd::" , stringify!( $scalar) , "x4;" ) ]
64
+ #[ doc = concat!( "let v = " , stringify!( $scalar) , "x4::from_array([1, 2, 3, 4]);" ) ]
65
+ /// assert_eq!(v.reduce_max(), 4);
66
+ /// ```
29
67
#[ inline]
30
68
pub fn reduce_max( self ) -> $scalar {
31
69
// Safety: `self` is an integer vector
32
70
unsafe { simd_reduce_max( self ) }
33
71
}
34
72
35
73
/// Reducing minimum. Returns the minimum lane in the vector.
74
+ ///
75
+ /// # Examples
76
+ ///
77
+ /// ```
78
+ /// # #![feature(portable_simd)]
79
+ /// # use core::simd::Simd;
80
+ #[ doc = concat!( "# use core::simd::" , stringify!( $scalar) , "x4;" ) ]
81
+ #[ doc = concat!( "let v = " , stringify!( $scalar) , "x4::from_array([1, 2, 3, 4]);" ) ]
82
+ /// assert_eq!(v.reduce_min(), 1);
83
+ /// ```
36
84
#[ inline]
37
85
pub fn reduce_min( self ) -> $scalar {
38
86
// Safety: `self` is an integer vector
@@ -61,6 +109,16 @@ macro_rules! impl_float_reductions {
61
109
{
62
110
63
111
/// Reducing add. Returns the sum of the lanes of the vector.
112
+ ///
113
+ /// # Examples
114
+ ///
115
+ /// ```
116
+ /// # #![feature(portable_simd)]
117
+ /// # use core::simd::Simd;
118
+ #[ doc = concat!( "# use core::simd::" , stringify!( $scalar) , "x2;" ) ]
119
+ #[ doc = concat!( "let v = " , stringify!( $scalar) , "x2::from_array([1., 2.]);" ) ]
120
+ /// assert_eq!(v.reduce_sum(), 3.);
121
+ /// ```
64
122
#[ inline]
65
123
pub fn reduce_sum( self ) -> $scalar {
66
124
// LLVM sum is inaccurate on i586
@@ -73,6 +131,16 @@ macro_rules! impl_float_reductions {
73
131
}
74
132
75
133
/// Reducing multiply. Returns the product of the lanes of the vector.
134
+ ///
135
+ /// # Examples
136
+ ///
137
+ /// ```
138
+ /// # #![feature(portable_simd)]
139
+ /// # use core::simd::Simd;
140
+ #[ doc = concat!( "# use core::simd::" , stringify!( $scalar) , "x2;" ) ]
141
+ #[ doc = concat!( "let v = " , stringify!( $scalar) , "x2::from_array([3., 4.]);" ) ]
142
+ /// assert_eq!(v.reduce_product(), 12.);
143
+ /// ```
76
144
#[ inline]
77
145
pub fn reduce_product( self ) -> $scalar {
78
146
// LLVM product is inaccurate on i586
@@ -87,7 +155,30 @@ macro_rules! impl_float_reductions {
87
155
/// Reducing maximum. Returns the maximum lane in the vector.
88
156
///
89
157
/// Returns values based on equality, so a vector containing both `0.` and `-0.` may
90
- /// return either. This function will not return `NaN` unless all lanes are `NaN`.
158
+ /// return either.
159
+ ///
160
+ /// This function will not return `NaN` unless all lanes are `NaN`.
161
+ ///
162
+ /// # Examples
163
+ ///
164
+ /// ```
165
+ /// # #![feature(portable_simd)]
166
+ /// # use core::simd::Simd;
167
+ #[ doc = concat!( "# use core::simd::" , stringify!( $scalar) , "x2;" ) ]
168
+ #[ doc = concat!( "let v = " , stringify!( $scalar) , "x2::from_array([1., 2.]);" ) ]
169
+ /// assert_eq!(v.reduce_max(), 2.);
170
+ ///
171
+ /// // NaN values are skipped...
172
+ #[ doc = concat!( "let v = " , stringify!( $scalar) , "x2::from_array([1., " , stringify!( $scalar) , "::NAN]);" ) ]
173
+ /// assert_eq!(v.reduce_max(), 1.);
174
+ ///
175
+ /// // ...unless all values are NaN
176
+ #[ doc = concat!( "let v = " , stringify!( $scalar) , "x2::from_array([" ,
177
+ stringify!( $scalar) , "::NAN, " ,
178
+ stringify!( $scalar) , "::NAN]);"
179
+ ) ]
180
+ /// assert!(v.reduce_max().is_nan());
181
+ /// ```
91
182
#[ inline]
92
183
pub fn reduce_max( self ) -> $scalar {
93
184
// Safety: `self` is a float vector
@@ -97,7 +188,30 @@ macro_rules! impl_float_reductions {
97
188
/// Reducing minimum. Returns the minimum lane in the vector.
98
189
///
99
190
/// Returns values based on equality, so a vector containing both `0.` and `-0.` may
100
- /// return either. This function will not return `NaN` unless all lanes are `NaN`.
191
+ /// return either.
192
+ ///
193
+ /// This function will not return `NaN` unless all lanes are `NaN`.
194
+ ///
195
+ /// # Examples
196
+ ///
197
+ /// ```
198
+ /// # #![feature(portable_simd)]
199
+ /// # use core::simd::Simd;
200
+ #[ doc = concat!( "# use core::simd::" , stringify!( $scalar) , "x2;" ) ]
201
+ #[ doc = concat!( "let v = " , stringify!( $scalar) , "x2::from_array([3., 7.]);" ) ]
202
+ /// assert_eq!(v.reduce_min(), 3.);
203
+ ///
204
+ /// // NaN values are skipped...
205
+ #[ doc = concat!( "let v = " , stringify!( $scalar) , "x2::from_array([1., " , stringify!( $scalar) , "::NAN]);" ) ]
206
+ /// assert_eq!(v.reduce_min(), 1.);
207
+ ///
208
+ /// // ...unless all values are NaN
209
+ #[ doc = concat!( "let v = " , stringify!( $scalar) , "x2::from_array([" ,
210
+ stringify!( $scalar) , "::NAN, " ,
211
+ stringify!( $scalar) , "::NAN]);"
212
+ ) ]
213
+ /// assert!(v.reduce_min().is_nan());
214
+ /// ```
101
215
#[ inline]
102
216
pub fn reduce_min( self ) -> $scalar {
103
217
// Safety: `self` is a float vector
0 commit comments