Skip to content

Commit 0cbf655

Browse files
committed
CLJ-2670 - Use Math exact methods where possible for perf
Signed-off-by: Alex Miller <alex.miller@cognitect.com>
1 parent bf0c0e2 commit 0cbf655

3 files changed

Lines changed: 8 additions & 28 deletions

File tree

src/jvm/clojure/lang/Numbers.java

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,10 +1915,7 @@ static public Number unchecked_dec(Object x){
19151915
static public double remainder(long x, double y){return remainder((double)x,y);}
19161916

19171917
static public long add(long x, long y){
1918-
long ret = x + y;
1919-
if ((ret ^ x) < 0 && (ret ^ y) < 0)
1920-
return throwIntOverflow();
1921-
return ret;
1918+
return Math.addExact(x, y);
19221919
}
19231920

19241921
static public Number addP(long x, long y){
@@ -1929,10 +1926,7 @@ static public Number addP(long x, long y){
19291926
}
19301927

19311928
static public long minus(long x, long y){
1932-
long ret = x - y;
1933-
if (((ret ^ x) < 0 && (ret ^ ~y) < 0))
1934-
return throwIntOverflow();
1935-
return ret;
1929+
return Math.subtractExact(x, y);
19361930
}
19371931

19381932
static public Number minusP(long x, long y){
@@ -1943,9 +1937,7 @@ static public Number minusP(long x, long y){
19431937
}
19441938

19451939
static public long minus(long x){
1946-
if(x == Long.MIN_VALUE)
1947-
return throwIntOverflow();
1948-
return -x;
1940+
return Math.negateExact(x);
19491941
}
19501942

19511943
static public Number minusP(long x){
@@ -1955,9 +1947,7 @@ static public Number minusP(long x){
19551947
}
19561948

19571949
static public long inc(long x){
1958-
if(x == Long.MAX_VALUE)
1959-
return throwIntOverflow();
1960-
return x + 1;
1950+
return Math.incrementExact(x);
19611951
}
19621952

19631953
static public Number incP(long x){
@@ -1967,9 +1957,7 @@ static public Number incP(long x){
19671957
}
19681958

19691959
static public long dec(long x){
1970-
if(x == Long.MIN_VALUE)
1971-
return throwIntOverflow();
1972-
return x - 1;
1960+
return Math.decrementExact(x);
19731961
}
19741962

19751963
static public Number decP(long x){
@@ -1980,12 +1968,7 @@ static public Number decP(long x){
19801968

19811969

19821970
static public long multiply(long x, long y){
1983-
if (x == Long.MIN_VALUE && y < 0)
1984-
return throwIntOverflow();
1985-
long ret = x * y;
1986-
if (y != 0 && ret/y != x)
1987-
return throwIntOverflow();
1988-
return ret;
1971+
return Math.multiplyExact(x, y);
19891972
}
19901973

19911974
static public Number multiplyP(long x, long y){

src/jvm/clojure/lang/RT.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,10 +1244,7 @@ static public int intCast(float x){
12441244
}
12451245

12461246
static public int intCast(long x){
1247-
int i = (int) x;
1248-
if(i != x)
1249-
throw new IllegalArgumentException("Value out of range for int: " + x);
1250-
return i;
1247+
return Math.toIntExact(x);
12511248
}
12521249

12531250
static public int intCast(double x){

test/clojure/test_clojure/numbers.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@
181181
(let [wrapped (fn [x]
182182
(try
183183
(f x)
184-
(catch IllegalArgumentException e :error)))]
184+
(catch RuntimeException e :error)))]
185185
(is (= vals (map wrapped inputs)))))))
186186

187187
;; *** Functions ***

0 commit comments

Comments
 (0)