Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

simple riddle about macro

Name: Anonymous 2011-05-18 16:01

Bellow is a macro from Perl source code (perl.h).

# define _swab_32_(x) ((U32)( \
         (((U32)(x) & U32_CONST(0x000000ff)) << 24) | \
         (((U32)(x) & U32_CONST(0x0000ff00)) <<  8) | \
         (((U32)(x) & U32_CONST(0x00ff0000)) >>  8) | \
         (((U32)(x) & U32_CONST(0xff000000)) >> 24) ))


Provide at least 3 ideas, how this may be reused in other projects.

Name: Anonymous 2011-05-18 16:12

SWAB MY ANUS

Name: Anonymous 2011-05-18 16:13

>>1
It could serve as an example how ugly C++ is, and to justify usage of LISP for all other projects.

Name: Anonymous 2011-05-18 16:16

>>3
fuck off, lithper faggot. nobody likes you.

Name: Anonymous 2011-05-18 16:17

>>4
stop butthurting.

Name: Anonymous 2011-05-18 16:20

>>5
fuck off and die, lithpfag

Name: Anonymous 2011-05-18 16:28

>>3
It's C, not C++

Name: Anonymous 2011-05-18 16:37

>>7
all the same shit.

Name: Anonymous 2011-05-18 17:07

#define _swab_32_ __builtin_bswap32

Name: Anonymous 2011-05-18 17:10

It's to change the endianess of an integer. It's quite common in all kinds of binary parsing code such as code accessing binary files, network protocols and so on.
>>3
In Lisp you still have to write similar code, even if I usually just autogenerate the code, that way it can be reused regardless of the size and shape of the data.

Name: Anonymous 2011-05-18 17:11

The Lisp weenies are awful militant lately. Used to be a lot less Lisp vs. C, and almost never an unprovoked attack against C by a Lisper. Obv. trolling I guess. It's unbecoming though.

Name: Anonymous 2011-05-18 17:15

>>10
In Lisp I'll have no need for "(U32)(x)) & U32_CONST(" and I'll autogen swab_16 swab_32 swab_64, 128, 256,...

Name: Anonymous 2011-05-18 18:15

>>12
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))

Name: Anonymous 2011-05-18 19:23

>>11
I'd say it's the opposite, due to the recent afflux of /g/ people in /prog/. Lispers just defend themselves.

But then, there are Lisp trolls too, but you can recognize them because they call Lisp ``LISP''.

Name: Anonymous 2011-05-18 19:41

>>14
http://en.wikipedia.org/wiki/Lisp_(programming_language)
The name LISP derives from "LISt Processing"

Name: Anonymous 2011-05-18 19:42

>>15
iki/Lisp_(progr
It's not 1960 anymore.

Name: Anonymous 2011-05-18 19:46

>>16
so?

Name: Anonymous 2011-05-18 19:47

>>15
So the correct name is LisP.

Name: Anonymous 2011-05-18 19:49

>>17
So get the fuck out we don't like you I hope you die of testicular cancer you're the reason Lisp is hated by the mass you cretin smug weenie I hope you'll get anally raped and get AIDS.

Name: Anonymous 2011-05-18 19:49

>>15
That name died out in the 1960's. You don't happen to be using PDP-10 machines do you? Do you currently refer to PC's as microcomputers? No? Then you don't use LISP until you return to 1960.

Name: Anonymous 2011-05-18 22:26

>>20
LISP (all caps in bold, preferable in Closter Black) makes the name stand for its own. Being a marketing move, it also shows timeless power and stability of this 50-year old language. The best of all: you cant do the same with your Python or Ruby. Too bad for you and your inferior lowercase languages.

Name: Anonymous 2011-05-18 22:31

>>21
0/10

Trolling used to be an art.

Name: Anonymous 2011-05-18 23:03

COBOL (all caps in bold, preferabele Closter Black) makes the name stand for its own. Being a marketing move, it also shows timeless power and stability of this 50-year old language. The best of all: you cant do the same with your Python or Ruby. Too bad for you and your inferior lowercase languages.

Name: Anonymous 2011-05-18 23:05

>>23
You can't really compare the two.

Name: Anonymous 2011-05-18 23:32

>>24
Both are all caps and 50 years old.

Name: Anonymous 2011-05-18 23:38

>>25
at least cobol is much faster than ruby

Name: Autistic Duck 2011-05-18 23:41

FORTRAN ALGOL APL BASIC (all caps in bold, preferabele Comic Sans)

Name: Anonymous 2011-05-19 6:42

>>10, 12
So, let's see some auto generated code, then.

Name: Anonymous 2011-05-19 9:29

>>28
If you insist. I didn't have any handy code which does actual byteswapping as the data was read in in the proper endianess in the first place without requiring further swapping, but as the task itself is easy, I wrote this code to illustrate how to implement such a thing:

;;; Utilities

(defmacro with-gensyms ((&rest names) &body body)
  `(let ,(loop for name in names collect `(,name (gensym ,(string name))))
     ,@body))

(eval-when (:compile-toplevel :load-toplevel :execute)
  (defun symbolicate (syms &optional (package *package*))
    (intern (apply #'concatenate 'string (mapcar #'string syms)) package)))

;;; Implementation

(defmacro define-byte-swapper
    (name &key (byte-count 4) (byte-size 8))  
  (with-gensyms (int)
    `(progn
       (declaim (inline ,name))
       (defun ,name (,int)
         (logior
          ,@(loop for i from 0 below byte-count collect                   
                  `(ash
                    (logand
                     ,(ash (1- (ash 1 byte-size)) (* i byte-size)) ,int)
                    ,(* byte-size (- (1- byte-count) (* i 2))))))))))

(defmacro define-byte-swappers (&key (up-to-powers-of-2-bytes 5)
                                  (byte-size 8))
  `(progn ,@(loop for i from 1 to up-to-powers-of-2-bytes collect
                  `(define-byte-swapper
                       ,(symbolicate
                         `(bswap ,(format nil "~D" (* byte-size (ash 1 i)))))
                       :byte-count ,(ash 1 i)))))

;; defines bswap16 to bswap256
(define-byte-swappers)


;;; Test

(setf *print-base* 16 *print-radix* t)
(values (bswap16 #x1122) (bswap32 #x11223344) (bswap64 #x1122334455667788) (bswap128 #x1122334455667788AABBCCDDEEFF1234) (bswap256 #x1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF)) ; =>
#x2211
#x44332211
#x8877665544332211
#x3412FFEEDDCCBBAA8877665544332211
#xEFCDAB9078563412EFCDAB9078563412EFCDAB9078563412EFCDAB9078563412


If this were not meant to be an example, some of those defmacro's would be written as macrolet's and the inner defun may be written as a compiler macro or as a macro (if support for old implementation which can't inline is to be considered), but the difference in the implementation is too small to be worth showing, so only this inlining version is shown.

Name: >>29 2011-05-19 9:41

Also to show some alternate uses of the same code. Let's say you don't have 8bit bytes, or you want to reverse a string of arbitrary bits, you can do it just as well using this:

CL-USER> (setf *print-base* 2 *print-radix* t)
T
CL-USER> (define-byte-swapper bswap8bits :byte-count 8 :byte-size 1)
BSWAP8BITS
CL-USER> (bswap8bits #b11001010)
#b1010011

It truly shows that you only have to write code once and use anywhere. The exception to this rule is when you can find a much better way to optimize a specific case and you need that speed.

Name: Anonymous 2011-05-19 9:49

The teams of more humble craftspeople I've worked with have been compellingly more productive. I find it a joy to hear "I'm not really that great a programmer, I was a History major -- but let me show you the way that we combined Visitor and Strategy in our last project, I really liked how it came out" ... and onto my whiteboard flows an elegant, terse work of beauty. I respond to the humility, to the shift from "I" to "we", and to the obvious appreciation for the abstraction itself.

Name: Anonymous 2011-05-19 9:49

>>29-30
For how much I love your clean coding style, your CL knowledge and your BBCode-Lisp-Beautifier, I must say that YHBT.

Name: Anonymous 2011-05-19 9:51

>>29
>(declaim (inline ,name))
AFAIK, SBCL does this automatically.

Name: Anonymous 2011-05-19 9:53

>>31
OK, I think we're violently agreeing here. One of the best programmers (as in useful to the organization) I've worked with recently was a convert from Human Resources, and fits the pattern you describe.

Name: Anonymous 2011-05-19 15:51

>>29

(             -        ((&          ) &         )
  `(    ,(                               `(,     (       ,(           ))))
     ,@    ))

(    -     (:       -         :    -         :       )
  (                  (     &         (        *       *))
    (       (      #'            '       (       #'           ))        )))

;;;              

(               -    -      
    (     &    (    -       ) (    -      )) 
  (    -        (   )
    `(    
       (        (       ,    ))
       (      ,     (,   )
         (     
          ,@(                            -                               
                  `(  
                    (     
                     ,(    ( - (          -    )) (*       -    )) ,   )
                    ,(*     -     (- ( -     -     ) (*    ))))))))))

(               -    -         (&    (  -  -      -  - -       )
                                  (    -      ))
  `(      ,@(                       -  -      -  - -            
                  `(      -    -      
                       ,(          
                         `(      ,(           "~ " (*     -     (       )))))
                       :    -      ,(       )))))

;;                           
(      -    -        )

Oatmeal with toenail clippings.

Name: Anonymous 2011-05-20 17:19

>>35
You only notice the things that my mind filters out. Enjoy having to manually indent when my editor can do it for me.

Name: Anonymous 2011-05-20 18:34

>>36
I never knew /prog/ was an abbreviation for BULLSHIT

Don't change these.
Name: Email:
Entire Thread Thread List