@@ -94,7 +94,13 @@ depending on the target pointer size.
9494}
9595
9696macro_rules! widening_impl {
97- ( $SelfT: ty, $WideT: ty, $BITS: literal) => {
97+ ( $SelfT: ty, $WideT: ty, $BITS: literal, unsigned) => {
98+ widening_impl!( $SelfT, $WideT, $BITS, "" ) ;
99+ } ;
100+ ( $SelfT: ty, $WideT: ty, $BITS: literal, signed) => {
101+ widening_impl!( $SelfT, $WideT, $BITS, "# //" ) ;
102+ } ;
103+ ( $SelfT: ty, $WideT: ty, $BITS: literal, $AdaptiveTestPrefix: literal) => {
98104 /// Calculates the complete product `self * rhs` without the possibility to overflow.
99105 ///
100106 /// This returns the low-order (wrapping) bits and the high-order (overflow) bits
@@ -148,6 +154,33 @@ macro_rules! widening_impl {
148154 /// assert_eq!(5u32.carrying_mul(2, 10), (20, 0));
149155 /// assert_eq!(1_000_000_000u32.carrying_mul(10, 0), (1410065408, 2));
150156 /// assert_eq!(1_000_000_000u32.carrying_mul(10, 10), (1410065418, 2));
157+ #[ doc = concat!( $AdaptiveTestPrefix, "assert_eq!(" ,
158+ stringify!( $SelfT) , "::MAX.carrying_mul(" , stringify!( $SelfT) , "::MAX, " , stringify!( $SelfT) , "::MAX), " ,
159+ "(0, " , stringify!( $SelfT) , "::MAX));"
160+ ) ]
161+ /// ```
162+ ///
163+ /// If `carry` is zero, this is similar to [`overflowing_mul`](Self::overflowing_mul),
164+ /// except that it gives the value of the overflow instead of just whether one happened:
165+ ///
166+ /// ```
167+ /// #![feature(bigint_helper_methods)]
168+ /// let r = u8::carrying_mul(7, 13, 0);
169+ /// assert_eq!((r.0, r.1 != 0), u8::overflowing_mul(7, 13));
170+ /// let r = u8::carrying_mul(13, 42, 0);
171+ /// assert_eq!((r.0, r.1 != 0), u8::overflowing_mul(13, 42));
172+ /// ```
173+ ///
174+ /// The value of the first field in the returned tuple matches what you'd get
175+ /// by combining the [`wrapping_mul`](Self::wrapping_mul) and
176+ /// [`wrapping_add`](Self::wrapping_add) methods:
177+ ///
178+ /// ```
179+ /// #![feature(bigint_helper_methods)]
180+ /// assert_eq!(
181+ /// 789_u16.carrying_mul(456, 123).0,
182+ /// 789_u16.wrapping_mul(456).wrapping_add(123),
183+ /// );
151184 /// ```
152185 #[ unstable( feature = "bigint_helper_methods" , issue = "85532" ) ]
153186 #[ rustc_const_unstable( feature = "bigint_helper_methods" , issue = "85532" ) ]
@@ -168,29 +201,29 @@ macro_rules! widening_impl {
168201
169202#[ lang = "i8" ]
170203impl i8 {
171- widening_impl ! { i8 , i16 , 8 }
204+ widening_impl ! { i8 , i16 , 8 , signed }
172205 int_impl ! { i8 , i8 , u8 , 8 , 7 , -128 , 127 , 2 , "-0x7e" , "0xa" , "0x12" , "0x12" , "0x48" ,
173206 "[0x12]" , "[0x12]" , "" , "" }
174207}
175208
176209#[ lang = "i16" ]
177210impl i16 {
178- widening_impl ! { i16 , i32 , 16 }
211+ widening_impl ! { i16 , i32 , 16 , signed }
179212 int_impl ! { i16 , i16 , u16 , 16 , 15 , -32768 , 32767 , 4 , "-0x5ffd" , "0x3a" , "0x1234" , "0x3412" ,
180213 "0x2c48" , "[0x34, 0x12]" , "[0x12, 0x34]" , "" , "" }
181214}
182215
183216#[ lang = "i32" ]
184217impl i32 {
185- widening_impl ! { i32 , i64 , 32 }
218+ widening_impl ! { i32 , i64 , 32 , signed }
186219 int_impl ! { i32 , i32 , u32 , 32 , 31 , -2147483648 , 2147483647 , 8 , "0x10000b3" , "0xb301" ,
187220 "0x12345678" , "0x78563412" , "0x1e6a2c48" , "[0x78, 0x56, 0x34, 0x12]" ,
188221 "[0x12, 0x34, 0x56, 0x78]" , "" , "" }
189222}
190223
191224#[ lang = "i64" ]
192225impl i64 {
193- widening_impl ! { i64 , i128 , 64 }
226+ widening_impl ! { i64 , i128 , 64 , signed }
194227 int_impl ! { i64 , i64 , u64 , 64 , 63 , -9223372036854775808 , 9223372036854775807 , 12 ,
195228 "0xaa00000000006e1" , "0x6e10aa" , "0x1234567890123456" , "0x5634129078563412" ,
196229 "0x6a2c48091e6a2c48" , "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]" ,
@@ -212,7 +245,7 @@ impl i128 {
212245#[ cfg( target_pointer_width = "16" ) ]
213246#[ lang = "isize" ]
214247impl isize {
215- widening_impl ! { isize , i32 , 16 }
248+ widening_impl ! { isize , i32 , 16 , signed }
216249 int_impl ! { isize , i16 , usize , 16 , 15 , -32768 , 32767 , 4 , "-0x5ffd" , "0x3a" , "0x1234" ,
217250 "0x3412" , "0x2c48" , "[0x34, 0x12]" , "[0x12, 0x34]" ,
218251 usize_isize_to_xe_bytes_doc!( ) , usize_isize_from_xe_bytes_doc!( ) }
@@ -221,7 +254,7 @@ impl isize {
221254#[ cfg( target_pointer_width = "32" ) ]
222255#[ lang = "isize" ]
223256impl isize {
224- widening_impl ! { isize , i64 , 32 }
257+ widening_impl ! { isize , i64 , 32 , signed }
225258 int_impl ! { isize , i32 , usize , 32 , 31 , -2147483648 , 2147483647 , 8 , "0x10000b3" , "0xb301" ,
226259 "0x12345678" , "0x78563412" , "0x1e6a2c48" , "[0x78, 0x56, 0x34, 0x12]" ,
227260 "[0x12, 0x34, 0x56, 0x78]" ,
@@ -231,7 +264,7 @@ impl isize {
231264#[ cfg( target_pointer_width = "64" ) ]
232265#[ lang = "isize" ]
233266impl isize {
234- widening_impl ! { isize , i128 , 64 }
267+ widening_impl ! { isize , i128 , 64 , signed }
235268 int_impl ! { isize , i64 , usize , 64 , 63 , -9223372036854775808 , 9223372036854775807 ,
236269 12 , "0xaa00000000006e1" , "0x6e10aa" , "0x1234567890123456" , "0x5634129078563412" ,
237270 "0x6a2c48091e6a2c48" , "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]" ,
@@ -244,7 +277,7 @@ const ASCII_CASE_MASK: u8 = 0b0010_0000;
244277
245278#[ lang = "u8" ]
246279impl u8 {
247- widening_impl ! { u8 , u16 , 8 }
280+ widening_impl ! { u8 , u16 , 8 , unsigned }
248281 uint_impl ! { u8 , u8 , i8 , 8 , 255 , 2 , "0x82" , "0xa" , "0x12" , "0x12" , "0x48" , "[0x12]" ,
249282 "[0x12]" , "" , "" }
250283
@@ -793,21 +826,21 @@ impl u8 {
793826
794827#[ lang = "u16" ]
795828impl u16 {
796- widening_impl ! { u16 , u32 , 16 }
829+ widening_impl ! { u16 , u32 , 16 , unsigned }
797830 uint_impl ! { u16 , u16 , i16 , 16 , 65535 , 4 , "0xa003" , "0x3a" , "0x1234" , "0x3412" , "0x2c48" ,
798831 "[0x34, 0x12]" , "[0x12, 0x34]" , "" , "" }
799832}
800833
801834#[ lang = "u32" ]
802835impl u32 {
803- widening_impl ! { u32 , u64 , 32 }
836+ widening_impl ! { u32 , u64 , 32 , unsigned }
804837 uint_impl ! { u32 , u32 , i32 , 32 , 4294967295 , 8 , "0x10000b3" , "0xb301" , "0x12345678" ,
805838 "0x78563412" , "0x1e6a2c48" , "[0x78, 0x56, 0x34, 0x12]" , "[0x12, 0x34, 0x56, 0x78]" , "" , "" }
806839}
807840
808841#[ lang = "u64" ]
809842impl u64 {
810- widening_impl ! { u64 , u128 , 64 }
843+ widening_impl ! { u64 , u128 , 64 , unsigned }
811844 uint_impl ! { u64 , u64 , i64 , 64 , 18446744073709551615 , 12 , "0xaa00000000006e1" , "0x6e10aa" ,
812845 "0x1234567890123456" , "0x5634129078563412" , "0x6a2c48091e6a2c48" ,
813846 "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]" ,
@@ -830,15 +863,15 @@ impl u128 {
830863#[ cfg( target_pointer_width = "16" ) ]
831864#[ lang = "usize" ]
832865impl usize {
833- widening_impl ! { usize , u32 , 16 }
866+ widening_impl ! { usize , u32 , 16 , unsigned }
834867 uint_impl ! { usize , u16 , isize , 16 , 65535 , 4 , "0xa003" , "0x3a" , "0x1234" , "0x3412" , "0x2c48" ,
835868 "[0x34, 0x12]" , "[0x12, 0x34]" ,
836869 usize_isize_to_xe_bytes_doc!( ) , usize_isize_from_xe_bytes_doc!( ) }
837870}
838871#[ cfg( target_pointer_width = "32" ) ]
839872#[ lang = "usize" ]
840873impl usize {
841- widening_impl ! { usize , u64 , 32 }
874+ widening_impl ! { usize , u64 , 32 , unsigned }
842875 uint_impl ! { usize , u32 , isize , 32 , 4294967295 , 8 , "0x10000b3" , "0xb301" , "0x12345678" ,
843876 "0x78563412" , "0x1e6a2c48" , "[0x78, 0x56, 0x34, 0x12]" , "[0x12, 0x34, 0x56, 0x78]" ,
844877 usize_isize_to_xe_bytes_doc!( ) , usize_isize_from_xe_bytes_doc!( ) }
@@ -847,7 +880,7 @@ impl usize {
847880#[ cfg( target_pointer_width = "64" ) ]
848881#[ lang = "usize" ]
849882impl usize {
850- widening_impl ! { usize , u128 , 64 }
883+ widening_impl ! { usize , u128 , 64 , unsigned }
851884 uint_impl ! { usize , u64 , isize , 64 , 18446744073709551615 , 12 , "0xaa00000000006e1" , "0x6e10aa" ,
852885 "0x1234567890123456" , "0x5634129078563412" , "0x6a2c48091e6a2c48" ,
853886 "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]" ,
0 commit comments