souffle  2.0.2-371-g6315b36
RamTypes.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file RamTypes.h
12  *
13  * Defines tuple element type and data type for keys on table columns
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include <array>
20 #include <cstdint>
21 #include <cstring>
22 #include <iostream>
23 #include <limits>
24 #include <type_traits>
25 
26 namespace souffle {
27 
28 // deprecated. use `std::array` directly.
29 template <typename A, size_t N>
30 using Tuple = std::array<A, N>;
31 
32 /**
33  * Types of elements in a tuple.
34  *
35  * Default domain has size of 32 bits; may be overridden by user
36  * defining RAM_DOMAIN_SIZE.
37  */
38 
39 #ifndef RAM_DOMAIN_SIZE
40 #define RAM_DOMAIN_SIZE 32
41 #endif
42 
43 #if RAM_DOMAIN_SIZE == 64
44 using RamDomain = int64_t;
45 using RamSigned = RamDomain;
46 using RamUnsigned = uint64_t;
47 // There is not standard fixed size double/float.
48 using RamFloat = double;
49 #else
50 using RamDomain = int32_t;
51 using RamSigned = RamDomain;
52 using RamUnsigned = uint32_t;
53 // There is no standard - fixed size double/float.
54 using RamFloat = float;
55 #endif
56 
57 // Compile time sanity checks
58 static_assert(std::is_integral<RamSigned>::value && std::is_signed<RamSigned>::value,
59  "RamSigned must be represented by a signed type.");
60 static_assert(std::is_integral<RamUnsigned>::value && !std::is_signed<RamUnsigned>::value,
61  "RamUnsigned must be represented by an unsigned type.");
62 static_assert(std::is_floating_point<RamFloat>::value && sizeof(RamFloat) * 8 == RAM_DOMAIN_SIZE,
63  "RamFloat must be represented by a floating point and have the same size as other types.");
64 
65 template <typename T>
66 constexpr bool isRamType = (std::is_same<T, RamDomain>::value || std::is_same<T, RamSigned>::value ||
67  std::is_same<T, RamUnsigned>::value || std::is_same<T, RamFloat>::value);
68 
69 /**
70 In C++20 there will be a new way to cast between types by reinterpreting bits (std::bit_cast),
71 but as of January 2020 it is not yet supported.
72 **/
73 
74 /** Cast a type by reinterpreting its bits. Domain is restricted to Ram Types only.
75  * Template takes two types (second type is never necessary because it can be deduced from the argument)
76  * The following always holds
77  * For type T and a : T
78  * ramBitCast<T>(ramBitCast<RamDomain>(a)) == a
79  **/
80 template <typename To = RamDomain, typename From>
81 To ramBitCast(From source) {
82  static_assert(isRamType<From> && isRamType<To>, "Bit casting should only be used on Ram Types.");
83  static_assert(sizeof(To) == sizeof(From), "Can't bit cast types with different size.");
84  To destination;
85  memcpy(&destination, &source, sizeof(destination));
86  return destination;
87 }
88 
89 /** lower and upper boundaries for the ram types **/
90 constexpr RamSigned MIN_RAM_SIGNED = std::numeric_limits<RamSigned>::min();
91 constexpr RamSigned MAX_RAM_SIGNED = std::numeric_limits<RamSigned>::max();
92 
93 constexpr RamUnsigned MIN_RAM_UNSIGNED = std::numeric_limits<RamUnsigned>::min();
94 constexpr RamUnsigned MAX_RAM_UNSIGNED = std::numeric_limits<RamUnsigned>::max();
95 
96 constexpr RamFloat MIN_RAM_FLOAT = std::numeric_limits<RamFloat>::lowest();
97 constexpr RamFloat MAX_RAM_FLOAT = std::numeric_limits<RamFloat>::max();
98 } // end of namespace souffle
souffle::RamUnsigned
uint32_t RamUnsigned
Definition: RamTypes.h:58
souffle::MAX_RAM_SIGNED
constexpr RamSigned MAX_RAM_SIGNED
Definition: RamTypes.h:97
souffle::RamDomain
int32_t RamDomain
Definition: RamTypes.h:56
souffle::RamFloat
float RamFloat
Definition: RamTypes.h:60
souffle::isRamType
constexpr bool isRamType
Definition: RamTypes.h:72
souffle::MIN_RAM_FLOAT
constexpr RamFloat MIN_RAM_FLOAT
Definition: RamTypes.h:102
souffle::MAX_RAM_UNSIGNED
constexpr RamUnsigned MAX_RAM_UNSIGNED
Definition: RamTypes.h:100
souffle::MAX_RAM_FLOAT
constexpr RamFloat MAX_RAM_FLOAT
Definition: RamTypes.h:103
souffle::Tuple
std::array< A, N > Tuple
Definition: RamTypes.h:36
souffle::MIN_RAM_SIGNED
constexpr RamSigned MIN_RAM_SIGNED
lower and upper boundaries for the ram types
Definition: RamTypes.h:96
souffle::MIN_RAM_UNSIGNED
constexpr RamUnsigned MIN_RAM_UNSIGNED
Definition: RamTypes.h:99
souffle
Definition: AggregateOp.h:25
souffle::ramBitCast
To ramBitCast(From source)
In C++20 there will be a new way to cast between types by reinterpreting bits (std::bit_cast),...
Definition: RamTypes.h:87
RAM_DOMAIN_SIZE
#define RAM_DOMAIN_SIZE
Types of elements in a tuple.
Definition: RamTypes.h:46
souffle::RamSigned
RamDomain RamSigned
Definition: RamTypes.h:57