freenode/#lisp - IRC Chatlog
Search
16:20:54
kinope
Hi! Quick question. Are Common Lisp's bitwise operations like 'logand' considered as performant as their C/C++ counterparts? I've seen this used as an optimisation where a bitmask that is a power of two is used to do modular arithmetic.
16:22:36
phoe
which means that you need to do a proper LDB or MOD for the compiler to notice "oh, I can optimize it into a single CPU instruction on a machine word"
16:26:17
pjb
kinope: if you want to learn something, try to write in C: (defun f (x) (if (< x 0) 1 (* x (f (- x 1))))) (defun main () (print (f 1000)))
16:26:41
pjb
kinope: then you can try to compare the performance of your C program and of this CL program.
16:32:17
_death
rumbler31_: I use a repl-specific pprint dispatch table for pervasive hex printing.. see https://github.com/death/slime/commit/fedcefab7e8378ea90979ff3697056dea092ae0a
16:35:05
kinope
ralt: unfortunately for me ECL doesn't do dissasemble, but I did just have a look at the source and it looks like logand is directly translated to C's bitwise and '&'. I can't find where mod is implemented just now though.
16:38:12
jackdaniel
the function mod is implemented in src/c/num_co.d, but the compiler may opencode it into something more efficient if it i.e knows that arguments are fixnums
16:38:17
kinope
pjb: I couldn't tell you about that operation on 10000 bits, Bit twiddling is not my forte
16:39:13
jackdaniel
regarding bitwise operations, their optimizations are in src/cmp/cmpopt-bits.lsp
16:44:11
pjb
kinope: (defun make-lots-of-bits (n) (map-into (make-array n :element-type 'bit) (lambda () (random 2)))) (let* ((n 10000) (a (make-lots-of-bits n)) (b (make-lots-of-bits n)) (c (bit-and a b))) (print a) (print b) (print c))
16:44:25
kinope
jackdaniel: I'm looking at an implementation of a lock-free data structure that I think is bit twiddling for the added performance. I'm just trying to figure out if i can get away with using just the standard 'mod' to the same effect.
16:44:55
_death
kinope: a tail recursive one would have something like (f (- x 1) (* x acc)) in the "induction step", i.e. calling itself would be the last operation
16:44:58
pjb
kinope: or: (defun make-int-with-lots-of-bits (n) (random (expt 2 n))) (let* ((n 10000) (a (make-int-with-lots-of-bits n)) (b (make-int-with-lots-of-bits n)) (c (logand a b))) (print a) (print b) (print c))
16:46:53
jackdaniel
it was a question of uneducated folk, but I don't think that confusing him more would make him ask better questions
16:47:42
phoe
pjb: answering a question with an equally idiotic answer doesn't help anyone, even if the original question is, in your opinion, idiot.s
16:48:57
pjb
phoe: the original questionw as a comparison of C and CL on logand. There is no more relevant than to write two programs, in C and in CL using logand.
16:48:58
kinope
I may give that a shot later pjb, but right now its 3am in the morning and I'm not set up for C development on my phone, haha.
16:50:13
phoe
pjb: the original question was about using modular arithmetic and its performance, since that's all what C and C++ are capable of unless one uses a bignum library, and your answer was about bignums, which do *not* use modular arithmetic, and therefore your bignum answer did not apply to it.
16:51:15
pjb
phoe: most modular arithemetic is used in cryptography, on numbers bigger than a long long.
18:06:39
_death
it's useful if you want to program FORTRAN in Lisp.. though there are better options like prog and &aux
18:12:12
enedil
I mean, I would try to understand id better, but now I'm writing for code to pass my class, due to covid we had "remote classes" - consultations by email, so now, I'm trying to make some code that works. My go-to functional language is OCaml and it wouldn't like this kind of solution too
20:39:02
pjb
dlowe: I prefer to use let rather than setf: (cond ((let ((result (compute-result))) (do-something-with result) result)) …) ; not the empty body.