Debian Codegraph @

Source/Reasoner/Kernel/Process/CDatatypeFloatValueSpaceMap.cpp

raw
1 /*
2 * Copyright (C) 2013-2015, 2019 by the Konclude Developer Team.
3 *
4 * This file is part of the reasoning system Konclude.
5 * For details and support, see <http://konclude.com/>.
6 *
7 * Konclude is free software: you can redistribute it and/or modify
8 * it under the terms of version 3 of the GNU Lesser General Public
9 * License (LGPLv3) as published by the Free Software Foundation.
10 *
11 * Konclude is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU (Lesser) General Public License for more details.
15 *
16 * You should have received a copy of the GNU (Lesser) General Public
17 * License along with Konclude. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 #include "CDatatypeFloatValueSpaceMap.h"
22
23
24 namespace Konclude {
25
26 namespace Reasoner {
27
28 namespace Kernel {
29
30 namespace Process {
31
32
33 CDatatypeFloatValueSpaceMap::CDatatypeFloatValueSpaceMap(CProcessContext* processContext) : CDatatypeCompareValueSpaceMap(processContext) {
34 }
35
36 bool CDatatypeFloatValueSpaceMap::representsInfinitelyManyValues(CDataLiteralCompareValue* leftValueExcluded, CDataLiteralCompareValue* rightValueExcluded) {
37 return false;
38 }
39
40 bool CDatatypeFloatValueSpaceMap::representsInfinitelyManyValues(CDataLiteralCompareValue* value) {
41 return false;
42 }
43
44 bool CDatatypeFloatValueSpaceMap::getValueCopy(CDataLiteralCompareValue* valueCopyInto, CDataLiteralCompareValue* valueCopyFrom) {
45 CDataLiteralFloatValue* floatValueCopyInto = dynamic_cast<CDataLiteralFloatValue*>(valueCopyInto);
46 CDataLiteralFloatValue* floatValueCopyFrom = dynamic_cast<CDataLiteralFloatValue*>(valueCopyFrom);
47 floatValueCopyInto->initValue(floatValueCopyFrom);
48 return true;
49 }
50
51
52 bool CDatatypeFloatValueSpaceMap::isNaN(int bits) {
53 return ((bits & 0x7f800000) == 0x7f800000) && ((bits & 0x003fffff) != 0);
54 }
55
56 bool CDatatypeFloatValueSpaceMap::getValueNext(CDataLiteralCompareValue* valueNext, CDataLiteralCompareValue* valueLast) {
57 CDataLiteralFloatValue* floatValueNext = dynamic_cast<CDataLiteralFloatValue*>(valueNext);
58 CDataLiteralFloatValue* floatValueLast = dynamic_cast<CDataLiteralFloatValue*>(valueLast);
59
60
61 float nextFloatValue = 0.;
62 float lastFloatValue = floatValueLast->getFloat();
63 int bits = *reinterpret_cast<int*>(&lastFloatValue);
64 int magnitude = (bits & 0x7fffffff);
65 bool positive = ((bits & 0x80000000)==0);
66 // The successors of NaN and +INF are these numbers themselves.
67 if (isNaN(bits) || (magnitude == 0x7f800000 && positive)) {
68 floatValueNext->initValueFromFloat(lastFloatValue);
69 } else {
70 bool newPositive;
71 int newMagnitude;
72 if (positive) {
73 newPositive = true;
74 newMagnitude = magnitude+1;
75 } else if (!positive && magnitude == 0) {
76 // The successor of -0 is +0
77 newPositive = true;
78 newMagnitude = 1; // skip +0 for now
79 } else { // if (!positive && magnitude != 0)
80 newPositive = false;
81 newMagnitude = magnitude-1;
82 }
83 int newBits = newMagnitude | (newPositive ? 0 : 0x80000000);
84 nextFloatValue = *reinterpret_cast<float*>(&newBits);
85 floatValueNext->initValueFromFloat(nextFloatValue);
86 return true;
87 }
88 return false;
89 }
90
91
92 cuint64 CDatatypeFloatValueSpaceMap::getIntervalValueCount(CDataLiteralCompareValue* leftValueExcluded, CDataLiteralCompareValue* rightValueExcluded) {
93 CDataLiteralFloatValue* floatValueLeft = dynamic_cast<CDataLiteralFloatValue*>(leftValueExcluded);
94 CDataLiteralFloatValue* floatValueRight = dynamic_cast<CDataLiteralFloatValue*>(rightValueExcluded);
95
96
97 float leftFloatValue = floatValueLeft->getFloat();
98 float rightFloatValue = floatValueRight->getFloat();
99
100 int bitsLowerBoundExclusive = *reinterpret_cast<int*>(&leftFloatValue);
101 int bitsUpperBoundExclusive = *reinterpret_cast<int*>(&rightFloatValue);
102 if (isNaN(bitsLowerBoundExclusive) || isNaN(bitsUpperBoundExclusive))
103 return 0;
104
105 cuint64 valueCount = 0;
106
107 bool positiveLowerBoundExclusive = ((bitsLowerBoundExclusive & 0x80000000)==0);
108 bool positiveUpperBoundExclusive = ((bitsUpperBoundExclusive & 0x80000000)==0);
109
110 int magnitudeLowerBoundExclusive = (bitsLowerBoundExclusive & 0x7fffffff);
111 int magnitudeUpperBoundExclusive = (bitsUpperBoundExclusive & 0x7fffffff);
112
113 // Determine the number of elements. This works even if 'lowerBoundExclusive' or 'upperBoundExclusive' is +INF or -INF
114 if (positiveLowerBoundExclusive && positiveUpperBoundExclusive) {
115 // it must be that magnitudeLowerBoundExclusive < magnitudeUpperBoundExclusive
116 valueCount = magnitudeUpperBoundExclusive-magnitudeLowerBoundExclusive-1;
117
118 } else if (!positiveLowerBoundExclusive && !positiveUpperBoundExclusive) {
119 // it must be that magnitudeUpperBoundExclusive < magnitudeLowerBoundExclusive
120 valueCount = magnitudeLowerBoundExclusive-magnitudeUpperBoundExclusive-1;
121
122 } else if (!positiveLowerBoundExclusive && positiveUpperBoundExclusive) {
123 // the number of values from 'lowerBoundExclusive' to -0
124 cuint64 startToMinusZero = magnitudeLowerBoundExclusive;
125
126 // the number of values from +0 to 'upperBoundExclusive'
127 int plusZeroToEnd = magnitudeUpperBoundExclusive;
128 valueCount = startToMinusZero+plusZeroToEnd -1; // extra -1 to count +0/-0 only once for now
129 }
130
131 return valueCount;
132 }
133
134
135
136 }; // end namespace Process
137
138 }; // end namespace Kernel
139
140 }; // end namespace Reasoner
141
142 }; // end namespace Konclude