+/EE360P
distributed systems
git clone https://git.julia.blue/+/EE360P
07ca65d
master
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 { 24static final int BITS_PER_LONG =62 ; 25static final int MID =BITS_PER_LONG >>1 ; 26static final int SIZE =BITS_PER_LONG <<1 ; 27 28static final long FULL_MASK = (1L <<BITS_PER_LONG ) -1 ; 29static final long LOWER_MASK =FULL_MASK >>>MID ; 30static final long UPPER_MASK =LOWER_MASK <<MID ; 31 32private long d0 ; 33private long d1 ; 34 35/** Set the values. */ 36LongLong set (long d0 ,long d1 ) { 37this .d0 =d0 ; 38this .d1 =d1 ; 39return this ; 40 } 41 42/** And operation (&). */ 43long and (long mask ) { 44return d0 &mask ; 45 } 46 47/** Shift right operation (<<). */ 48long shiftRight (int n ) { 49return (d1 << (BITS_PER_LONG -n )) + (d0 >>>n ); 50 } 51 52/** Plus equal operation (+=). */ 53LongLong plusEqual (LongLong that ) { 54this .d0 +=that .d0 ; 55this .d1 +=that .d1 ; 56return this ; 57 } 58 59/** Convert this to a BigInteger. */ 60BigInteger toBigInteger () { 61return BigInteger .valueOf (d1 ).shiftLeft (BITS_PER_LONG ).add (BigInteger .valueOf (d0 )); 62 } 63 64/** {@inheritDoc} */ 65@ Override 66public String toString () { 67final int remainder =BITS_PER_LONG %4 ; 68return 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*/ 74static LongLong multiplication (final LongLong r ,final long a ,final long b ) { 75/* 76final long x0 = a & LOWER_MASK; 77final long x1 = (a & UPPER_MASK) >> MID; 78 79final long y0 = b & LOWER_MASK; 80final long y1 = (b & UPPER_MASK) >> MID; 81 82final long t = (x0 + x1)*(y0 + y1); 83final long u = (x0 - x1)*(y0 - y1); 84final long v = x1*y1; 85 86final long tmp = (t - u)>>>1; 87result.d0 = ((t + u)>>>1) - v + ((tmp << MID) & FULL_MASK);; 88result.d1 = v + (tmp >> MID); 89return result; 90*/ 91final long a_lower =a &LOWER_MASK ; 92final long a_upper = (a &UPPER_MASK ) >>MID ; 93 94final long b_lower =b &LOWER_MASK ; 95final long b_upper = (b &UPPER_MASK ) >>MID ; 96 97final long tmp =a_lower *b_upper +a_upper *b_lower ; 98r .d0 =a_lower *b_lower + ((tmp <<MID ) &FULL_MASK ); 99r .d1 =a_upper *b_upper + (tmp >>MID ); 100return r ; 101 } 102}