1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /* Copyright (c) 1988 AT&T */
22 /* All Rights Reserved */
23 /*
24 * Copyright 1994 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
26 */
27 /*
28 * Copyright 1987, 2006-2020 J. Schilling
29 *
30 * @(#)zero.c 1.8 20/09/06 J. Schilling
31 */
32 #if defined(sun)
33 #pragma ident "@(#)zero.c 1.8 20/09/06 J. Schilling"
34 #endif
35 /*
36 * @(#)zero.c 1.3 06/12/12
37 */
38
39 #if defined(sun)
40 #pragma ident "@(#)zero.c"
41 #pragma ident "@(#)sccs:lib/mpwlib/zero.c"
42 #endif
43 /*
44 Zero `cnt' bytes starting at the address `ptr'.
45 Return `ptr'.
46 */
47
48 #include <defines.h>
49
50 #ifdef pdp11
51
zero(p,n)52 char *zero(p,n)
53 register char *p;
54 register int n;
55 {
56 char *op = p;
57 while (--n >= 0)
58 *p++ = 0;
59 return(op);
60 }
61
62 #else /* !pdp11 */
63
64 #include <schily/align.h>
65
66 #define DO8(a) a; a; a; a; a; a; a; a;
67
68 /*
69 * zero(to, cnt)
70 */
71 EXPORT char *
zero(to,cnt)72 zero(to, cnt)
73 register char *to;
74 int cnt;
75 {
76 char *oto = to;
77 register ssize_t n;
78 register long lval = 0L;
79
80 /*
81 * If we change cnt to be unsigned, check for == instead of <=
82 */
83 if ((n = cnt) <= 0)
84 return (to);
85
86 if (n < 8 * sizeof (long)) { /* Simple may be faster... */
87 do { /* n is always > 0 */
88 *to++ = '\0';
89 } while (--n > 0);
90 return (oto);
91 }
92
93 /*
94 * Assign byte-wise until properly aligned for a long pointer.
95 */
96 while (--n >= 0 && !laligned(to)) {
97 *to++ = '\0';
98 }
99 n++;
100
101 if (n >= (ssize_t)(8 * sizeof (long))) {
102 register ssize_t rem = n % (8 * sizeof (long));
103
104 n /= (8 * sizeof (long));
105 {
106 register long *tol = (long *)to;
107
108 do {
109 DO8 (*tol++ = lval);
110 } while (--n > 0);
111
112 to = (char *)tol;
113 }
114 n = rem;
115
116 if (n >= 8) {
117 n -= 8;
118 do {
119 DO8 (*to++ = '\0');
120 } while ((n -= 8) >= 0);
121 n += 8;
122 }
123 if (n > 0) do {
124 *to++ = '\0';
125 } while (--n > 0);
126 return (oto);
127 }
128 if (n > 0) do {
129 *to++ = '\0';
130 } while (--n > 0);
131 return (oto);
132 }
133
134 #endif /* !pdp11 */
135