Nestedsums library
utils.h
Go to the documentation of this file.
1
8/*
9 * Copyright (C) 2001-2017 Stefan Weinzierl
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26#ifndef __NESTEDSUMS_UTILS_H__
27#define __NESTEDSUMS_UTILS_H__
28
29#include <typeinfo>
30
31#include "ginac/ginac.h"
32
33#include "config.h"
34
35namespace nestedsums {
36
37// some functions stolen from GiNaC:
38
41inline unsigned rotate_left_31(unsigned n)
42{
43 // clear highest bit and shift 1 bit to the left
44 n = (n & 0x7FFFFFFFU) << 1;
45
46 // overflow? clear highest bit and set lowest bit
47 if (n & 0x80000000U)
48 n = (n & 0x7FFFFFFFU) | 0x00000001U;
49
50 GINAC_ASSERT(n<0x80000000U);
51
52 return n;
53}
54
55#if SIZEOF_VOID_P == SIZEOF_INT
56typedef unsigned int p_int;
57#elif SIZEOF_VOID_P == SIZEOF_LONG
58typedef unsigned long p_int;
59#elif SIZEOF_VOID_P == SIZEOF_LONG_LONG
60typedef unsigned long long p_int;
61#else
62typedef unsigned long p_int;
63#endif
64
66inline unsigned golden_ratio_hash(unsigned n)
67{
68 // This function requires arithmetic with at least 64 significant bits
69#if SIZEOF_LONG >= 8
70 // So 'long' has 64 bits. Excellent! We prefer it because it might be
71 // more efficient than 'long long'.
72 unsigned long l = n * 0x4f1bbcddL;
73 return (l & 0x7fffffffU) ^ (l >> 32);
74#elif SIZEOF_LONG_LONG >= 8
75 // This requires 'long long' (or an equivalent 64 bit type)---which is,
76 // unfortunately, not ANSI-C++-compliant.
77 // (Yet C99 demands it, which is reason for hope.)
78 unsigned long long l = n * 0x4f1bbcddL;
79 return (l & 0x7fffffffU) ^ (l >> 32);
80#elif SIZEOF_LONG_DOUBLE > 8
81 // If 'long double' is bigger than 64 bits, we assume that the mantissa
82 // has at least 64 bits. This is not guaranteed but it's a good guess.
83 // Unfortunately, it may lead to horribly slow code.
84 const static long double golden_ratio = .618033988749894848204586834370;
85 long double m = golden_ratio * n;
86 return unsigned((m - int(m)) * 0x80000000);
87#else
88#error "No 64 bit data type. You lose."
89#endif
90}
91
92static inline unsigned make_hash_seed(const std::type_info& tinfo)
93{
94 // this pointer is the same for all objects of the same type.
95 // Hence we can use that pointer
96 const void* mangled_name_ptr = (const void*)tinfo.name();
97 unsigned v = golden_ratio_hash((p_int)mangled_name_ptr);
98 return v;
99}
100
101
102} // namespace nestedsums
103
104#endif // ndef __NESTEDSUMS_UTILS_H__
105
Definition basic_letter.cc:35
unsigned rotate_left_31(unsigned n)
Definition utils.h:41
unsigned golden_ratio_hash(unsigned n)
Definition utils.h:66