Skip to content

Commit

Permalink
Normalize uncheckeds, quotient, remainder. Inline quot and rem. unche…
Browse files Browse the repository at this point in the history
…cked-foo-long are now just unchecked-foo.
  • Loading branch information
richhickey committed Nov 27, 2010
1 parent cbd789d commit 3e644c7
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 74 deletions.
70 changes: 29 additions & 41 deletions src/clj/clojure/core.clj
Expand Up @@ -1066,12 +1066,12 @@
:added "1.0"}
[x] (. clojure.lang.Numbers (unchecked_int_inc x)))

(defn unchecked-inc-long
(defn unchecked-inc
"Returns a number one greater than x, a long.
Note - uses a primitive operator subject to overflow."
{:inline (fn [x] `(. clojure.lang.Numbers (unchecked_long_inc ~x)))
{:inline (fn [x] `(. clojure.lang.Numbers (unchecked_inc ~x)))
:added "1.0"}
[x] (. clojure.lang.Numbers (unchecked_long_inc x)))
[x] (. clojure.lang.Numbers (unchecked_inc x)))

(defn unchecked-dec-int
"Returns a number one less than x, an int.
Expand All @@ -1080,12 +1080,12 @@
:added "1.0"}
[x] (. clojure.lang.Numbers (unchecked_int_dec x)))

(defn unchecked-dec-long
(defn unchecked-dec
"Returns a number one less than x, a long.
Note - uses a primitive operator subject to overflow."
{:inline (fn [x] `(. clojure.lang.Numbers (unchecked_long_dec ~x)))
{:inline (fn [x] `(. clojure.lang.Numbers (unchecked_dec ~x)))
:added "1.0"}
[x] (. clojure.lang.Numbers (unchecked_long_dec x)))
[x] (. clojure.lang.Numbers (unchecked_dec x)))

(defn unchecked-negate-int
"Returns the negation of x, an int.
Expand All @@ -1094,12 +1094,12 @@
:added "1.0"}
[x] (. clojure.lang.Numbers (unchecked_int_negate x)))

(defn unchecked-negate-long
(defn unchecked-negate
"Returns the negation of x, a long.
Note - uses a primitive operator subject to overflow."
{:inline (fn [x] `(. clojure.lang.Numbers (unchecked_long_negate ~x)))
{:inline (fn [x] `(. clojure.lang.Numbers (unchecked_minus ~x)))
:added "1.0"}
[x] (. clojure.lang.Numbers (unchecked_long_negate x)))
[x] (. clojure.lang.Numbers (unchecked_minus x)))

(defn unchecked-add-int
"Returns the sum of x and y, both int.
Expand All @@ -1108,12 +1108,12 @@
:added "1.0"}
[x y] (. clojure.lang.Numbers (unchecked_int_add x y)))

(defn unchecked-add-long
(defn unchecked-add
"Returns the sum of x and y, both long.
Note - uses a primitive operator subject to overflow."
{:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_long_add ~x ~y)))
{:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_add ~x ~y)))
:added "1.0"}
[x y] (. clojure.lang.Numbers (unchecked_long_add x y)))
[x y] (. clojure.lang.Numbers (unchecked_add x y)))

(defn unchecked-subtract-int
"Returns the difference of x and y, both int.
Expand All @@ -1122,12 +1122,12 @@
:added "1.0"}
[x y] (. clojure.lang.Numbers (unchecked_int_subtract x y)))

(defn unchecked-subtract-long
(defn unchecked-subtract
"Returns the difference of x and y, both long.
Note - uses a primitive operator subject to overflow."
{:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_long_subtract ~x ~y)))
{:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_minus ~x ~y)))
:added "1.0"}
[x y] (. clojure.lang.Numbers (unchecked_long_subtract x y)))
[x y] (. clojure.lang.Numbers (unchecked_minus x y)))

(defn unchecked-multiply-int
"Returns the product of x and y, both int.
Expand All @@ -1136,12 +1136,12 @@
:added "1.0"}
[x y] (. clojure.lang.Numbers (unchecked_int_multiply x y)))

(defn unchecked-multiply-long
(defn unchecked-multiply
"Returns the product of x and y, both long.
Note - uses a primitive operator subject to overflow."
{:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_long_multiply ~x ~y)))
{:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_multiply ~x ~y)))
:added "1.0"}
[x y] (. clojure.lang.Numbers (unchecked_long_multiply x y)))
[x y] (. clojure.lang.Numbers (unchecked_multiply x y)))

(defn unchecked-divide-int
"Returns the division of x by y, both int.
Expand All @@ -1150,27 +1150,13 @@
:added "1.0"}
[x y] (. clojure.lang.Numbers (unchecked_int_divide x y)))

(defn unchecked-divide-long
"Returns the division of x by y, both long.
Note - uses a primitive operator subject to truncation."
{:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_long_divide ~x ~y)))
:added "1.0"}
[x y] (. clojure.lang.Numbers (unchecked_long_divide x y)))

(defn unchecked-remainder-int
"Returns the remainder of division of x by y, both int.
Note - uses a primitive operator subject to truncation."
{:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_int_remainder ~x ~y)))
:added "1.0"}
[x y] (. clojure.lang.Numbers (unchecked_int_remainder x y)))

(defn unchecked-remainder-long
"Returns the remainder of division of x by y, both long.
Note - uses a primitive operator subject to truncation."
{:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_long_remainder ~x ~y)))
:added "1.0"}
[x y] (. clojure.lang.Numbers (unchecked_long_remainder x y)))

(defn pos?
"Returns true if num is greater than zero, else false"
{
Expand All @@ -1188,14 +1174,16 @@
(defn quot
"quot[ient] of dividing numerator by denominator."
{:added "1.0"
:static true}
:static true
:inline (fn [x y] `(. clojure.lang.Numbers (quotient ~x ~y)))}
[num div]
(. clojure.lang.Numbers (quotient num div)))

(defn rem
"remainder of dividing numerator by denominator."
{:added "1.0"
:static true}
:static true
:inline (fn [x y] `(. clojure.lang.Numbers (remainder ~x ~y)))}
[num div]
(. clojure.lang.Numbers (remainder num div)))

Expand Down Expand Up @@ -2354,7 +2342,7 @@
(loop [~i 0]
(when (< ~i n#)
~@body
(recur (unchecked-inc-long ~i)))))))
(recur (unchecked-inc ~i)))))))

(defn map
"Returns a lazy sequence consisting of the result of applying f to the
Expand Down Expand Up @@ -2726,7 +2714,7 @@
needrec (steppair 0)
subform (steppair 1)
recform-chunk
`(recur ~seq- ~chunk- ~count- (unchecked-inc-long ~i-))
`(recur ~seq- ~chunk- ~count- (unchecked-inc ~i-))
steppair-chunk (step recform-chunk (nnext exprs))
subform-chunk (steppair-chunk 1)]
[true
Expand Down Expand Up @@ -2832,7 +2820,7 @@
(loop [~i 0]
(when (< ~i n#)
~@body
(recur (unchecked-inc-long ~i)))))))
(recur (unchecked-inc ~i)))))))

#_(defn into
"Returns a new coll consisting of to-coll with all of the items of
Expand Down Expand Up @@ -4073,12 +4061,12 @@
(= k :when) `(if ~v
~(do-cmod etc)
(recur
(unchecked-inc-long ~gi)))
(unchecked-inc ~gi)))
(keyword? k)
(err "Invalid 'for' keyword " k)
:else
`(do (chunk-append ~gb ~body-expr)
(recur (unchecked-inc-long ~gi)))))]
(recur (unchecked-inc ~gi)))))]
`(fn ~giter [~gxs]
(lazy-seq
(loop [~gxs ~gxs]
Expand Down Expand Up @@ -4583,7 +4571,7 @@
(if (< ~idx (alength a#))
(do
(aset ~ret ~idx ~expr)
(recur (unchecked-inc-long ~idx)))
(recur (unchecked-inc ~idx)))
~ret))))

(defmacro areduce
Expand All @@ -4595,7 +4583,7 @@
`(let [a# ~a]
(loop [~idx 0 ~ret ~init]
(if (< ~idx (alength a#))
(recur (unchecked-inc-long ~idx) ~expr)
(recur (unchecked-inc ~idx) ~expr)
~ret))))

(defn float-array
Expand Down
104 changes: 71 additions & 33 deletions src/jvm/clojure/lang/Numbers.java
Expand Up @@ -185,21 +185,24 @@ static public Number divide(Object x, Object y){
return ops(x).combine(yops).divide((Number)x, (Number)y);
}

static public Number quotient(Number x, Number y){
static public Number quotient(Object x, Object y){
Ops yops = ops(y);
if(yops.isZero(y))
if(yops.isZero((Number) y))
throw new ArithmeticException("Divide by zero");
return ops(x).combine(yops).quotient(x, y);
return ops(x).combine(yops).quotient((Number)x, (Number)y);
}

static public Number remainder(Number x, Number y){
static public Number remainder(Object x, Object y){
Ops yops = ops(y);
if(yops.isZero(y))
if(yops.isZero((Number) y))
throw new ArithmeticException("Divide by zero");
return ops(x).combine(yops).remainder(x, y);
return ops(x).combine(yops).remainder((Number)x, (Number)y);
}

static double quotient(double n, double d){
static public double quotient(double n, double d){
if(d == 0)
throw new ArithmeticException("Divide by zero");

double q = n / d;
if(q <= Long.MAX_VALUE && q >= Long.MIN_VALUE)
{
Expand All @@ -211,7 +214,10 @@ static double quotient(double n, double d){
}
}

static double remainder(double n, double d){
static public double remainder(double n, double d){
if(d == 0)
throw new ArithmeticException("Divide by zero");

double q = n / d;
if(q <= Long.MAX_VALUE && q >= Long.MIN_VALUE)
{
Expand Down Expand Up @@ -1717,29 +1723,61 @@ static public Number num(long x){
return Long.valueOf(x);
}

static public long unchecked_long_add(long x, long y){
return x + y;
}

static public long unchecked_long_subtract(long x, long y){
return x - y;
}

static public long unchecked_long_negate(long x){
return -x;
}

static public long unchecked_long_inc(long x){
return x + 1;
}

static public long unchecked_long_dec(long x){
return x - 1;
}

static public long unchecked_long_multiply(long x, long y){
return x * y;
}
static public long unchecked_add(long x, long y){return x + y;}
static public long unchecked_minus(long x, long y){return x - y;}
static public long unchecked_multiply(long x, long y){return x * y;}
static public long unchecked_minus(long x){return -x;}
static public long unchecked_inc(long x){return x + 1;}
static public long unchecked_dec(long x){return x - 1;}

static public Number unchecked_add(Object x, Object y){return add(x,y);}
static public Number unchecked_minus(Object x, Object y){return minus(x,y);}
static public Number unchecked_multiply(Object x, Object y){return multiply(x,y);}
static public Number unchecked_minus(Object x){return minus(x);}
static public Number unchecked_inc(Object x){return inc(x);}
static public Number unchecked_dec(Object x){return dec(x);}

static public double unchecked_add(double x, double y){return add(x,y);}
static public double unchecked_minus(double x, double y){return minus(x,y);}
static public double unchecked_multiply(double x, double y){return multiply(x,y);}
static public double unchecked_minus(double x){return minus(x);}
static public double unchecked_inc(double x){return inc(x);}
static public double unchecked_dec(double x){return dec(x);}

static public double unchecked_add(double x, Object y){return add(x,y);}
static public double unchecked_minus(double x, Object y){return minus(x,y);}
static public double unchecked_multiply(double x, Object y){return multiply(x,y);}
static public double unchecked_add(Object x, double y){return add(x,y);}
static public double unchecked_minus(Object x, double y){return minus(x,y);}
static public double unchecked_multiply(Object x, double y){return multiply(x,y);}

static public double unchecked_add(double x, long y){return add(x,y);}
static public double unchecked_minus(double x, long y){return minus(x,y);}
static public double unchecked_multiply(double x, long y){return multiply(x,y);}
static public double unchecked_add(long x, double y){return add(x,y);}
static public double unchecked_minus(long x, double y){return minus(x,y);}
static public double unchecked_multiply(long x, double y){return multiply(x,y);}

static public Number unchecked_add(long x, Object y){return add(x,y);}
static public Number unchecked_minus(long x, Object y){return minus(x,y);}
static public Number unchecked_multiply(long x, Object y){return multiply(x,y);}
static public Number unchecked_add(Object x, long y){return add(x,y);}
static public Number unchecked_minus(Object x, long y){return minus(x,y);}
static public Number unchecked_multiply(Object x, long y){return multiply(x,y);}

static public Number quotient(double x, Object y){return quotient((Object)x,y);}
static public Number quotient(Object x, double y){return quotient(x,(Object)y);}
static public Number quotient(long x, Object y){return quotient((Object)x,y);}
static public Number quotient(Object x, long y){return quotient(x,(Object)y);}
static public double quotient(double x, long y){return quotient(x,(double)y);}
static public double quotient(long x, double y){return quotient((double)x,y);}

static public Number remainder(double x, Object y){return remainder((Object)x,y);}
static public Number remainder(Object x, double y){return remainder(x,(Object)y);}
static public Number remainder(long x, Object y){return remainder((Object)x,y);}
static public Number remainder(Object x, long y){return remainder(x,(Object)y);}
static public double remainder(double x, long y){return remainder(x,(double)y);}
static public double remainder(long x, double y){return remainder((double)x,y);}

static public long add(long x, long y){
long ret = x + y;
Expand Down Expand Up @@ -1820,11 +1858,11 @@ static public Number multiplyP(long x, long y){
return num(ret);
}

static public long unchecked_long_divide(long x, long y){
static public long quotient(long x, long y){
return x / y;
}

static public long unchecked_long_remainder(long x, long y){
static public long remainder(long x, long y){
return x % y;
}

Expand Down

0 comments on commit 3e644c7

Please sign in to comment.