+/EE360P

distributed systems

git clone https://git.julia.blue/+/EE360P

vijaygarg1init07ca65d

master
3.0 KiB102 linesraw
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package org.apache.hadoop.examples.pi.math;
19
20import java.math.BigInteger;
21
22/** Support 124-bit integer arithmetic. */
23class LongLong {
24  static final int BITS_PER_LONG = 62;
25  static final int MID = BITS_PER_LONG >> 1;
26  static final int SIZE = BITS_PER_LONG << 1;
27
28  static final long FULL_MASK = (1L << BITS_PER_LONG) - 1;
29  static final long LOWER_MASK = FULL_MASK >>> MID;
30  static final long UPPER_MASK = LOWER_MASK << MID;
31
32  private long d0;
33  private long d1;
34
35  /** Set the values. */
36  LongLong set(long d0, long d1) {
37    this.d0 = d0;
38    this.d1 = d1;
39    return this;
40  }
41
42  /** And operation (&). */
43  long and(long mask) {
44    return d0 & mask;
45  }
46
47  /** Shift right operation (<<). */
48  long shiftRight(int n) {
49    return (d1 << (BITS_PER_LONG - n)) + (d0 >>> n);
50  }
51
52  /** Plus equal operation (+=). */
53  LongLong plusEqual(LongLong that) {
54    this.d0 += that.d0;
55    this.d1 += that.d1;
56    return this;
57  }
58
59  /** Convert this to a BigInteger. */
60  BigInteger toBigInteger() {
61    return BigInteger.valueOf(d1).shiftLeft(BITS_PER_LONG).add(BigInteger.valueOf(d0));
62  }
63
64  /** {@inheritDoc} */
65  @Override
66  public String toString() {
67    final int remainder = BITS_PER_LONG % 4;
68    return String.format("%x*2^%d + %016x", d1<<remainder, BITS_PER_LONG-remainder, d0);
69  }
70
71  /** Compute a*b and store the result to r.
72   * @return r
73   */
74  static LongLong multiplication(final LongLong r, final long a, final long b) {
75    /*
76    final long x0 = a & LOWER_MASK;
77    final long x1 = (a & UPPER_MASK) >> MID;
78
79    final long y0 = b & LOWER_MASK;
80    final long y1 = (b & UPPER_MASK) >> MID;
81
82    final long t = (x0 + x1)*(y0 + y1);
83    final long u = (x0 - x1)*(y0 - y1);
84    final long v = x1*y1;
85
86    final long tmp = (t - u)>>>1;
87    result.d0 = ((t + u)>>>1) - v + ((tmp << MID) & FULL_MASK);;
88    result.d1 = v + (tmp >> MID);
89    return result;
90    */
91    final long a_lower = a & LOWER_MASK;
92    final long a_upper = (a & UPPER_MASK) >> MID;
93
94    final long b_lower = b & LOWER_MASK;
95    final long b_upper = (b & UPPER_MASK) >> MID;
96
97    final long tmp = a_lower*b_upper + a_upper*b_lower;
98    r.d0 = a_lower*b_lower + ((tmp << MID) & FULL_MASK);
99    r.d1 = a_upper*b_upper + (tmp >> MID);
100    return r;
101  }
102}