duff.c
author Markus Bröker <mbroeker@volpe.spectre.org>
Wed, 18 Aug 2010 16:15:18 +0200
changeset 141 0b5befeb361e
parent 63 5a82f89d607e
permissions -rw-r--r--
getbits: calculates the proper value but showed a different one
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
 * Duff's Device
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
 * fast copy algorithm
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 * performs 8 times faster on long strings.
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
 */
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdio.h>
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <stdlib.h>
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <string.h>
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
63
5a82f89d607e uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 57
diff changeset
    11
char *duffcopy (char *to, char *from, size_t len)
56
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
{
63
5a82f89d607e uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 57
diff changeset
    13
    size_t n, pos = 0;
56
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
57
7739518acafb duff.c:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 56
diff changeset
    15
    n = (len + 7) / 8;
56
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
57
7739518acafb duff.c:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 56
diff changeset
    17
    switch (len % 8) {
56
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
    case 0:
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
        do {
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
            to[pos] = from[pos];
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
            pos++;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
    case 7:
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
            to[pos] = from[pos];
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
            pos++;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
    case 6:
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
            to[pos] = from[pos];
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
            pos++;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
    case 5:
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
            to[pos] = from[pos];
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
            pos++;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    case 4:
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
            to[pos] = from[pos];
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
            pos++;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
    case 3:
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
            to[pos] = from[pos];
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
            pos++;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
    case 2:
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
            to[pos] = from[pos];
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
            pos++;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
    case 1:
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
            to[pos] = from[pos];
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
            pos++;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
        } while (--n > 0);
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
    }
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
    return to;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
}
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
int main (int argc, char **argv)
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
{
57
7739518acafb duff.c:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 56
diff changeset
    51
    char *to, *from;
63
5a82f89d607e uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 57
diff changeset
    52
    size_t len;
56
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
    if (argc != 2) {
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
        printf ("Usage: %s <string>\n", argv[0]);
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
        return EXIT_FAILURE;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
    }
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
    from = argv[1];
57
7739518acafb duff.c:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 56
diff changeset
    60
    len = strlen (from) + 1;
56
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
57
7739518acafb duff.c:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 56
diff changeset
    62
    if ((to = malloc (len)) == NULL) {
7739518acafb duff.c:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 56
diff changeset
    63
        perror ("MALLOC");
7739518acafb duff.c:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 56
diff changeset
    64
        return EXIT_FAILURE;
7739518acafb duff.c:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 56
diff changeset
    65
    }
7739518acafb duff.c:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 56
diff changeset
    66
7739518acafb duff.c:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 56
diff changeset
    67
    printf ("DUFF: %s\n", duffcopy (to, from, len));
7739518acafb duff.c:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 56
diff changeset
    68
7739518acafb duff.c:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 56
diff changeset
    69
    if (to != NULL) {
7739518acafb duff.c:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 56
diff changeset
    70
        free (to);
7739518acafb duff.c:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 56
diff changeset
    71
    }
7739518acafb duff.c:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 56
diff changeset
    72
56
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    73
    return EXIT_SUCCESS;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    74
}