blob: 44a9bb122ec0768410c9a43a0c1cb637b1cb6295 [file] [log] [blame]
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Andrei Popescu402d9372010-02-26 13:31:12 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/diy-fp.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006
7#include <stdint.h>
Andrei Popescu402d9372010-02-26 13:31:12 +00008
9namespace v8 {
10namespace internal {
11
Steve Block6ded16b2010-05-10 14:33:55 +010012void DiyFp::Multiply(const DiyFp& other) {
13 // Simply "emulates" a 128 bit multiplication.
14 // However: the resulting number only contains 64 bits. The least
15 // significant 64 bits are only used for rounding the most significant 64
16 // bits.
17 const uint64_t kM32 = 0xFFFFFFFFu;
18 uint64_t a = f_ >> 32;
19 uint64_t b = f_ & kM32;
20 uint64_t c = other.f_ >> 32;
21 uint64_t d = other.f_ & kM32;
22 uint64_t ac = a * c;
23 uint64_t bc = b * c;
24 uint64_t ad = a * d;
25 uint64_t bd = b * d;
26 uint64_t tmp = (bd >> 32) + (ad & kM32) + (bc & kM32);
27 // By adding 1U << 31 to tmp we round the final result.
28 // Halfway cases will be round up.
29 tmp += 1U << 31;
30 uint64_t result_f = ac + (ad >> 32) + (bc >> 32) + (tmp >> 32);
31 e_ += other.e_ + 64;
32 f_ = result_f;
33}
Andrei Popescu402d9372010-02-26 13:31:12 +000034
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000035} // namespace internal
36} // namespace v8