double-conversion / test / cctest / test-ieee.cc

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Copyright 2006-2008 the V8 project authors. All rights reserved.

#include <stdlib.h>

#include "cctest.h"
#include "diy-fp.h"
#include "ieee.h"
#include "utils.h"
#include "../../src/ieee.h"


using namespace double_conversion;


TEST(Uint64Conversions) {
  // Start by checking the byte-order.
  uint64_t ordered = UINT64_2PART_C(0x01234567, 89ABCDEF);
  CHECK_EQ(3512700564088504e-318, Double(ordered).value());

  uint64_t min_double64 = UINT64_2PART_C(0x00000000, 00000001);
  CHECK_EQ(5e-324, Double(min_double64).value());

  uint64_t max_double64 = UINT64_2PART_C(0x7fefffff, ffffffff);
  CHECK_EQ(1.7976931348623157e308, Double(max_double64).value());
}


TEST(Uint32Conversions) {
  // Start by checking the byte-order.
  uint32_t ordered = 0x01234567;
  CHECK_EQ(2.9988165487136453e-38f, Single(ordered).value());

  uint32_t min_float32 = 0x00000001;
  CHECK_EQ(1.4e-45f, Single(min_float32).value());

  uint32_t max_float32 = 0x7f7fffff;
  CHECK_EQ(3.4028234e38f, Single(max_float32).value());
}


TEST(Double_AsDiyFp) {
  uint64_t ordered = UINT64_2PART_C(0x01234567, 89ABCDEF);
  DiyFp diy_fp = Double(ordered).AsDiyFp();
  CHECK_EQ(0x12 - 0x3FF - 52, diy_fp.e());
  // The 52 mantissa bits, plus the implicit 1 in bit 52 as a UINT64.
  CHECK(UINT64_2PART_C(0x00134567, 89ABCDEF) == diy_fp.f());  // NOLINT

  uint64_t min_double64 = UINT64_2PART_C(0x00000000, 00000001);
  diy_fp = Double(min_double64).AsDiyFp();
  CHECK_EQ(-0x3FF - 52 + 1, diy_fp.e());
  // This is a denormal; so no hidden bit.
  CHECK(1 == diy_fp.f());  // NOLINT

  uint64_t max_double64 = UINT64_2PART_C(0x7fefffff, ffffffff);
  diy_fp = Double(max_double64).AsDiyFp();
  CHECK_EQ(0x7FE - 0x3FF - 52, diy_fp.e());
  CHECK(UINT64_2PART_C(0x001fffff, ffffffff) == diy_fp.f());  // NOLINT
}


TEST(Single_AsDiyFp) {
  uint32_t ordered = 0x01234567;
  DiyFp diy_fp = Single(ordered).AsDiyFp();
  CHECK_EQ(0x2 - 0x7F - 23, diy_fp.e());
  // The 23 mantissa bits, plus the implicit 1 in bit 24 as a uint32_t.
  CHECK_EQ(0xA34567, diy_fp.f());

  uint32_t min_float32 = 0x00000001;
  diy_fp = Single(min_float32).AsDiyFp();
  CHECK_EQ(-0x7F - 23 + 1, diy_fp.e());
  // This is a denormal; so no hidden bit.
  CHECK_EQ(1, diy_fp.f());

  uint32_t max_float32 = 0x7f7fffff;
  diy_fp = Single(max_float32).AsDiyFp();
  CHECK_EQ(0xFE - 0x7F - 23, diy_fp.e());
  CHECK_EQ(0x00ffffff, diy_fp.f());
}


TEST(AsNormalizedDiyFp) {
  uint64_t ordered = UINT64_2PART_C(0x01234567, 89ABCDEF);
  DiyFp diy_fp = Double(ordered).AsNormalizedDiyFp();
  CHECK_EQ(0x12 - 0x3FF - 52 - 11, diy_fp.e());
  CHECK((UINT64_2PART_C(0x00134567, 89ABCDEF) << 11) ==
        diy_fp.f());  // NOLINT

  uint64_t min_double64 = UINT64_2PART_C(0x00000000, 00000001);
  diy_fp = Double(min_double64).AsNormalizedDiyFp();
  CHECK_EQ(-0x3FF - 52 + 1 - 63, diy_fp.e());
  // This is a denormal; so no hidden bit.
  CHECK(UINT64_2PART_C(0x80000000, 00000000) == diy_fp.f());  // NOLINT

  uint64_t max_double64 = UINT64_2PART_C(0x7fefffff, ffffffff);
  diy_fp = Double(max_double64).AsNormalizedDiyFp();
  CHECK_EQ(0x7FE - 0x3FF - 52 - 11, diy_fp.e());
  CHECK((UINT64_2PART_C(0x001fffff, ffffffff) << 11) ==
        diy_fp.f());  // NOLINT
}


TEST(Double_IsDenormal) {
  uint64_t min_double64 = UINT64_2PART_C(0x00000000, 00000001);
  CHECK(Double(min_double64).IsDenormal());
  uint64_t bits = UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
  CHECK(Double(bits).IsDenormal());
  bits = UINT64_2PART_C(0x00100000, 00000000);
  CHECK(!Double(bits).IsDenormal());
}


TEST(Single_IsDenormal) {
  uint32_t min_float32 = 0x00000001;
  CHECK(Single(min_float32).IsDenormal());
  uint32_t bits = 0x007FFFFF;
  CHECK(Single(bits).IsDenormal());
  bits = 0x00800000;
  CHECK(!Single(bits).IsDenormal());
}


TEST(Double_IsSpecial) {
  CHECK(Double(Double::Infinity()).IsSpecial());
  CHECK(Double(-Double::Infinity()).IsSpecial());
  CHECK(Double(Double::NaN()).IsSpecial());
  uint64_t bits = UINT64_2PART_C(0xFFF12345, 00000000);
  CHECK(Double(bits).IsSpecial());
  // Denormals are not special:
  CHECK(!Double(5e-324).IsSpecial());
  CHECK(!Double(-5e-324).IsSpecial());
  // And some random numbers:
  CHECK(!Double(0.0).IsSpecial());
  CHECK(!Double(-0.0).IsSpecial());
  CHECK(!Double(1.0).IsSpecial());
  CHECK(!Double(-1.0).IsSpecial());
  CHECK(!Double(1000000.0).IsSpecial());
  CHECK(!Double(-1000000.0).IsSpecial());
  CHECK(!Double(1e23).IsSpecial());
  CHECK(!Double(-1e23).IsSpecial());
  CHECK(!Double(1.7976931348623157e308).IsSpecial());
  CHECK(!Double(-1.7976931348623157e308).IsSpecial());
}


TEST(Single_IsSpecial) {
  CHECK(Single(Single::Infinity()).IsSpecial());
  CHECK(Single(-Single::Infinity()).IsSpecial());
  CHECK(Single(Single::NaN()).IsSpecial());
  uint32_t bits = 0xFFF12345;
  CHECK(Single(bits).IsSpecial());
  // Denormals are not special:
  CHECK(!Single(1.4e-45f).IsSpecial());
  CHECK(!Single(-1.4e-45f).IsSpecial());
  // And some random numbers:
  CHECK(!Single(0.0f).IsSpecial());
  CHECK(!Single(-0.0f).IsSpecial());
  CHECK(!Single(1.0f).IsSpecial());
  CHECK(!Single(-1.0f).IsSpecial());
  CHECK(!Single(1000000.0f).IsSpecial());
  CHECK(!Single(-1000000.0f).IsSpecial());
  CHECK(!Single(1e23f).IsSpecial());
  CHECK(!Single(-1e23f).IsSpecial());
  CHECK(!Single(1.18e-38f).IsSpecial());
  CHECK(!Single(-1.18e-38f).IsSpecial());
}


TEST(Double_IsInfinite) {
  CHECK(Double(Double::Infinity()).IsInfinite());
  CHECK(Double(-Double::Infinity()).IsInfinite());
  CHECK(!Double(Double::NaN()).IsInfinite());
  CHECK(!Double(0.0).IsInfinite());
  CHECK(!Double(-0.0).IsInfinite());
  CHECK(!Double(1.0).IsInfinite());
  CHECK(!Double(-1.0).IsInfinite());
  uint64_t min_double64 = UINT64_2PART_C(0x00000000, 00000001);
  CHECK(!Double(min_double64).IsInfinite());
}


TEST(Single_IsInfinite) {
  CHECK(Single(Single::Infinity()).IsInfinite());
  CHECK(Single(-Single::Infinity()).IsInfinite());
  CHECK(!Single(Single::NaN()).IsInfinite());
  CHECK(!Single(0.0f).IsInfinite());
  CHECK(!Single(-0.0f).IsInfinite());
  CHECK(!Single(1.0f).IsInfinite());
  CHECK(!Single(-1.0f).IsInfinite());
  uint32_t min_float32 = 0x00000001;
  CHECK(!Single(min_float32).IsInfinite());
}


TEST(Double_IsNan) {
  CHECK(Double(Double::NaN()).IsNan());
  uint64_t other_nan = UINT64_2PART_C(0xFFFFFFFF, 00000001);
  CHECK(Double(other_nan).IsNan());
  CHECK(!Double(Double::Infinity()).IsNan());
  CHECK(!Double(-Double::Infinity()).IsNan());
  CHECK(!Double(0.0).IsNan());
  CHECK(!Double(-0.0).IsNan());
  CHECK(!Double(1.0).IsNan());
  CHECK(!Double(-1.0).IsNan());
  uint64_t min_double64 = UINT64_2PART_C(0x00000000, 00000001);
  CHECK(!Double(min_double64).IsNan());
}


TEST(Single_IsNan) {
  CHECK(Single(Single::NaN()).IsNan());
  uint32_t other_nan = 0xFFFFF001;
  CHECK(Single(other_nan).IsNan());
  CHECK(!Single(Single::Infinity()).IsNan());
  CHECK(!Single(-Single::Infinity()).IsNan());
  CHECK(!Single(0.0f).IsNan());
  CHECK(!Single(-0.0f).IsNan());
  CHECK(!Single(1.0f).IsNan());
  CHECK(!Single(-1.0f).IsNan());
  uint32_t min_float32 = 0x00000001;
  CHECK(!Single(min_float32).IsNan());
}


TEST(Double_Sign) {
  CHECK_EQ(1, Double(1.0).Sign());
  CHECK_EQ(1, Double(Double::Infinity()).Sign());
  CHECK_EQ(-1, Double(-Double::Infinity()).Sign());
  CHECK_EQ(1, Double(0.0).Sign());
  CHECK_EQ(-1, Double(-0.0).Sign());
  uint64_t min_double64 = UINT64_2PART_C(0x00000000, 00000001);
  CHECK_EQ(1, Double(min_double64).Sign());
}


TEST(Single_Sign) {
  CHECK_EQ(1, Single(1.0f).Sign());
  CHECK_EQ(1, Single(Single::Infinity()).Sign());
  CHECK_EQ(-1, Single(-Single::Infinity()).Sign());
  CHECK_EQ(1, Single(0.0f).Sign());
  CHECK_EQ(-1, Single(-0.0f).Sign());
  uint32_t min_float32 = 0x00000001;
  CHECK_EQ(1, Single(min_float32).Sign());
}


TEST(Double_NormalizedBoundaries) {
  DiyFp boundary_plus;
  DiyFp boundary_minus;
  DiyFp diy_fp = Double(1.5).AsNormalizedDiyFp();
  Double(1.5).NormalizedBoundaries(&boundary_minus, &boundary_plus);
  CHECK_EQ(diy_fp.e(), boundary_minus.e());
  CHECK_EQ(diy_fp.e(), boundary_plus.e());
  // 1.5 does not have a significand of the form 2^p (for some p).
  // Therefore its boundaries are at the same distance.
  CHECK(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
  CHECK((1 << 10) == diy_fp.f() - boundary_minus.f());  // NOLINT

  diy_fp = Double(1.0).AsNormalizedDiyFp();
  Double(1.0).NormalizedBoundaries(&boundary_minus, &boundary_plus);
  CHECK_EQ(diy_fp.e(), boundary_minus.e());
  CHECK_EQ(diy_fp.e(), boundary_plus.e());
  // 1.0 does have a significand of the form 2^p (for some p).
  // Therefore its lower boundary is twice as close as the upper boundary.
  CHECK(boundary_plus.f() - diy_fp.f() > diy_fp.f() - boundary_minus.f());
  CHECK((1 << 9) == diy_fp.f() - boundary_minus.f());  // NOLINT
  CHECK((1 << 10) == boundary_plus.f() - diy_fp.f());  // NOLINT

  uint64_t min_double64 = UINT64_2PART_C(0x00000000, 00000001);
  diy_fp = Double(min_double64).AsNormalizedDiyFp();
  Double(min_double64).NormalizedBoundaries(&boundary_minus, &boundary_plus);
  CHECK_EQ(diy_fp.e(), boundary_minus.e());
  CHECK_EQ(diy_fp.e(), boundary_plus.e());
  // min-value does not have a significand of the form 2^p (for some p).
  // Therefore its boundaries are at the same distance.
  CHECK(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
  // Denormals have their boundaries much closer.
  CHECK((static_cast<uint64_t>(1) << 62) ==
        diy_fp.f() - boundary_minus.f());  // NOLINT

  uint64_t smallest_normal64 = UINT64_2PART_C(0x00100000, 00000000);
  diy_fp = Double(smallest_normal64).AsNormalizedDiyFp();
  Double(smallest_normal64).NormalizedBoundaries(&boundary_minus,
                                                 &boundary_plus);
  CHECK_EQ(diy_fp.e(), boundary_minus.e());
  CHECK_EQ(diy_fp.e(), boundary_plus.e());
  // Even though the significand is of the form 2^p (for some p), its boundaries
  // are at the same distance. (This is the only exception).
  CHECK(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
  CHECK((1 << 10) == diy_fp.f() - boundary_minus.f());  // NOLINT

  uint64_t largest_denormal64 = UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
  diy_fp = Double(largest_denormal64).AsNormalizedDiyFp();
  Double(largest_denormal64).NormalizedBoundaries(&boundary_minus,
                                                  &boundary_plus);
  CHECK_EQ(diy_fp.e(), boundary_minus.e());
  CHECK_EQ(diy_fp.e(), boundary_plus.e());
  CHECK(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
  CHECK((1 << 11) == diy_fp.f() - boundary_minus.f());  // NOLINT

  uint64_t max_double64 = UINT64_2PART_C(0x7fefffff, ffffffff);
  diy_fp = Double(max_double64).AsNormalizedDiyFp();
  Double(max_double64).NormalizedBoundaries(&boundary_minus, &boundary_plus);
  CHECK_EQ(diy_fp.e(), boundary_minus.e());
  CHECK_EQ(diy_fp.e(), boundary_plus.e());
  // max-value does not have a significand of the form 2^p (for some p).
  // Therefore its boundaries are at the same distance.
  CHECK(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
  CHECK((1 << 10) == diy_fp.f() - boundary_minus.f());  // NOLINT
}


TEST(Single_NormalizedBoundaries) {
  uint64_t kOne64 = 1;
  DiyFp boundary_plus;
  DiyFp boundary_minus;
  DiyFp diy_fp = Single(1.5f).AsDiyFp();
  diy_fp.Normalize();
  Single(1.5f).NormalizedBoundaries(&boundary_minus, &boundary_plus);
  CHECK_EQ(diy_fp.e(), boundary_minus.e());
  CHECK_EQ(diy_fp.e(), boundary_plus.e());
  // 1.5 does not have a significand of the form 2^p (for some p).
  // Therefore its boundaries are at the same distance.
  CHECK(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
  // Normalization shifts the significand by 8 bits. Add 32 bits for the bigger
  // data-type, and remove 1 because boundaries are at half a ULP.
  CHECK((kOne64 << 39) == diy_fp.f() - boundary_minus.f());

  diy_fp = Single(1.0f).AsDiyFp();
  diy_fp.Normalize();
  Single(1.0f).NormalizedBoundaries(&boundary_minus, &boundary_plus);
  CHECK_EQ(diy_fp.e(), boundary_minus.e());
  CHECK_EQ(diy_fp.e(), boundary_plus.e());
  // 1.0 does have a significand of the form 2^p (for some p).
  // Therefore its lower boundary is twice as close as the upper boundary.
  CHECK(boundary_plus.f() - diy_fp.f() > diy_fp.f() - boundary_minus.f());
  CHECK((kOne64 << 38) == diy_fp.f() - boundary_minus.f());  // NOLINT
  CHECK((kOne64 << 39) == boundary_plus.f() - diy_fp.f());  // NOLINT

  uint32_t min_float32 = 0x00000001;
  diy_fp = Single(min_float32).AsDiyFp();
  diy_fp.Normalize();
  Single(min_float32).NormalizedBoundaries(&boundary_minus, &boundary_plus);
  CHECK_EQ(diy_fp.e(), boundary_minus.e());
  CHECK_EQ(diy_fp.e(), boundary_plus.e());
  // min-value does not have a significand of the form 2^p (for some p).
  // Therefore its boundaries are at the same distance.
  CHECK(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
  // Denormals have their boundaries much closer.
  CHECK((kOne64 << 62) == diy_fp.f() - boundary_minus.f());  // NOLINT

  uint32_t smallest_normal32 = 0x00800000;
  diy_fp = Single(smallest_normal32).AsDiyFp();
  diy_fp.Normalize();
  Single(smallest_normal32).NormalizedBoundaries(&boundary_minus,
                                                 &boundary_plus);
  CHECK_EQ(diy_fp.e(), boundary_minus.e());
  CHECK_EQ(diy_fp.e(), boundary_plus.e());
  // Even though the significand is of the form 2^p (for some p), its boundaries
  // are at the same distance. (This is the only exception).
  CHECK(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
  CHECK((kOne64 << 39) == diy_fp.f() - boundary_minus.f());  // NOLINT

  uint32_t largest_denormal32 = 0x007FFFFF;
  diy_fp = Single(largest_denormal32).AsDiyFp();
  diy_fp.Normalize();
  Single(largest_denormal32).NormalizedBoundaries(&boundary_minus,
                                                  &boundary_plus);
  CHECK_EQ(diy_fp.e(), boundary_minus.e());
  CHECK_EQ(diy_fp.e(), boundary_plus.e());
  CHECK(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
  CHECK((kOne64 << 40) == diy_fp.f() - boundary_minus.f());  // NOLINT

  uint32_t max_float32 = 0x7f7fffff;
  diy_fp = Single(max_float32).AsDiyFp();
  diy_fp.Normalize();
  Single(max_float32).NormalizedBoundaries(&boundary_minus, &boundary_plus);
  CHECK_EQ(diy_fp.e(), boundary_minus.e());
  CHECK_EQ(diy_fp.e(), boundary_plus.e());
  // max-value does not have a significand of the form 2^p (for some p).
  // Therefore its boundaries are at the same distance.
  CHECK(diy_fp.f() - boundary_minus.f() == boundary_plus.f() - diy_fp.f());
  CHECK((kOne64 << 39) == diy_fp.f() - boundary_minus.f());  // NOLINT
}


TEST(NextDouble) {
  CHECK_EQ(4e-324, Double(0.0).NextDouble());
  CHECK_EQ(0.0, Double(-0.0).NextDouble());
  CHECK_EQ(-0.0, Double(-4e-324).NextDouble());
  CHECK(Double(Double(-0.0).NextDouble()).Sign() > 0);
  CHECK(Double(Double(-4e-324).NextDouble()).Sign() < 0);
  Double d0(-4e-324);
  Double d1(d0.NextDouble());
  Double d2(d1.NextDouble());
  CHECK_EQ(-0.0, d1.value());
  CHECK(d1.Sign() < 0);
  CHECK_EQ(0.0, d2.value());
  CHECK(d2.Sign() > 0);
  CHECK_EQ(4e-324, d2.NextDouble());
  CHECK_EQ(-1.7976931348623157e308, Double(-Double::Infinity()).NextDouble());
  CHECK_EQ(Double::Infinity(),
           Double(UINT64_2PART_C(0x7fefffff, ffffffff)).NextDouble());
}


TEST(PreviousDouble) {
  CHECK_EQ(0.0, Double(4e-324).PreviousDouble());
  CHECK_EQ(-0.0, Double(0.0).PreviousDouble());
  CHECK(Double(Double(0.0).PreviousDouble()).Sign() < 0);
  CHECK_EQ(-4e-324, Double(-0.0).PreviousDouble());
  Double d0(4e-324);
  Double d1(d0.PreviousDouble());
  Double d2(d1.PreviousDouble());
  CHECK_EQ(0.0, d1.value());
  CHECK(d1.Sign() > 0);
  CHECK_EQ(-0.0, d2.value());
  CHECK(d2.Sign() < 0);
  CHECK_EQ(-4e-324, d2.PreviousDouble());
  CHECK_EQ(1.7976931348623157e308, Double(Double::Infinity()).PreviousDouble());
  CHECK_EQ(-Double::Infinity(),
           Double(UINT64_2PART_C(0xffefffff, ffffffff)).PreviousDouble());
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.