]> git.proxmox.com Git - ceph.git/blame - ceph/src/include/blobhash.h
import 15.2.0 Octopus source
[ceph.git] / ceph / src / include / blobhash.h
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2/*
3 * Ceph - scalable distributed file system
4 *
5 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
6 *
7 * This is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License version 2.1, as published by the Free Software
10 * Foundation. See file COPYING.
11 *
12 */
13
14#ifndef CEPH_BLOBHASH_H
15#define CEPH_BLOBHASH_H
16
9f95a23c 17#include <cstdint>
7c673cae
FG
18#include "hash.h"
19
7c673cae
FG
20class blobhash {
21public:
9f95a23c
TL
22 uint32_t operator()(const void* p, size_t len) {
23 static rjhash<std::uint32_t> H;
24 std::uint32_t acc = 0;
25 auto buf = static_cast<const unsigned char*>(p);
7c673cae 26 while (len >= sizeof(acc)) {
9f95a23c
TL
27 acc ^= unaligned_load(buf);
28 buf += sizeof(std::uint32_t);
29 len -= sizeof(std::uint32_t);
7c673cae 30 }
9f95a23c
TL
31 // handle the last few bytes of p[-(len % 4):]
32 switch (len) {
33 case 3:
34 acc ^= buf[2] << 16;
35 [[fallthrough]];
36 case 2:
37 acc ^= buf[1] << 8;
38 [[fallthrough]];
39 case 1:
40 acc ^= buf[0];
7c673cae
FG
41 }
42 return H(acc);
43 }
9f95a23c
TL
44private:
45 static inline std::uint32_t unaligned_load(const unsigned char* p) {
46 std::uint32_t result;
47 __builtin_memcpy(&result, p, sizeof(result));
48 return result;
49 }
7c673cae
FG
50};
51
52
53#endif