fast code and no morals

wendt at megaron.UUCP wendt at megaron.UUCP
Wed Jan 22 10:22:29 AEST 1986


Here's a version of calloc that breaks every rule of "structured"
programming ever invented -- and runs about twice as fast as a
vanilla version:

/* calloc - allocate and clear memory block */
#define CHARPERINT (sizeof(int)/sizeof(char))
#define NULL 0

char *calloc(num, size)
unsigned num, size;
    {
    register char *mp;
    char *malloc();
    register int *q, *qlim, m;

    num *= size;
    mp = malloc(num);
    if (mp == NULL) return (NULL);
    q = (int *) mp;
    qlim = (m = (num+CHARPERINT-1)/CHARPERINT) + (q = (int *)mp);

    switch (m & 7)
        do      {
                    *q++ = 0;
            case 7: *q++ = 0;
            case 6: *q++ = 0;
            case 5: *q++ = 0;
            case 4: *q++ = 0;
            case 3: *q++ = 0;
            case 2: *q++ = 0;
            case 1: *q++ = 0;
            case 0: ;
            } while (q < qlim);

    return (mp);
    }

.align  1
.globl  _calloc
.set    L32,0xf00
.data
.text
_calloc:.word   L32
mull2   8(ap),4(ap)
pushl   4(ap)
calls   $1,_malloc
movl    r0,r11
jneq    L37
clrl    r0
ret
L37:movl        r11,r10
pushl   $4
addl3   $3,4(ap),-(sp)
calls   $2,udiv
movl    r0,r8
ashl    $2,r0,r0
movl    r11,r10
addl3   r10,r0,r9
extzv   $0,$3,r8,r0
casel   r0,$0,$7
L2000000:
.word   L41-L2000000
.word   L49-L2000000
.word   L48-L2000000
.word   L47-L2000000
.word   L46-L2000000
.word   L45-L2000000
.word   L44-L2000000
.word   L43-L2000000
L38:movl        r11,r0
ret
L42:clrl        (r10)+
L43:clrl        (r10)+
L44:clrl        (r10)+
L45:clrl        (r10)+
L46:clrl        (r10)+
L47:clrl        (r10)+
L48:clrl        (r10)+
L49:clrl        (r10)+
L41:cmpl        r10,r9
jlss    L42
jbr     L38



More information about the Comp.lang.c mailing list