@@ -283,13 +283,20 @@ macro_rules! impl_unsigned_int_ops {
283283
284284 #[ inline]
285285 fn div( self , rhs: Self ) -> Self :: Output {
286- // TODO there is probably a better way of doing this
287- if AsRef :: <[ $scalar] >:: as_ref( & rhs)
286+ if rhs. as_slice( )
288287 . iter( )
289288 . any( |x| * x == 0 )
290289 {
291290 panic!( "attempt to divide by zero" ) ;
292291 }
292+
293+ // Guards for div(MIN, -1),
294+ // this check only applies to signed ints
295+ if <$scalar>:: MIN != 0 && self . as_slice( ) . iter( )
296+ . zip( rhs. as_slice( ) . iter( ) )
297+ . any( |( x, y) | * x == <$scalar>:: MIN && * y == -1 as _) {
298+ panic!( "attempt to divide with overflow" ) ;
299+ }
293300 unsafe { crate :: intrinsics:: simd_div( self , rhs) }
294301 }
295302 }
@@ -304,6 +311,11 @@ macro_rules! impl_unsigned_int_ops {
304311 if rhs == 0 {
305312 panic!( "attempt to divide by zero" ) ;
306313 }
314+ if <$scalar>:: MIN != 0 &&
315+ self . as_slice( ) . iter( ) . any( |x| * x == <$scalar>:: MIN ) &&
316+ rhs == -1 as _ {
317+ panic!( "attempt to divide with overflow" ) ;
318+ }
307319 let rhs = Self :: splat( rhs) ;
308320 unsafe { crate :: intrinsics:: simd_div( self , rhs) }
309321 }
@@ -353,6 +365,14 @@ macro_rules! impl_unsigned_int_ops {
353365 {
354366 panic!( "attempt to calculate the remainder with a divisor of zero" ) ;
355367 }
368+
369+ // Guards for rem(MIN, -1)
370+ // this branch applies the check only to signed ints
371+ if <$scalar>:: MIN != 0 && self . as_slice( ) . iter( )
372+ . zip( rhs. as_slice( ) . iter( ) )
373+ . any( |( x, y) | * x == <$scalar>:: MIN && * y == -1 as _) {
374+ panic!( "attempt to calculate the remainder with overflow" ) ;
375+ }
356376 unsafe { crate :: intrinsics:: simd_rem( self , rhs) }
357377 }
358378 }
@@ -367,6 +387,11 @@ macro_rules! impl_unsigned_int_ops {
367387 if rhs == 0 {
368388 panic!( "attempt to calculate the remainder with a divisor of zero" ) ;
369389 }
390+ if <$scalar>:: MIN != 0 &&
391+ self . as_slice( ) . iter( ) . any( |x| * x == <$scalar>:: MIN ) &&
392+ rhs == -1 as _ {
393+ panic!( "attempt to calculate the remainder with overflow" ) ;
394+ }
370395 let rhs = Self :: splat( rhs) ;
371396 unsafe { crate :: intrinsics:: simd_rem( self , rhs) }
372397 }
0 commit comments