duff.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:13 +0200
changeset 56 966ad681f25d
child 57 7739518acafb
permissions -rw-r--r--
Short Description * duff.c Duffs Device, a fast copy algorithm * crypt.c the Salt was to short. * files.c massive directory creation * fork.c a small fork demo committer: Markus Bröker <mbroeker@largo.homelinux.org>
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
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
char *duffcopy (char *to, char *from, int num)
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
{
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
    int n, pos = 0;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
    n = (num + 7) / 8;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
    switch (num % 8) {
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
{
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
    char to[80], *from;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
    if (argc != 2) {
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
        printf ("Usage: %s <string>\n", argv[0]);
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
        return EXIT_FAILURE;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
    }
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
    from = argv[1];
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
    printf ("DUFF: %s\n", duffcopy (to, from, strlen (from)));
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
    return EXIT_SUCCESS;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
}