~ubuntu-branches/ubuntu/utopic/cdrdao/utopic

« back to all changes in this revision

Viewing changes to scsilib/libschily/cmpnullbytes.c

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Suffield
  • Date: 2004-06-24 22:33:16 UTC
  • Revision ID: james.westby@ubuntu.com-20040624223316-534onzugaeeyq61j
Tags: upstream-1.1.9
ImportĀ upstreamĀ versionĀ 1.1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* @(#)cmpnullbytes.c   1.2 03/06/15 Copyright 1988,2002-2003 J. Schilling */
 
2
#ifndef lint
 
3
static  char sccsid[] =
 
4
        "@(#)cmpnullbytes.c     1.2 03/06/15 Copyright 1988,2002-2003 J. Schilling";
 
5
#endif  /* lint */
 
6
/*
 
7
 *      compare data against null
 
8
 *
 
9
 *      Copyright (c) 1988,2002-2003 J. Schilling
 
10
 */
 
11
/*
 
12
 * This program is free software; you can redistribute it and/or modify
 
13
 * it under the terms of the GNU General Public License as published by
 
14
 * the Free Software Foundation; either version 2, or (at your option)
 
15
 * any later version.
 
16
 *
 
17
 * This program is distributed in the hope that it will be useful,
 
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
 * GNU General Public License for more details.
 
21
 *
 
22
 * You should have received a copy of the GNU General Public License along with
 
23
 * this program; see the file COPYING.  If not, write to the Free Software
 
24
 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
25
 */
 
26
 
 
27
#include <standard.h>
 
28
#include <align.h>
 
29
#include <schily.h>
 
30
 
 
31
#define DO8(a)  a; a; a; a; a; a; a; a;
 
32
 
 
33
EXPORT int
 
34
cmpnullbytes(fromp, cnt)
 
35
        const void      *fromp;
 
36
        int             cnt;
 
37
{
 
38
        register const char     *from   = (char *)fromp;
 
39
        register int            n;
 
40
 
 
41
        /*
 
42
         * If we change cnt to be unsigned, check for == instead of <=
 
43
         */
 
44
        if ((n = cnt) <= 0)
 
45
                return (cnt);
 
46
 
 
47
        /*
 
48
         * Compare byte-wise until properly aligned for a long pointer.
 
49
         */
 
50
        while (--n >= 0 && !laligned(from)) {
 
51
                if (*from++ != 0)
 
52
                        goto cdiff;
 
53
        }
 
54
        n++;
 
55
 
 
56
        if (n >= (int)(8 * sizeof (long))) {
 
57
                if (laligned(from)) {
 
58
                        register const long *froml = (const long *)from;
 
59
                        register int rem = n % (8 * sizeof (long));
 
60
 
 
61
                        n /= (8 * sizeof (long));
 
62
                        do {
 
63
                                DO8(
 
64
                                        if (*froml++ != 0)
 
65
                                                break;
 
66
                                );
 
67
                        } while (--n > 0);
 
68
 
 
69
                        if (n > 0) {
 
70
                                --froml;
 
71
                                from = (const char *)froml;
 
72
                                goto ldiff;
 
73
                        }
 
74
                        from = (const char *)froml;
 
75
                        n = rem;
 
76
                }
 
77
 
 
78
                if (n >= 8) {
 
79
                        n -= 8;
 
80
                        do {
 
81
                                DO8(
 
82
                                        if (*from++ != 0)
 
83
                                                goto cdiff;
 
84
                                );
 
85
                        } while ((n -= 8) >= 0);
 
86
                        n += 8;
 
87
                }
 
88
                if (n > 0) do {
 
89
                        if (*from++ != 0)
 
90
                                goto cdiff;
 
91
                } while (--n > 0);
 
92
                return (cnt);
 
93
        }
 
94
        if (n > 0) do {
 
95
                if (*from++ != 0)
 
96
                        goto cdiff;
 
97
        } while (--n > 0);
 
98
        return (cnt);
 
99
ldiff:
 
100
        n = sizeof (long);
 
101
        do {
 
102
                if (*from++ != 0)
 
103
                        goto cdiff;
 
104
        } while (--n > 0);
 
105
cdiff:
 
106
        return (--from - (char *)fromp);
 
107
}