00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "config.h"
00025 #include "adler32.h"
00026 #include "common.h"
00027 #include "intreadwrite.h"
00028
00029 #define BASE 65521L
00030
00031 #define DO1(buf) { s1 += *buf++; s2 += s1; }
00032 #define DO4(buf) DO1(buf); DO1(buf); DO1(buf); DO1(buf);
00033 #define DO16(buf) DO4(buf); DO4(buf); DO4(buf); DO4(buf);
00034
00035 unsigned long av_adler32_update(unsigned long adler, const uint8_t * buf,
00036 unsigned int len)
00037 {
00038 unsigned long s1 = adler & 0xffff;
00039 unsigned long s2 = adler >> 16;
00040
00041 while (len > 0) {
00042 #if HAVE_FAST_64BIT && HAVE_FAST_UNALIGNED && !CONFIG_SMALL
00043 unsigned len2 = FFMIN((len-1) & ~7, 23*8);
00044 if (len2) {
00045 uint64_t a1= 0;
00046 uint64_t a2= 0;
00047 uint64_t b1= 0;
00048 uint64_t b2= 0;
00049 len -= len2;
00050 s2 += s1*len2;
00051 while (len2 >= 8) {
00052 uint64_t v = AV_RN64(buf);
00053 a2 += a1;
00054 b2 += b1;
00055 a1 += v &0x00FF00FF00FF00FF;
00056 b1 += (v>>8)&0x00FF00FF00FF00FF;
00057 len2 -= 8;
00058 buf+=8;
00059 }
00060
00061
00062
00063
00064
00065 s1 += ((a1+b1)*0x1000100010001)>>48;
00066 s2 += ((((a2&0xFFFF0000FFFF)+(b2&0xFFFF0000FFFF)+((a2>>16)&0xFFFF0000FFFF)+((b2>>16)&0xFFFF0000FFFF))*0x800000008)>>32)
00067 #if HAVE_BIGENDIAN
00068 + 2*((b1*0x1000200030004)>>48)
00069 + ((a1*0x1000100010001)>>48)
00070 + 2*((a1*0x0000100020003)>>48);
00071 #else
00072 + 2*((a1*0x4000300020001)>>48)
00073 + ((b1*0x1000100010001)>>48)
00074 + 2*((b1*0x3000200010000)>>48);
00075 #endif
00076 }
00077 #else
00078 while (len > 4 && s2 < (1U << 31)) {
00079 DO4(buf);
00080 len -= 4;
00081 }
00082 #endif
00083 DO1(buf); len--;
00084 s1 %= BASE;
00085 s2 %= BASE;
00086 }
00087 return (s2 << 16) | s1;
00088 }
00089
00090 #ifdef TEST
00091
00092 #include <string.h>
00093 #include "log.h"
00094 #include "timer.h"
00095 #define LEN 7001
00096
00097 static volatile int checksum;
00098
00099 int main(int argc, char **argv)
00100 {
00101 int i;
00102 char data[LEN];
00103
00104 av_log_set_level(AV_LOG_DEBUG);
00105
00106 for (i = 0; i < LEN; i++)
00107 data[i] = ((i * i) >> 3) + 123 * i;
00108
00109 if (argc > 1 && !strcmp(argv[1], "-t")) {
00110 for (i = 0; i < 1000; i++) {
00111 START_TIMER;
00112 checksum = av_adler32_update(1, data, LEN);
00113 STOP_TIMER("adler");
00114 }
00115 } else {
00116 checksum = av_adler32_update(1, data, LEN);
00117 }
00118
00119 av_log(NULL, AV_LOG_DEBUG, "%X (expected 50E6E508)\n", checksum);
00120 return checksum == 0x50e6e508 ? 0 : 1;
00121 }
00122
00123 #endif