<< Back to previous view

[CLJ-184] n-ary bit functions, also inlining of n-ary bit and math operations Created: 26/Aug/09  Updated: 01/Mar/13  Resolved: 14/May/11

Status: Closed
Project: Clojure
Component/s: None
Affects Version/s: None
Fix Version/s: Release 1.3

Type: Enhancement Priority: Major
Reporter: Assembla Importer Assignee: Alan Dipert
Resolution: Completed Votes: 0
Labels: None

Attachments: File 184-inlining-nary-math-2.diff     File 184-inlining-nary-math-3.diff     File 184-inlining-nary-math.diff     Text File inlinemath.patch    
Approval: Ok


Add an &more version to the bit operations bit-and bit-or bit-xor bit-and-not (via reduce),
Inline the n-ary version of the math ops, + - * /, inline the n-ary versions of bit-and bit-or bit-xor bit-and-not

Comment by Assembla Importer [ 24/Aug/10 6:19 AM ]

Converted from http://www.assembla.com/spaces/clojure/tickets/184
inlinemath.patch - https://www.assembla.com/spaces/clojure/documents/dlFRFQKPyr3RaoeJe5aVNr/download/dlFRFQKPyr3RaoeJe5aVNr

Comment by Assembla Importer [ 24/Aug/10 6:19 AM ]

Jonathan A Smith said: [file:dlFRFQKPyr3RaoeJe5aVNr]: inline math and bit-or patch file

Comment by Assembla Importer [ 24/Aug/10 6:19 AM ]

Jonathan A Smith said: original math defs stay, new ones get re-defined after proxy is available.

Comment by Assembla Importer [ 24/Aug/10 6:19 AM ]

mikehinchey said: Applies clean (though has extra whitespace and tabs). Existing tests still pass.

Comment by Assembla Importer [ 24/Aug/10 6:19 AM ]

Jonathan A Smith said: The defn functions are within a let, which means that they get tabbed in by Emacs ctrl+alt+Q. Is this the referred to 'extra whitespace and tabs'?

I think that this line:
>0-arities (rule-set (fn [x] (> x 1)))

might be a mistake, and perhaps should be:
>0-arities (rule-set (fn [x] (> x 0)))

But I will have to look at it and rebuild it when I get home tonight. resulting tests would still pass as the only effected function would be the single arity subtraction operation.

Comment by Assembla Importer [ 24/Aug/10 6:19 AM ]

mikehinchey said: "git am" complained about whitespace at the ends of lines. When I tab on each line to re-indent, emacs replaced tabs with spaces and deleted space at the end. Also, you could combine the 2 let blocks into 1.

Comment by Assembla Importer [ 24/Aug/10 6:19 AM ]

richhickey said: This still needs work. I'd like to avoid the need for proxy. It would be easy to make Compiler.isInline() in the compiler presume :inline-arities is an IFn (so sets literals would still work, but so would predicate fns)

Comment by Assembla Importer [ 24/Aug/10 6:19 AM ]

richhickey said: I've made that change, so isInline now treats :inline-arities as a predicate IFn. This should allow for a simpler implementation

Comment by Christopher Redinger [ 22/Apr/11 11:40 AM ]

Adding patch from Assembla

Comment by Alan Dipert [ 30/Apr/11 3:59 PM ]

inlining and n-ary bit functions and math ops

  • n-ary versions and inlines of bit-and, bit-or, bit-xor, bit-and-not
  • n-ary inlines for +, +', *, *', /, -, -'

This patch doesn't optimize / such that the & more version expands to equivalent of (/ x (reduce * y more)) rather than (reduce / (/ x y) more). The original patch had a divide-by-zero bug, and I think adding this should be another ticket.

Comment by Alan Dipert [ 30/Apr/11 4:12 PM ]

Created CLJ-785 for optimizing n-ary /

Comment by Alexander Taggart [ 03/May/11 1:46 PM ]

You'd have less copypasta using this:

(defn ^:private binary-inline
      ([x y] `(. clojure.lang.Numbers (~op ~x ~y)))
      ([x y & more]
          (fn [a b] `(. clojure.lang.Numbers (~op ~a ~b)))
          `(. clojure.lang.Numbers (~op ~x ~y)) more))))
  ([op unchecked-op]
    (if *unchecked-math*
      (binary-inline unchecked-op)
      (binary-inline op))))

(defn ^:private >1? [n] (clojure.lang.Numbers/gt n 1))

Also, ^:static doesn't do anything anymore.

The above was from the branch I made, that I mentioned in the thread on ticket status. It also included a fix for the inefficient divide.

Comment by Alan Dipert [ 03/May/11 8:50 PM ]

Thanks, I'll extract a patch for this from your branch.

Comment by Alan Dipert [ 06/May/11 12:39 PM ]

This patch incorporates Alex Taggart's work in the comments, but leaves out the change to /. It made a call to the nonexistent Numbers/unchecked_divide method and multiplies in the denominator, which is faster but has different overflow behavior than divide/reduce.

Comment by Alexander Taggart [ 07/May/11 12:30 AM ]

One problem with the patch: while the bit-ops fns have n-ary support for their inlines, the fns themselves do not. They need to be updated to something like:

(defn bit-and
  "Bitwise and"
  {:inline (nary-inline 'and)
   :inline-arities >1?
   :added "1.0"}
  ([x y] (. clojure.lang.Numbers and x y))
  ([x y & more]
    (reduce1 bit-and (bit-and x y) more)))
Comment by Alan Dipert [ 09/May/11 11:42 PM ]

Attached 184-inlining-nary-math-3.diff. Same as previous patch, but adds n-ary version of bit fns in addition to inlines, which were missing as Alex Taggart pointed out. Thanks for catching that!

Comment by Alan Dipert [ 14/May/11 4:21 PM ]

Patch is in 1.3-alpha7

Generated at Sat Oct 21 00:03:48 CDT 2017 using JIRA 4.4#649-r158309.