Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/expression/SLDivNode.java @ 21121:b625b459cf45
SL: fix division long overflow. Contributed-by: Raffaello Giulietti <raffaello.giulietti@supsi.ch>
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Mon, 27 Apr 2015 17:24:27 +0200 |
parents | abe7128ca473 |
children |
comparison
equal
deleted
inserted
replaced
21120:c92676a390b8 | 21121:b625b459cf45 |
---|---|
39 | 39 |
40 public SLDivNode(SourceSection src) { | 40 public SLDivNode(SourceSection src) { |
41 super(src); | 41 super(src); |
42 } | 42 } |
43 | 43 |
44 @Specialization | 44 @Specialization(rewriteOn = ArithmeticException.class) |
45 protected long div(long left, long right) { | 45 protected long div(long left, long right) throws ArithmeticException { |
46 /* No overflow is possible on a division. */ | 46 long result = left / right; |
47 return left / right; | 47 /* |
48 * The division overflows if left is Long.MIN_VALUE and right is -1. | |
49 */ | |
50 if ((left & right & result) < 0) { | |
51 throw new ArithmeticException("long overflow"); | |
52 } | |
53 return result; | |
48 } | 54 } |
49 | 55 |
50 @Specialization | 56 @Specialization |
51 protected BigInteger div(BigInteger left, BigInteger right) { | 57 protected BigInteger div(BigInteger left, BigInteger right) { |
52 return left.divide(right); | 58 return left.divide(right); |