v8  3.14.5(node0.10.28)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test-diy-fp.cc
Go to the documentation of this file.
1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 
3 #include <stdlib.h>
4 
5 #include "v8.h"
6 
7 #include "platform.h"
8 #include "cctest.h"
9 #include "diy-fp.h"
10 
11 
12 using namespace v8::internal;
13 
14 
15 TEST(Subtract) {
16  DiyFp diy_fp1 = DiyFp(3, 0);
17  DiyFp diy_fp2 = DiyFp(1, 0);
18  DiyFp diff = DiyFp::Minus(diy_fp1, diy_fp2);
19 
20  CHECK(2 == diff.f()); // NOLINT
21  CHECK_EQ(0, diff.e());
22  diy_fp1.Subtract(diy_fp2);
23  CHECK(2 == diy_fp1.f()); // NOLINT
24  CHECK_EQ(0, diy_fp1.e());
25 }
26 
27 
28 TEST(Multiply) {
29  DiyFp diy_fp1 = DiyFp(3, 0);
30  DiyFp diy_fp2 = DiyFp(2, 0);
31  DiyFp product = DiyFp::Times(diy_fp1, diy_fp2);
32 
33  CHECK(0 == product.f()); // NOLINT
34  CHECK_EQ(64, product.e());
35  diy_fp1.Multiply(diy_fp2);
36  CHECK(0 == diy_fp1.f()); // NOLINT
37  CHECK_EQ(64, diy_fp1.e());
38 
39  diy_fp1 = DiyFp(V8_2PART_UINT64_C(0x80000000, 00000000), 11);
40  diy_fp2 = DiyFp(2, 13);
41  product = DiyFp::Times(diy_fp1, diy_fp2);
42  CHECK(1 == product.f()); // NOLINT
43  CHECK_EQ(11 + 13 + 64, product.e());
44 
45  // Test rounding.
46  diy_fp1 = DiyFp(V8_2PART_UINT64_C(0x80000000, 00000001), 11);
47  diy_fp2 = DiyFp(1, 13);
48  product = DiyFp::Times(diy_fp1, diy_fp2);
49  CHECK(1 == product.f()); // NOLINT
50  CHECK_EQ(11 + 13 + 64, product.e());
51 
52  diy_fp1 = DiyFp(V8_2PART_UINT64_C(0x7fffffff, ffffffff), 11);
53  diy_fp2 = DiyFp(1, 13);
54  product = DiyFp::Times(diy_fp1, diy_fp2);
55  CHECK(0 == product.f()); // NOLINT
56  CHECK_EQ(11 + 13 + 64, product.e());
57 
58  // Halfway cases are allowed to round either way. So don't check for it.
59 
60  // Big numbers.
61  diy_fp1 = DiyFp(V8_2PART_UINT64_C(0xFFFFFFFF, FFFFFFFF), 11);
62  diy_fp2 = DiyFp(V8_2PART_UINT64_C(0xFFFFFFFF, FFFFFFFF), 13);
63  // 128bit result: 0xfffffffffffffffe0000000000000001
64  product = DiyFp::Times(diy_fp1, diy_fp2);
65  CHECK(V8_2PART_UINT64_C(0xFFFFFFFF, FFFFFFFe) == product.f());
66  CHECK_EQ(11 + 13 + 64, product.e());
67 }
#define CHECK_EQ(expected, value)
Definition: checks.h:219
static DiyFp Minus(const DiyFp &a, const DiyFp &b)
Definition: diy-fp.h:59
uint64_t f() const
Definition: diy-fp.h:102
void Subtract(const DiyFp &other)
Definition: diy-fp.h:50
#define CHECK(condition)
Definition: checks.h:56
void Multiply(const DiyFp &other)
Definition: diy-fp.cc:36
int e() const
Definition: diy-fp.h:103
#define V8_2PART_UINT64_C(a, b)
Definition: globals.h:187
static DiyFp Times(const DiyFp &a, const DiyFp &b)
Definition: diy-fp.h:70