Fix Idle count
[samplevnf.git] / VNFs / DPPD-PROX / tools / flow_extract / crc.hpp
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #ifndef _CRC_H_
18 #define _CRC_H_
19
20 static uint32_t crc32(const uint8_t *buf, size_t len, int init)
21 {
22         uint32_t ret = init;
23
24         while (len/8) {
25                 ret = __builtin_ia32_crc32di(ret, *((uint64_t*)buf));
26                 len -= 8;
27                 buf += 8;
28         }
29
30         while (len/4) {
31                 ret = __builtin_ia32_crc32si(ret, *((uint32_t*)buf));
32                 len -= 4;
33                 buf += 4;
34         }
35
36         while (len/2) {
37                 ret = __builtin_ia32_crc32hi(ret, *((uint16_t*)buf));
38                 len -= 2;
39                 buf += 2;
40         }
41
42         while (len) {
43                 ret = __builtin_ia32_crc32qi(ret, *((uint8_t*)buf));
44                 len -= 1;
45                 buf += 1;
46         }
47
48         return ret;
49 }
50
51 #endif /* _CRC_H_ */