]>
Commit | Line | Data |
---|---|---|
b0d623f7 A |
1 | /* |
2 | * Copyright (c) 2008 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
25 | * | |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
27 | */ | |
28 | /* adler32.c -- compute the Adler-32 checksum of a data stream | |
29 | * Copyright (C) 1995-2004 Mark Adler | |
30 | * For conditions of distribution and use, see copyright notice in zlib.h | |
31 | */ | |
32 | ||
33 | /* @(#) $Id$ */ | |
34 | ||
b7266188 A |
35 | #include <stdint.h> // For uintptr_t. |
36 | ||
37 | ||
b0d623f7 A |
38 | #define ZLIB_INTERNAL |
39 | #if KERNEL | |
40 | #include <libkern/zlib.h> | |
41 | #else | |
42 | #include "zlib.h" | |
43 | #endif /* KERNEL */ | |
44 | ||
b7266188 A |
45 | #if defined _ARM_ARCH_6 |
46 | extern uLong adler32_vec(uLong adler, uLong sum2, const Bytef *buf, uInt len); | |
47 | #endif | |
48 | ||
b0d623f7 A |
49 | #define BASE 65521UL /* largest prime smaller than 65536 */ |
50 | #define NMAX 5552 | |
51 | /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ | |
52 | ||
53 | #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;} | |
54 | #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1); | |
55 | #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2); | |
56 | #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); | |
57 | #define DO16(buf) DO8(buf,0); DO8(buf,8); | |
58 | ||
59 | /* use NO_DIVIDE if your processor does not do division in hardware */ | |
60 | #ifdef NO_DIVIDE | |
61 | # define MOD(a) \ | |
62 | do { \ | |
63 | if (a >= (BASE << 16)) a -= (BASE << 16); \ | |
64 | if (a >= (BASE << 15)) a -= (BASE << 15); \ | |
65 | if (a >= (BASE << 14)) a -= (BASE << 14); \ | |
66 | if (a >= (BASE << 13)) a -= (BASE << 13); \ | |
67 | if (a >= (BASE << 12)) a -= (BASE << 12); \ | |
68 | if (a >= (BASE << 11)) a -= (BASE << 11); \ | |
69 | if (a >= (BASE << 10)) a -= (BASE << 10); \ | |
70 | if (a >= (BASE << 9)) a -= (BASE << 9); \ | |
71 | if (a >= (BASE << 8)) a -= (BASE << 8); \ | |
72 | if (a >= (BASE << 7)) a -= (BASE << 7); \ | |
73 | if (a >= (BASE << 6)) a -= (BASE << 6); \ | |
74 | if (a >= (BASE << 5)) a -= (BASE << 5); \ | |
75 | if (a >= (BASE << 4)) a -= (BASE << 4); \ | |
76 | if (a >= (BASE << 3)) a -= (BASE << 3); \ | |
77 | if (a >= (BASE << 2)) a -= (BASE << 2); \ | |
78 | if (a >= (BASE << 1)) a -= (BASE << 1); \ | |
79 | if (a >= BASE) a -= BASE; \ | |
80 | } while (0) | |
81 | # define MOD4(a) \ | |
82 | do { \ | |
83 | if (a >= (BASE << 4)) a -= (BASE << 4); \ | |
84 | if (a >= (BASE << 3)) a -= (BASE << 3); \ | |
85 | if (a >= (BASE << 2)) a -= (BASE << 2); \ | |
86 | if (a >= (BASE << 1)) a -= (BASE << 1); \ | |
87 | if (a >= BASE) a -= BASE; \ | |
88 | } while (0) | |
89 | #else | |
90 | # define MOD(a) a %= BASE | |
91 | # define MOD4(a) a %= BASE | |
92 | #endif | |
93 | ||
94 | /* ========================================================================= */ | |
95 | uLong ZEXPORT adler32(adler, buf, len) | |
96 | uLong adler; | |
97 | const Bytef *buf; | |
98 | uInt len; | |
99 | { | |
100 | unsigned long sum2; | |
b7266188 | 101 | #if !defined _ARM_ARCH_6 |
b0d623f7 | 102 | unsigned n; |
b7266188 | 103 | #endif |
b0d623f7 A |
104 | |
105 | /* split Adler-32 into component sums */ | |
106 | sum2 = (adler >> 16) & 0xffff; | |
107 | adler &= 0xffff; | |
108 | ||
109 | /* in case user likes doing a byte at a time, keep it fast */ | |
110 | if (len == 1) { | |
111 | adler += buf[0]; | |
112 | if (adler >= BASE) | |
113 | adler -= BASE; | |
114 | sum2 += adler; | |
115 | if (sum2 >= BASE) | |
116 | sum2 -= BASE; | |
117 | return adler | (sum2 << 16); | |
118 | } | |
119 | ||
120 | /* initial Adler-32 value (deferred check for len == 1 speed) */ | |
121 | if (buf == Z_NULL) | |
122 | return 1L; | |
123 | ||
124 | /* in case short lengths are provided, keep it somewhat fast */ | |
125 | if (len < 16) { | |
126 | while (len--) { | |
127 | adler += *buf++; | |
128 | sum2 += adler; | |
129 | } | |
130 | if (adler >= BASE) | |
131 | adler -= BASE; | |
132 | MOD4(sum2); /* only added so many BASE's */ | |
133 | return adler | (sum2 << 16); | |
134 | } | |
135 | ||
b7266188 A |
136 | #if defined _ARM_ARCH_6 |
137 | /* align buf to 16-byte boundary */ | |
138 | while (((uintptr_t)buf)&15) { /* not on a 16-byte boundary */ | |
139 | len--; | |
140 | adler += *buf++; | |
141 | sum2 += adler; | |
142 | if (adler >= BASE) adler -= BASE; | |
143 | MOD4(sum2); /* only added so many BASE's */ | |
144 | } | |
145 | ||
146 | return adler32_vec(adler, sum2, buf, len); // armv7 neon vectorized implementation | |
147 | ||
148 | #else // _ARM_ARCH_6 | |
149 | ||
b0d623f7 A |
150 | /* do length NMAX blocks -- requires just one modulo operation */ |
151 | while (len >= NMAX) { | |
152 | len -= NMAX; | |
153 | n = NMAX / 16; /* NMAX is divisible by 16 */ | |
154 | do { | |
155 | DO16(buf); /* 16 sums unrolled */ | |
156 | buf += 16; | |
157 | } while (--n); | |
158 | MOD(adler); | |
159 | MOD(sum2); | |
160 | } | |
161 | ||
162 | /* do remaining bytes (less than NMAX, still just one modulo) */ | |
163 | if (len) { /* avoid modulos if none remaining */ | |
164 | while (len >= 16) { | |
165 | len -= 16; | |
166 | DO16(buf); | |
167 | buf += 16; | |
168 | } | |
169 | while (len--) { | |
170 | adler += *buf++; | |
171 | sum2 += adler; | |
172 | } | |
173 | MOD(adler); | |
174 | MOD(sum2); | |
175 | } | |
176 | ||
177 | /* return recombined sums */ | |
178 | return adler | (sum2 << 16); | |
b7266188 A |
179 | |
180 | #endif // _ARM_ARCH_6 | |
b0d623f7 A |
181 | } |
182 | ||
183 | /* ========================================================================= */ | |
184 | uLong ZEXPORT adler32_combine(adler1, adler2, len2) | |
185 | uLong adler1; | |
186 | uLong adler2; | |
187 | z_off_t len2; | |
188 | { | |
189 | unsigned long sum1; | |
190 | unsigned long sum2; | |
191 | unsigned rem; | |
192 | ||
193 | /* the derivation of this formula is left as an exercise for the reader */ | |
194 | rem = (unsigned)(len2 % BASE); | |
195 | sum1 = adler1 & 0xffff; | |
196 | sum2 = rem * sum1; | |
197 | MOD(sum2); | |
198 | sum1 += (adler2 & 0xffff) + BASE - 1; | |
199 | sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem; | |
200 | if (sum1 > BASE) sum1 -= BASE; | |
201 | if (sum1 > BASE) sum1 -= BASE; | |
202 | if (sum2 > (BASE << 1)) sum2 -= (BASE << 1); | |
203 | if (sum2 > BASE) sum2 -= BASE; | |
204 | return sum1 | (sum2 << 16); | |
205 | } |