13fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch// Copyright 2011 the V8 project authors. All rights reserved.
2402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// Redistribution and use in source and binary forms, with or without
3402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// modification, are permitted provided that the following conditions are
4402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// met:
5402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//
6402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//     * Redistributions of source code must retain the above copyright
7402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//       notice, this list of conditions and the following disclaimer.
8402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//     * Redistributions in binary form must reproduce the above
9402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//       copyright notice, this list of conditions and the following
10402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//       disclaimer in the documentation and/or other materials provided
11402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//       with the distribution.
12402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//     * Neither the name of Google Inc. nor the names of its
13402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//       contributors may be used to endorse or promote products derived
14402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//       from this software without specific prior written permission.
15402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//
16402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu
283fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch#include "../include/v8stdint.h"
293fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch#include "globals.h"
303fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch#include "checks.h"
316ded16be15dd865a9b21ea304d5273c8be299c87Steve Block#include "diy-fp.h"
32402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu
33402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescunamespace v8 {
34402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescunamespace internal {
35402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu
366ded16be15dd865a9b21ea304d5273c8be299c87Steve Blockvoid DiyFp::Multiply(const DiyFp& other) {
376ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  // Simply "emulates" a 128 bit multiplication.
386ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  // However: the resulting number only contains 64 bits. The least
396ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  // significant 64 bits are only used for rounding the most significant 64
406ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  // bits.
416ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  const uint64_t kM32 = 0xFFFFFFFFu;
426ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  uint64_t a = f_ >> 32;
436ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  uint64_t b = f_ & kM32;
446ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  uint64_t c = other.f_ >> 32;
456ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  uint64_t d = other.f_ & kM32;
466ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  uint64_t ac = a * c;
476ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  uint64_t bc = b * c;
486ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  uint64_t ad = a * d;
496ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  uint64_t bd = b * d;
506ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  uint64_t tmp = (bd >> 32) + (ad & kM32) + (bc & kM32);
516ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  // By adding 1U << 31 to tmp we round the final result.
526ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  // Halfway cases will be round up.
536ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  tmp += 1U << 31;
546ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  uint64_t result_f = ac + (ad >> 32) + (bc >> 32) + (tmp >> 32);
556ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  e_ += other.e_ + 64;
566ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  f_ = result_f;
576ded16be15dd865a9b21ea304d5273c8be299c87Steve Block}
58402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu
59402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu} }  // namespace v8::internal
60