12e5b6d6dSopenharmony_ci// Copyright 2006-2008 the V8 project authors. All rights reserved. 22e5b6d6dSopenharmony_ci// Redistribution and use in source and binary forms, with or without 32e5b6d6dSopenharmony_ci// modification, are permitted provided that the following conditions are 42e5b6d6dSopenharmony_ci// met: 52e5b6d6dSopenharmony_ci// 62e5b6d6dSopenharmony_ci// * Redistributions of source code must retain the above copyright 72e5b6d6dSopenharmony_ci// notice, this list of conditions and the following disclaimer. 82e5b6d6dSopenharmony_ci// * Redistributions in binary form must reproduce the above 92e5b6d6dSopenharmony_ci// copyright notice, this list of conditions and the following 102e5b6d6dSopenharmony_ci// disclaimer in the documentation and/or other materials provided 112e5b6d6dSopenharmony_ci// with the distribution. 122e5b6d6dSopenharmony_ci// * Neither the name of Google Inc. nor the names of its 132e5b6d6dSopenharmony_ci// contributors may be used to endorse or promote products derived 142e5b6d6dSopenharmony_ci// from this software without specific prior written permission. 152e5b6d6dSopenharmony_ci// 162e5b6d6dSopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172e5b6d6dSopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182e5b6d6dSopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192e5b6d6dSopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202e5b6d6dSopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212e5b6d6dSopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222e5b6d6dSopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232e5b6d6dSopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242e5b6d6dSopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252e5b6d6dSopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262e5b6d6dSopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272e5b6d6dSopenharmony_ci 282e5b6d6dSopenharmony_ci#include <stdlib.h> 292e5b6d6dSopenharmony_ci 302e5b6d6dSopenharmony_ci#include "cctest.h" 312e5b6d6dSopenharmony_ci#include "double-conversion/diy-fp.h" 322e5b6d6dSopenharmony_ci#include "double-conversion/utils.h" 332e5b6d6dSopenharmony_ci 342e5b6d6dSopenharmony_ci 352e5b6d6dSopenharmony_ciusing namespace double_conversion; 362e5b6d6dSopenharmony_ci 372e5b6d6dSopenharmony_ci 382e5b6d6dSopenharmony_ciTEST(Subtract) { 392e5b6d6dSopenharmony_ci DiyFp diy_fp1 = DiyFp(3, 0); 402e5b6d6dSopenharmony_ci DiyFp diy_fp2 = DiyFp(1, 0); 412e5b6d6dSopenharmony_ci DiyFp diff = DiyFp::Minus(diy_fp1, diy_fp2); 422e5b6d6dSopenharmony_ci 432e5b6d6dSopenharmony_ci CHECK(2 == diff.f()); // NOLINT 442e5b6d6dSopenharmony_ci CHECK_EQ(0, diff.e()); 452e5b6d6dSopenharmony_ci diy_fp1.Subtract(diy_fp2); 462e5b6d6dSopenharmony_ci CHECK(2 == diy_fp1.f()); // NOLINT 472e5b6d6dSopenharmony_ci CHECK_EQ(0, diy_fp1.e()); 482e5b6d6dSopenharmony_ci} 492e5b6d6dSopenharmony_ci 502e5b6d6dSopenharmony_ci 512e5b6d6dSopenharmony_ciTEST(Multiply) { 522e5b6d6dSopenharmony_ci DiyFp diy_fp1 = DiyFp(3, 0); 532e5b6d6dSopenharmony_ci DiyFp diy_fp2 = DiyFp(2, 0); 542e5b6d6dSopenharmony_ci DiyFp product = DiyFp::Times(diy_fp1, diy_fp2); 552e5b6d6dSopenharmony_ci 562e5b6d6dSopenharmony_ci CHECK(0 == product.f()); // NOLINT 572e5b6d6dSopenharmony_ci CHECK_EQ(64, product.e()); 582e5b6d6dSopenharmony_ci diy_fp1.Multiply(diy_fp2); 592e5b6d6dSopenharmony_ci CHECK(0 == diy_fp1.f()); // NOLINT 602e5b6d6dSopenharmony_ci CHECK_EQ(64, diy_fp1.e()); 612e5b6d6dSopenharmony_ci 622e5b6d6dSopenharmony_ci diy_fp1 = DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0x80000000, 00000000), 11); 632e5b6d6dSopenharmony_ci diy_fp2 = DiyFp(2, 13); 642e5b6d6dSopenharmony_ci product = DiyFp::Times(diy_fp1, diy_fp2); 652e5b6d6dSopenharmony_ci CHECK(1 == product.f()); // NOLINT 662e5b6d6dSopenharmony_ci CHECK_EQ(11 + 13 + 64, product.e()); 672e5b6d6dSopenharmony_ci 682e5b6d6dSopenharmony_ci // Test rounding. 692e5b6d6dSopenharmony_ci diy_fp1 = DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0x80000000, 00000001), 11); 702e5b6d6dSopenharmony_ci diy_fp2 = DiyFp(1, 13); 712e5b6d6dSopenharmony_ci product = DiyFp::Times(diy_fp1, diy_fp2); 722e5b6d6dSopenharmony_ci CHECK(1 == product.f()); // NOLINT 732e5b6d6dSopenharmony_ci CHECK_EQ(11 + 13 + 64, product.e()); 742e5b6d6dSopenharmony_ci 752e5b6d6dSopenharmony_ci diy_fp1 = DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0x7fffffff, ffffffff), 11); 762e5b6d6dSopenharmony_ci diy_fp2 = DiyFp(1, 13); 772e5b6d6dSopenharmony_ci product = DiyFp::Times(diy_fp1, diy_fp2); 782e5b6d6dSopenharmony_ci CHECK(0 == product.f()); // NOLINT 792e5b6d6dSopenharmony_ci CHECK_EQ(11 + 13 + 64, product.e()); 802e5b6d6dSopenharmony_ci 812e5b6d6dSopenharmony_ci // Halfway cases are allowed to round either way. So don't check for it. 822e5b6d6dSopenharmony_ci 832e5b6d6dSopenharmony_ci // Big numbers. 842e5b6d6dSopenharmony_ci diy_fp1 = DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF), 11); 852e5b6d6dSopenharmony_ci diy_fp2 = DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF), 13); 862e5b6d6dSopenharmony_ci // 128bit result: 0xfffffffffffffffe0000000000000001 872e5b6d6dSopenharmony_ci product = DiyFp::Times(diy_fp1, diy_fp2); 882e5b6d6dSopenharmony_ci CHECK(DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFFFFFF, FFFFFFFe) == product.f()); 892e5b6d6dSopenharmony_ci CHECK_EQ(11 + 13 + 64, product.e()); 902e5b6d6dSopenharmony_ci} 91