RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
RDValue-doublemagic.h
Go to the documentation of this file.
1// Copyright (c) 2015, Novartis Institutes for BioMedical Research Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following
12// disclaimer in the documentation and/or other materials provided
13// with the distribution.
14// * Neither the name of Novartis Institutes for BioMedical Research Inc.
15// nor the names of its contributors may be used to endorse or promote
16// products derived from this software without specific prior written
17// permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30//
31#include <RDGeneral/export.h>
32#ifndef RDKIT_RDVALUE_PTRMAGIC_H
33#define RDKIT_RDVALUE_PTRMAGIC_H
34
35#include <cstdint>
36#include <cassert>
37#include <any>
38#include "Invariant.h"
39#include <iostream>
40#include <iomanip>
41#include <sstream>
42#include <vector>
43#include <string>
45#include <boost/utility.hpp>
46#include <boost/lexical_cast.hpp>
47#include <boost/type_traits.hpp>
48#include <boost/static_assert.hpp>
50#include <cmath>
51#include "LocaleSwitcher.h"
52
53#define RDVALUE_HASBOOL
54
55namespace RDKit {
56
57// Inspired by
58// https://nikic.github.io/2012/02/02/Pointer-magic-for-efficient-dynamic-value-representations.html
59// 16 bit storage for value types using Quiet NaN spaces in
60// doubles
61// Won't work on Solaris and some other os's as mmaping maps from
62// top memory down
63// Example check:
64// std::string *pointer = new std::string(v);
65// assert((reinterpret_cast<boost::uint64_t>(pointer) & StringTag) == 0);
66
67// implementations, need a typedef at compile time to figure this out.
68// current implementation is probably little endian, need to check.
69
70/*
71 Encoding for storing other things as a double. Use
72 Quiet NaN
73 Quiet NaN: // used to encode types
74 F F F 1XXX < - X = type bits (first bit is set to one)
75
76 seeeeeee|eeeemmmm|mmmmmmmm|mmmmmmmm|mmmmmmmm|mmmmmmmm|mmmmmmmm|mmmmmmmm
77 s1111111|11111ppp|pppppppp|pppppppp|pppppppp|pppppppp|pppppppp|pppppppp
78 ^- first mantissa bit 1 everything else is "payload" -^
79 ^- exponent bits all 1 and mustn't be all-zero (as it
80 ^- any sign bit would be INF then)
81
82 Available
83 8 = 1000 MaxDouble // Not really a tag, is a sentinel
84 9 = 1001 Float
85 b = 1010 Int32
86 a = 1011 Uint32
87 C = 1100 <none>
88 D = 1101 <none>
89 E = 1110 <none>
90 F = 1111 PtrTag (look at lower 3 bits for type)
91*/
92
93namespace RDTypeTag {
94static const boost::uint64_t NaN = 0xfff7FFFFFFFFFFFF; // signalling NaN
95static const boost::uint64_t MaxDouble = 0xfff8000000000000; //
96static const boost::uint64_t DoubleTag = 0xfff8000000000000; //
97static const boost::uint64_t FloatTag = 0xfff9000000000000; //
98static const boost::uint64_t IntTag = 0xfffa000000000000; //
99static const boost::uint64_t UnsignedIntTag = 0xfffb000000000000; //
100static const boost::uint64_t BoolTag = 0xfffc000000000000; //
101
102// PTR Tags use the last 3 bits for typing info
103static const boost::uint64_t PtrTag = 0xffff000000000000;
104static const boost::uint64_t StringTag = 0xffff000000000001; // 001
105static const boost::uint64_t VecDoubleTag = 0xffff000000000002; // 010
106static const boost::uint64_t VecFloatTag = 0xffff000000000003; // 011
107static const boost::uint64_t VecIntTag = 0xffff000000000004; // 100
108static const boost::uint64_t VecUnsignedIntTag = 0xffff000000000005; // 101
109static const boost::uint64_t VecStringTag = 0xffff000000000006; // 110
110static const boost::uint64_t AnyTag = 0xffff000000000007; // 111
111
112// Retrieves the tag (and PtrMask) from the type
113template <class T>
114inline boost::uint64_t GetTag() {
115 return AnyTag;
116}
117template <>
118inline boost::uint64_t GetTag<double>() {
119 return MaxDouble;
120}
121template <>
122inline boost::uint64_t GetTag<float>() {
123 return FloatTag;
124}
125template <>
126inline boost::uint64_t GetTag<int>() {
127 return IntTag;
128}
129template <>
130inline boost::uint64_t GetTag<unsigned int>() {
131 return UnsignedIntTag;
132}
133template <>
134inline boost::uint64_t GetTag<bool>() {
135 return BoolTag;
136}
137template <>
138inline boost::uint64_t GetTag<std::string>() {
139 return StringTag;
140}
141template <>
142inline boost::uint64_t GetTag<std::vector<double>>() {
143 return VecDoubleTag;
144}
145template <>
146inline boost::uint64_t GetTag<std::vector<float>>() {
147 return VecFloatTag;
148}
149template <>
150inline boost::uint64_t GetTag<std::vector<int>>() {
151 return VecIntTag;
152}
153template <>
154inline boost::uint64_t GetTag<std::vector<unsigned int>>() {
155 return VecUnsignedIntTag;
156}
157template <>
158inline boost::uint64_t GetTag<std::vector<std::string>>() {
159 return VecStringTag;
160}
161template <>
162inline boost::uint64_t GetTag<std::any>() {
163 return AnyTag;
164}
165} // namespace RDTypeTag
166
167struct RDValue {
168 // Bit Twidling for conversion from the Tag to a Pointer
169 static const boost::uint64_t TagMask = 0xFFFF000000000000;
170 static const boost::uint64_t PointerTagMask = 0xFFFF000000000007;
171 static const boost::uint64_t ApplyMask = 0x0000FFFFFFFFFFFF;
172 static const boost::uint64_t ApplyPtrMask = 0x0000FFFFFFFFFFF8;
173
174 union {
176 boost::uint64_t otherBits;
177 };
178
179 inline RDValue() : doubleBits(0.0) {}
180
181 inline RDValue(double number) {
182 if (boost::math::isnan(number)) {
183 // Store a signalling NaN for NaN's.
184 // quiet NaNs are used for other types.
186 assert(boost::math::isnan(doubleBits));
187 } else
188 doubleBits = number;
189 }
190
191 inline RDValue(float number) {
193 memcpy(((char *)&otherBits), &number, sizeof(float));
194 }
195
196 inline RDValue(int32_t number) {
197 otherBits = (((boost::uint64_t)number) & ApplyMask) | RDTypeTag::IntTag;
198 }
199
200 inline RDValue(unsigned int number) {
201 otherBits =
202 (((boost::uint64_t)number) & ApplyMask) | RDTypeTag::UnsignedIntTag;
203 }
204
205 inline RDValue(bool number) {
206 otherBits =
207 (static_cast<boost::uint64_t>(number) & ApplyMask) | RDTypeTag::BoolTag;
208 }
209
210 inline RDValue(std::any *pointer) {
211 // ensure that the pointer really is only 48 bit
212 assert((reinterpret_cast<boost::uint64_t>(pointer) & RDTypeTag::AnyTag) ==
213 0);
214 otherBits = reinterpret_cast<boost::uint64_t>(pointer) | RDTypeTag::AnyTag;
215 }
216
217 inline RDValue(const std::any &any) {
218 // ensure that the pointer really is only 48 bit
219 std::any *pointer = new std::any(any);
220 assert((reinterpret_cast<boost::uint64_t>(pointer) & RDTypeTag::AnyTag) ==
221 0);
222 otherBits = reinterpret_cast<boost::uint64_t>(pointer) | RDTypeTag::AnyTag;
223 }
224
225 // Unknown types are stored as std::any
226 template <class T>
227 inline RDValue(const T &v) {
228 std::any *pointer = new std::any(v);
229 assert((reinterpret_cast<boost::uint64_t>(pointer) & RDTypeTag::AnyTag) ==
230 0);
231 otherBits = reinterpret_cast<boost::uint64_t>(pointer) | RDTypeTag::AnyTag;
232 }
233
234 inline RDValue(const std::string &v) {
235 std::string *pointer = new std::string(v);
236 assert((reinterpret_cast<boost::uint64_t>(pointer) &
238 otherBits =
239 reinterpret_cast<boost::uint64_t>(pointer) | RDTypeTag::StringTag;
240 }
241
242 inline RDValue(const std::vector<double> &v) {
243 std::vector<double> *pointer = new std::vector<double>(v);
244 assert((reinterpret_cast<boost::uint64_t>(pointer) &
246 otherBits =
247 reinterpret_cast<boost::uint64_t>(pointer) | RDTypeTag::VecDoubleTag;
248 }
249
250 inline RDValue(const std::vector<float> &v) {
251 std::vector<float> *pointer = new std::vector<float>(v);
252 assert((reinterpret_cast<boost::uint64_t>(pointer) &
254 otherBits =
255 reinterpret_cast<boost::uint64_t>(pointer) | RDTypeTag::VecFloatTag;
256 }
257
258 inline RDValue(const std::vector<int> &v) {
259 std::vector<int> *pointer = new std::vector<int>(v);
260 assert((reinterpret_cast<boost::uint64_t>(pointer) &
262 otherBits =
263 reinterpret_cast<boost::uint64_t>(pointer) | RDTypeTag::VecIntTag;
264 }
265
266 inline RDValue(const std::vector<unsigned int> &v) {
267 std::vector<unsigned int> *pointer = new std::vector<unsigned int>(v);
268 assert((reinterpret_cast<boost::uint64_t>(pointer) &
270 otherBits = reinterpret_cast<boost::uint64_t>(pointer) |
272 }
273
274 inline RDValue(const std::vector<std::string> &v) {
275 std::vector<std::string> *pointer = new std::vector<std::string>(v);
276 assert((reinterpret_cast<boost::uint64_t>(pointer) &
278 otherBits =
279 reinterpret_cast<boost::uint64_t>(pointer) | RDTypeTag::VecStringTag;
280 }
281
282 boost::uint64_t getTag() const {
286 }
287
288 boost::uint64_t tag = otherBits & TagMask;
290 return tag;
291 }
292
293 // ptrCast - unsafe, use rdvalue_cast instead.
294 template <class T>
295 inline T *ptrCast() const {
296 return reinterpret_cast<T *>(otherBits & ~RDTypeTag::GetTag<T>());
297 }
298
299 // RDValue doesn't have an explicit destructor, it must
300 // be wrapped in a container.
301 // The idea is that POD types don't need to be destroyed
302 // and this allows the container optimization possibilities.
303 inline void destroy() {
304 switch (getTag()) {
306 delete ptrCast<std::string>();
307 break;
310 break;
313 break;
315 delete ptrCast<std::vector<int>>();
316 break;
319 break;
322 break;
324 delete ptrCast<std::any>();
325 break;
326 default:
327 break;
328 }
329 }
330
331 static inline void cleanup_rdvalue(RDValue v) { v.destroy(); }
332};
333
334/////////////////////////////////////////////////////////////////////////////////////
335// Given two RDValue::Values - copy the appropriate structure
336// RDValue doesn't have a copy constructor, the default
337// copy act's like a move for better value semantics.
338// Containers may need to copy though.
339inline void copy_rdvalue(RDValue &dest, const RDValue &src) {
340 dest.destroy();
341 switch (src.getTag()) {
343 dest = RDValue(*src.ptrCast<std::string>());
344 break;
346 dest = RDValue(*src.ptrCast<std::vector<double>>());
347 break;
349 dest = RDValue(*src.ptrCast<std::vector<float>>());
350 break;
352 dest = RDValue(*src.ptrCast<std::vector<int>>());
353 break;
355 dest = RDValue(*src.ptrCast<std::vector<unsigned int>>());
356 break;
358 dest = RDValue(*src.ptrCast<std::vector<std::string>>());
359 break;
361 dest = RDValue(*src.ptrCast<std::any>());
362 break;
363 default:
364 dest = src;
365 }
366}
367
368/////////////////////////////////////////////////////////////////////////////////////
369// rdvalue_is<T>
370
371template <class T>
372inline bool rdvalue_is(const RDValue_cast_t) {
373 const short tag =
374 RDTypeTag::GetTag<typename boost::remove_reference<T>::type>();
375 if (v.getTag() == tag) return true;
376
377 // If we are an Any tag, check the any type info
378 if (v.getTag() == RDTypeTag::AnyTag) {
379 return v.value.a->type() == typeid(T);
380 }
381
382 return false;
383}
384
385template <>
387 return v.otherBits < RDTypeTag::MaxDouble ||
388 (v.otherBits & RDTypeTag::NaN) == RDTypeTag::NaN;
389}
390
391template <>
393 return rdvalue_is<double>(v);
394}
395
396/*
397template<>
398inline bool rdvalue_is<bool>(const RDValue_cast_t) {
399 return (v.getTag() == RDTypeTag::IntTag &&
400 (static_cast<int32_t>(v.otherBits & ~RDTypeTag::IntTag) == 1 ||
401 static_cast<int32_t>(v.otherBits & ~RDTypeTag::IntTag) == 0 ));
402}
403
404template<>
405inline bool rdvalue_is<const bool&>(const RDValue_cast_t) {
406 return rdvalue_is<bool>(v);
407}
408*/
409
410/////////////////////////////////////////////////////////////////////////////////////
411// rdvalue_cast<T>
412//
413// POD types do not support reference semantics. Other types do.
414// rdvalue_cast<const std::vector<double> &>(RDValue); // ok
415// rdvalue_cast<const float &>(RDValue); // bad_any_cast
416
418// Get stuff stored in boost any
419template <class T>
421 // Disable reference and pointer casts to POD data.
423 (boost::is_pointer<T>::value &&
424 (boost::is_integral<typename boost::remove_pointer<T>::type>::value ||
425 boost::is_floating_point<
426 typename boost::remove_pointer<T>::type>::value)) ||
427 (boost::is_reference<T>::value &&
428 (boost::is_integral<typename boost::remove_reference<T>::type>::value ||
429 boost::is_floating_point<
430 typename boost::remove_reference<T>::type>::value))));
431
432 if (rdvalue_is<std::any>(v)) {
433 return std::any_cast<T>(*v.ptrCast<std::any>());
434 }
435 throw std::bad_any_cast();
436}
437
438// POD casts
439template <>
441 if (rdvalue_is<double>(v)) return v.doubleBits;
442 throw std::bad_any_cast();
443}
444
445template <>
447 if (rdvalue_is<float>(v)) {
448 float f;
449 memcpy(&f, ((char *)&v.otherBits), sizeof(float));
450 return f;
451 }
452 throw std::bad_any_cast();
453}
454
455// n.b. with const expressions, could use ~RDTagTypes::GetTag<T>()
456// and enable_if
457template <>
459 if (rdvalue_is<int>(v))
460 return static_cast<int32_t>(v.otherBits & ~RDTypeTag::IntTag);
461 throw std::bad_any_cast();
462}
463template <>
466 return static_cast<uint32_t>(v.otherBits & ~RDTypeTag::UnsignedIntTag);
467 throw std::bad_any_cast();
468}
469
470template <>
472 if (rdvalue_is<bool>(v))
473 return static_cast<bool>(v.otherBits & ~RDTypeTag::BoolTag);
474 throw std::bad_any_cast();
475}
476
477} // namespace RDKit
478#endif
static const boost::uint64_t NaN
boost::uint64_t GetTag< int >()
static const boost::uint64_t UnsignedIntTag
static const boost::uint64_t StringTag
boost::uint64_t GetTag< std::string >()
static const boost::uint64_t MaxDouble
boost::uint64_t GetTag< bool >()
boost::uint64_t GetTag< unsigned int >()
boost::uint64_t GetTag()
static const boost::uint64_t VecStringTag
boost::uint64_t GetTag< std::any >()
static const boost::uint64_t VecIntTag
static const boost::uint64_t FloatTag
static const boost::uint64_t VecUnsignedIntTag
static const boost::uint64_t DoubleTag
boost::uint64_t GetTag< double >()
static const boost::uint64_t IntTag
boost::uint64_t GetTag< float >()
static const boost::uint64_t AnyTag
static const boost::uint64_t PtrTag
static const boost::uint64_t VecFloatTag
static const boost::uint64_t VecDoubleTag
static const boost::uint64_t BoolTag
Std stuff.
int rdvalue_cast< int >(RDValue_cast_t v)
unsigned int rdvalue_cast< unsigned int >(RDValue_cast_t v)
bool rdvalue_is(const RDValue_cast_t)
double rdvalue_cast< double >(RDValue_cast_t v)
bool rdvalue_is< const double & >(const RDValue_cast_t)
void copy_rdvalue(RDValue &dest, const RDValue &src)
T rdvalue_cast(RDValue_cast_t v)
bool rdvalue_is< double >(const RDValue_cast_t)
RDValue RDValue_cast_t
bool rdvalue_cast< bool >(RDValue_cast_t v)
float rdvalue_cast< float >(RDValue_cast_t v)
bool rdvalue_is< std::any >(RDValue_cast_t v)
RDValue(const std::vector< int > &v)
RDValue(const std::vector< std::string > &v)
static const boost::uint64_t TagMask
RDValue(const std::vector< double > &v)
RDValue(const std::vector< unsigned int > &v)
RDValue(double number)
RDValue(int32_t number)
boost::uint64_t getTag() const
RDValue(const std::any &any)
static const boost::uint64_t ApplyPtrMask
boost::uint64_t otherBits
RDValue(float number)
RDValue(const std::string &v)
RDValue(unsigned int number)
RDValue(const std::vector< float > &v)
static void cleanup_rdvalue(RDValue v)
static const boost::uint64_t PointerTagMask
RDValue(std::any *pointer)
static const boost::uint64_t ApplyMask