xapian-core  1.4.27
sortable-serialise.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2007,2009,2015,2016 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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 General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <config.h>
22 
23 #include <xapian/queryparser.h>
24 
25 // Disable these assertions when building the library as these functions are
26 // marked not to throw exceptions in the API headers. In unittest we define
27 // UNITTEST_ASSERT_NOTHROW to set a variable to the exception message and
28 // return, then the harness checks if that variable has been set.
29 #ifndef XAPIAN_UNITTEST
30 # define UNITTEST_ASSERT_NOTHROW(COND,RET)
31 #endif
32 
33 #include <cfloat>
34 #include <cmath>
35 #include <cstring>
36 
37 #include <string>
38 
39 using namespace std;
40 
41 #if FLT_RADIX != 2
42 # error Code currently assumes FLT_RADIX == 2
43 #endif
44 
45 #ifdef _MSC_VER
46 // Disable warning about negating an unsigned type, which we do deliberately.
47 # pragma warning(disable:4146)
48 #endif
49 
50 size_t
52 {
53  double mantissa;
54  int exponent;
55 
56  // Negative infinity.
57  if (value < -DBL_MAX) return 0;
58 
59  mantissa = frexp(value, &exponent);
60 
61  /* Deal with zero specially.
62  *
63  * IEEE representation of doubles uses 11 bits for the exponent, with a
64  * bias of 1023. We bias this by subtracting 8, and non-IEEE
65  * representations may allow higher exponents, so allow exponents down to
66  * -2039 - if smaller exponents are possible anywhere, we underflow such
67  * numbers to 0.
68  */
69  if (mantissa == 0.0 || exponent < -2039) {
70  *buf = '\x80';
71  return 1;
72  }
73 
74  bool negative = (mantissa < 0);
75  if (negative) mantissa = -mantissa;
76 
77  // Infinity, or extremely large non-IEEE representation.
78  if (value > DBL_MAX || exponent > 2055) {
79  if (negative) {
80  // This can only happen with a non-IEEE representation, because
81  // we've already tested for value < -DBL_MAX
82  return 0;
83  } else {
84  memset(buf, '\xff', 9);
85  return 9;
86  }
87  }
88 
89  // Encoding:
90  //
91  // [ 7 | 6 | 5 | 4 3 2 1 0]
92  // Sm Se Le
93  //
94  // Sm stores the sign of the mantissa: 1 = positive or zero, 0 = negative.
95  // Se stores the sign of the exponent: Sm for positive/zero, !Sm for neg.
96  // Le stores the length of the exponent: !Se for 3 bits, Se for 11 bits.
97  unsigned char next = (negative ? 0 : 0xe0);
98 
99  // Bias the exponent by 8 so that more small integers get short encodings.
100  exponent -= 8;
101  bool exponent_negative = (exponent < 0);
102  if (exponent_negative) {
103  exponent = -exponent;
104  next ^= 0x60;
105  }
106 
107  size_t len = 0;
108 
109  /* We store the exponent in 3 or 11 bits. If the number is negative, we
110  * flip all the bits of the exponent, since larger negative numbers should
111  * sort first.
112  *
113  * If the exponent is negative, we flip the bits of the exponent, since
114  * larger negative exponents should sort first (unless the number is
115  * negative, in which case they should sort later).
116  */
117  UNITTEST_ASSERT_NOTHROW(exponent >= 0, 0);
118  if (exponent < 8) {
119  next ^= 0x20;
120  next |= static_cast<unsigned char>(exponent << 2);
121  if (negative ^ exponent_negative) next ^= 0x1c;
122  } else {
123  UNITTEST_ASSERT_NOTHROW((exponent >> 11) == 0, 0);
124  // Put the top 5 bits of the exponent into the lower 5 bits of the
125  // first byte:
126  next |= static_cast<unsigned char>(exponent >> 6);
127  if (negative ^ exponent_negative) next ^= 0x1f;
128  buf[len++] = next;
129  // And the lower 6 bits of the exponent go into the upper 6 bits
130  // of the second byte:
131  next = static_cast<unsigned char>(exponent << 2);
132  if (negative ^ exponent_negative) next ^= 0xfc;
133  }
134 
135  // Convert the 52 (or 53) bits of the mantissa into two 32-bit words.
136  mantissa *= 1 << (negative ? 26 : 27);
137  unsigned word1 = static_cast<unsigned>(mantissa);
138  mantissa -= word1;
139  unsigned word2 = static_cast<unsigned>(mantissa * 4294967296.0); // 1<<32
140  // If the number is positive, the first bit will always be set because 0.5
141  // <= mantissa < 1, unless mantissa is zero, which we handle specially
142  // above). If the number is negative, we negate the mantissa instead of
143  // flipping all the bits, so in the case of 0.5, the first bit isn't set
144  // so we need to store it explicitly. But for the cost of one extra
145  // leading bit, we can save several trailing 0xff bytes in lots of common
146  // cases.
147  UNITTEST_ASSERT_NOTHROW(negative || (word1 & (1<<26)), 0);
148  if (negative) {
149  // We negate the mantissa for negative numbers, so that the sort order
150  // is reversed (since larger negative numbers should come first).
151  word1 = -word1;
152  if (word2 != 0) ++word1;
153  word2 = -word2;
154  }
155 
156  word1 &= 0x03ffffff;
157  next |= static_cast<unsigned char>(word1 >> 24);
158  buf[len++] = next;
159  buf[len++] = char(word1 >> 16);
160  buf[len++] = char(word1 >> 8);
161  buf[len++] = char(word1);
162 
163  buf[len++] = char(word2 >> 24);
164  buf[len++] = char(word2 >> 16);
165  buf[len++] = char(word2 >> 8);
166  buf[len++] = char(word2);
167 
168  // Finally, we can chop off any trailing zero bytes.
169  while (len > 0 && buf[len - 1] == '\0') {
170  --len;
171  }
172 
173  return len;
174 }
175 
178 static inline unsigned char
179 numfromstr(const std::string & str, std::string::size_type pos)
180 {
181  return (pos < str.size()) ? static_cast<unsigned char>(str[pos]) : '\0';
182 }
183 
184 double
186 {
187  // Zero.
188  if (value.size() == 1 && value[0] == '\x80') return 0.0;
189 
190  // Positive infinity.
191  if (value.size() == 9 &&
192  memcmp(value.data(), "\xff\xff\xff\xff\xff\xff\xff\xff\xff", 9) == 0) {
193  // "On implementations that support floating-point infinities,
194  // [HUGE_VAL] always expand[s] to the positive infinit[y] of double"
195  // https://en.cppreference.com/w/cpp/numeric/math/HUGE_VAL
196  return HUGE_VAL;
197  }
198 
199  // Negative infinity.
200  if (value.empty()) {
201  return -HUGE_VAL;
202  }
203 
204  unsigned char first = numfromstr(value, 0);
205  size_t i = 0;
206 
207  first ^= static_cast<unsigned char>(first & 0xc0) >> 1;
208  bool negative = !(first & 0x80);
209  bool exponent_negative = (first & 0x40);
210  bool explen = !(first & 0x20);
211  int exponent = first & 0x1f;
212  if (!explen) {
213  exponent >>= 2;
214  if (negative ^ exponent_negative) exponent ^= 0x07;
215  } else {
216  first = numfromstr(value, ++i);
217  exponent <<= 6;
218  exponent |= (first >> 2);
219  if (negative ^ exponent_negative) exponent ^= 0x07ff;
220  }
221 
222  unsigned word1;
223  word1 = (unsigned(first & 0x03) << 24);
224  word1 |= numfromstr(value, ++i) << 16;
225  word1 |= numfromstr(value, ++i) << 8;
226  word1 |= numfromstr(value, ++i);
227 
228  unsigned word2 = 0;
229  if (i < value.size()) {
230  word2 = numfromstr(value, ++i) << 24;
231  word2 |= numfromstr(value, ++i) << 16;
232  word2 |= numfromstr(value, ++i) << 8;
233  word2 |= numfromstr(value, ++i);
234  }
235 
236  if (negative) {
237  word1 = -word1;
238  if (word2 != 0) ++word1;
239  word2 = -word2;
240  UNITTEST_ASSERT_NOTHROW((word1 & 0xf0000000) != 0, 0);
241  word1 &= 0x03ffffff;
242  }
243  if (!negative) word1 |= 1<<26;
244 
245  double mantissa = 0;
246  if (word2) mantissa = word2 / 4294967296.0; // 1<<32
247  mantissa += word1;
248  mantissa /= 1 << (negative ? 26 : 27);
249 
250  if (exponent_negative) exponent = -exponent;
251  exponent += 8;
252 
253  if (negative) mantissa = -mantissa;
254 
255  // We use scalbn() since it's equivalent to ldexp() when FLT_RADIX == 2
256  // (which we currently assume), except that ldexp() will set errno if the
257  // result overflows or underflows. We don't need or want errno set (indeed
258  // configure turns on -fno-math-errno if it detects the compiler supports
259  // it).
260  return scalbn(mantissa, exponent);
261 }
STL namespace.
size_t sortable_serialise_(double value, char *buf)
string str(int value)
Convert int to std::string.
Definition: str.cc:90
static unsigned char numfromstr(const std::string &str, std::string::size_type pos)
Get a number from the character at a given position in a string, returning 0 if the string isn&#39;t long...
double sortable_unserialise(const std::string &serialised)
Convert a string encoded using sortable_serialise back to a floating point number.
#define UNITTEST_ASSERT_NOTHROW(COND, RET)
#define XAPIAN_NOEXCEPT
Definition: attributes.h:39
parsing a user query string to build a Xapian::Query object