@@ -12,27 +12,75 @@ macro_rules! impl_integer_reductions {
1212 LaneCount <LANES >: SupportedLaneCount ,
1313 {
1414 /// 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+ /// ```
1529 #[ inline]
1630 pub fn reduce_sum( self ) -> $scalar {
1731 // Safety: `self` is an integer vector
1832 unsafe { simd_reduce_add_ordered( self , 0 ) }
1933 }
2034
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+ /// ```
2250 #[ inline]
2351 pub fn reduce_product( self ) -> $scalar {
2452 // Safety: `self` is an integer vector
2553 unsafe { simd_reduce_mul_ordered( self , 1 ) }
2654 }
2755
2856 /// 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+ /// ```
2967 #[ inline]
3068 pub fn reduce_max( self ) -> $scalar {
3169 // Safety: `self` is an integer vector
3270 unsafe { simd_reduce_max( self ) }
3371 }
3472
3573 /// 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+ /// ```
3684 #[ inline]
3785 pub fn reduce_min( self ) -> $scalar {
3886 // Safety: `self` is an integer vector
@@ -61,6 +109,16 @@ macro_rules! impl_float_reductions {
61109 {
62110
63111 /// 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+ /// ```
64122 #[ inline]
65123 pub fn reduce_sum( self ) -> $scalar {
66124 // LLVM sum is inaccurate on i586
@@ -73,6 +131,16 @@ macro_rules! impl_float_reductions {
73131 }
74132
75133 /// 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+ /// ```
76144 #[ inline]
77145 pub fn reduce_product( self ) -> $scalar {
78146 // LLVM product is inaccurate on i586
@@ -87,7 +155,30 @@ macro_rules! impl_float_reductions {
87155 /// Reducing maximum. Returns the maximum lane in the vector.
88156 ///
89157 /// 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+ /// ```
91182 #[ inline]
92183 pub fn reduce_max( self ) -> $scalar {
93184 // Safety: `self` is a float vector
@@ -97,7 +188,30 @@ macro_rules! impl_float_reductions {
97188 /// Reducing minimum. Returns the minimum lane in the vector.
98189 ///
99190 /// 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+ /// ```
101215 #[ inline]
102216 pub fn reduce_min( self ) -> $scalar {
103217 // Safety: `self` is a float vector
0 commit comments