Debian Codegraph @

src/atlas/util/Checksum.cc

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #include <stdint.h>
12 #include <cstddef>
13
14 #include "atlas/util/Checksum.h"
15
16 namespace atlas {
17 namespace util {
18 namespace { // anonymous
19
20 // Inefficient implementation of Fletcher's checksum algorithm
21 static uint16_t fletcher16(const uint8_t* data, int size) {
22 uint16_t s1(0), s2(0);
23 for (int i = 0; i < size; ++i) {
24 s1 = (s1 + data[i]) % 255;
25 s2 = (s2 + s1) % 255;
26 }
27 s2 <<= 8;
28 s2 |= s1;
29 return s2;
30 }
31
32 } // namespace
33
34 static checksum_t checksum(const char* data, size_t size) {
35 // return fletcher64( reinterpret_cast<const uint32_t*>(data),
36 // size/sizeof(uint32_t) );
37 // return fletcher32( reinterpret_cast<const uint16_t*>(data),
38 // size/sizeof(uint16_t) );
39 return fletcher16(reinterpret_cast<const uint8_t*>(data), size / sizeof(uint8_t));
40 }
41
42 checksum_t checksum(const int values[], size_t size) {
43 return checksum(reinterpret_cast<const char*>(&values[0]), size * sizeof(int) / sizeof(char));
44 }
45
46 checksum_t checksum(const long values[], size_t size) {
47 return checksum(reinterpret_cast<const char*>(&values[0]), size * sizeof(long) / sizeof(char));
48 }
49
50 checksum_t checksum(const float values[], size_t size) {
51 return checksum(reinterpret_cast<const char*>(&values[0]), size * sizeof(float) / sizeof(char));
52 }
53
54 checksum_t checksum(const double values[], size_t size) {
55 return checksum(reinterpret_cast<const char*>(&values[0]), size * sizeof(double) / sizeof(char));
56 }
57
58 checksum_t checksum(const checksum_t values[], size_t size) {
59 return checksum(reinterpret_cast<const char*>(&values[0]), size * sizeof(checksum_t) / sizeof(char));
60 }
61
62 } // namespace util
63 } // namespace atlas