@@ -283,13 +283,20 @@ macro_rules! impl_unsigned_int_ops {
283
283
284
284
#[ inline]
285
285
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( )
288
287
. iter( )
289
288
. any( |x| * x == 0 )
290
289
{
291
290
panic!( "attempt to divide by zero" ) ;
292
291
}
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
+ }
293
300
unsafe { crate :: intrinsics:: simd_div( self , rhs) }
294
301
}
295
302
}
@@ -304,6 +311,11 @@ macro_rules! impl_unsigned_int_ops {
304
311
if rhs == 0 {
305
312
panic!( "attempt to divide by zero" ) ;
306
313
}
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
+ }
307
319
let rhs = Self :: splat( rhs) ;
308
320
unsafe { crate :: intrinsics:: simd_div( self , rhs) }
309
321
}
@@ -353,6 +365,14 @@ macro_rules! impl_unsigned_int_ops {
353
365
{
354
366
panic!( "attempt to calculate the remainder with a divisor of zero" ) ;
355
367
}
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
+ }
356
376
unsafe { crate :: intrinsics:: simd_rem( self , rhs) }
357
377
}
358
378
}
@@ -367,6 +387,11 @@ macro_rules! impl_unsigned_int_ops {
367
387
if rhs == 0 {
368
388
panic!( "attempt to calculate the remainder with a divisor of zero" ) ;
369
389
}
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
+ }
370
395
let rhs = Self :: splat( rhs) ;
371
396
unsafe { crate :: intrinsics:: simd_rem( self , rhs) }
372
397
}
0 commit comments