jdk/test/sun/misc/FloatingDecimal/OldFDBigIntForTest.java
author bpb
Wed, 05 Jun 2013 21:01:02 -0700
changeset 18143 b6ef7bd945ce
child 23010 6dadb192ad81
permissions -rw-r--r--
7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal Summary: Performance improvements for double/float -> String and decimal/hex String -> double/float conversions. Reviewed-by: martin, iris Contributed-by: Sergey Kuksenko <sergey.kuksenko@oracle.com>, Brian Burkhalter <brian.burkhalter@oracle.com>, Dmitry Nadezhin <dmitry.nadezhin@oracle.com>, Olivier Lagneau <olivier.lagneau@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18143
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
     1
/*
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
     2
 * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
     4
 *
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
     8
 *
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    13
 * accompanied this code).
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    14
 *
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    18
 *
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    21
 * questions.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    22
 */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    23
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    24
//package sun.misc;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    25
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    26
/*
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    27
 * A really, really simple bigint package
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    28
 * tailored to the needs of floating base conversion.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    29
 */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    30
class OldFDBigIntForTest {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    31
    int nWords; // number of words used
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    32
    int data[]; // value: data[0] is least significant
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    33
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    34
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    35
    public OldFDBigIntForTest( int v ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    36
        nWords = 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    37
        data = new int[1];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    38
        data[0] = v;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    39
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    40
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    41
    public OldFDBigIntForTest( long v ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    42
        data = new int[2];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    43
        data[0] = (int)v;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    44
        data[1] = (int)(v>>>32);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    45
        nWords = (data[1]==0) ? 1 : 2;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    46
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    47
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    48
    public OldFDBigIntForTest( OldFDBigIntForTest other ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    49
        data = new int[nWords = other.nWords];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    50
        System.arraycopy( other.data, 0, data, 0, nWords );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    51
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    52
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    53
    private OldFDBigIntForTest( int [] d, int n ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    54
        data = d;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    55
        nWords = n;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    56
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    57
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    58
    public OldFDBigIntForTest( long seed, char digit[], int nd0, int nd ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    59
        int n= (nd+8)/9;        // estimate size needed.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    60
        if ( n < 2 ) n = 2;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    61
        data = new int[n];      // allocate enough space
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    62
        data[0] = (int)seed;    // starting value
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    63
        data[1] = (int)(seed>>>32);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    64
        nWords = (data[1]==0) ? 1 : 2;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    65
        int i = nd0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    66
        int limit = nd-5;       // slurp digits 5 at a time.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    67
        int v;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    68
        while ( i < limit ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    69
            int ilim = i+5;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    70
            v = (int)digit[i++]-(int)'0';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    71
            while( i <ilim ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    72
                v = 10*v + (int)digit[i++]-(int)'0';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    73
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    74
            multaddMe( 100000, v); // ... where 100000 is 10^5.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    75
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    76
        int factor = 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    77
        v = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    78
        while ( i < nd ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    79
            v = 10*v + (int)digit[i++]-(int)'0';
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    80
            factor *= 10;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    81
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    82
        if ( factor != 1 ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    83
            multaddMe( factor, v );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    84
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    85
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    86
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    87
    /*
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    88
     * Left shift by c bits.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    89
     * Shifts this in place.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    90
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    91
    public void
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    92
    lshiftMe( int c )throws IllegalArgumentException {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    93
        if ( c <= 0 ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    94
            if ( c == 0 )
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    95
                return; // silly.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    96
            else
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    97
                throw new IllegalArgumentException("negative shift count");
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    98
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
    99
        int wordcount = c>>5;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   100
        int bitcount  = c & 0x1f;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   101
        int anticount = 32-bitcount;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   102
        int t[] = data;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   103
        int s[] = data;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   104
        if ( nWords+wordcount+1 > t.length ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   105
            // reallocate.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   106
            t = new int[ nWords+wordcount+1 ];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   107
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   108
        int target = nWords+wordcount;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   109
        int src    = nWords-1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   110
        if ( bitcount == 0 ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   111
            // special hack, since an anticount of 32 won't go!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   112
            System.arraycopy( s, 0, t, wordcount, nWords );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   113
            target = wordcount-1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   114
        } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   115
            t[target--] = s[src]>>>anticount;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   116
            while ( src >= 1 ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   117
                t[target--] = (s[src]<<bitcount) | (s[--src]>>>anticount);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   118
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   119
            t[target--] = s[src]<<bitcount;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   120
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   121
        while( target >= 0 ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   122
            t[target--] = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   123
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   124
        data = t;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   125
        nWords += wordcount + 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   126
        // may have constructed high-order word of 0.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   127
        // if so, trim it
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   128
        while ( nWords > 1 && data[nWords-1] == 0 )
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   129
            nWords--;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   130
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   131
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   132
    /*
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   133
     * normalize this number by shifting until
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   134
     * the MSB of the number is at 0x08000000.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   135
     * This is in preparation for quoRemIteration, below.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   136
     * The idea is that, to make division easier, we want the
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   137
     * divisor to be "normalized" -- usually this means shifting
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   138
     * the MSB into the high words sign bit. But because we know that
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   139
     * the quotient will be 0 < q < 10, we would like to arrange that
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   140
     * the dividend not span up into another word of precision.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   141
     * (This needs to be explained more clearly!)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   142
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   143
    public int
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   144
    normalizeMe() throws IllegalArgumentException {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   145
        int src;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   146
        int wordcount = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   147
        int bitcount  = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   148
        int v = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   149
        for ( src= nWords-1 ; src >= 0 && (v=data[src]) == 0 ; src--){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   150
            wordcount += 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   151
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   152
        if ( src < 0 ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   153
            // oops. Value is zero. Cannot normalize it!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   154
            throw new IllegalArgumentException("zero value");
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   155
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   156
        /*
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   157
         * In most cases, we assume that wordcount is zero. This only
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   158
         * makes sense, as we try not to maintain any high-order
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   159
         * words full of zeros. In fact, if there are zeros, we will
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   160
         * simply SHORTEN our number at this point. Watch closely...
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   161
         */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   162
        nWords -= wordcount;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   163
        /*
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   164
         * Compute how far left we have to shift v s.t. its highest-
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   165
         * order bit is in the right place. Then call lshiftMe to
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   166
         * do the work.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   167
         */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   168
        if ( (v & 0xf0000000) != 0 ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   169
            // will have to shift up into the next word.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   170
            // too bad.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   171
            for( bitcount = 32 ; (v & 0xf0000000) != 0 ; bitcount-- )
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   172
                v >>>= 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   173
        } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   174
            while ( v <= 0x000fffff ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   175
                // hack: byte-at-a-time shifting
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   176
                v <<= 8;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   177
                bitcount += 8;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   178
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   179
            while ( v <= 0x07ffffff ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   180
                v <<= 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   181
                bitcount += 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   182
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   183
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   184
        if ( bitcount != 0 )
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   185
            lshiftMe( bitcount );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   186
        return bitcount;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   187
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   188
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   189
    /*
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   190
     * Multiply a OldFDBigIntForTest by an int.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   191
     * Result is a new OldFDBigIntForTest.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   192
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   193
    public OldFDBigIntForTest
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   194
    mult( int iv ) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   195
        long v = iv;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   196
        int r[];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   197
        long p;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   198
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   199
        // guess adequate size of r.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   200
        r = new int[ ( v * ((long)data[nWords-1]&0xffffffffL) > 0xfffffffL ) ? nWords+1 : nWords ];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   201
        p = 0L;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   202
        for( int i=0; i < nWords; i++ ) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   203
            p += v * ((long)data[i]&0xffffffffL);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   204
            r[i] = (int)p;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   205
            p >>>= 32;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   206
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   207
        if ( p == 0L){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   208
            return new OldFDBigIntForTest( r, nWords );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   209
        } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   210
            r[nWords] = (int)p;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   211
            return new OldFDBigIntForTest( r, nWords+1 );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   212
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   213
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   214
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   215
    /*
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   216
     * Multiply a OldFDBigIntForTest by an int and add another int.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   217
     * Result is computed in place.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   218
     * Hope it fits!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   219
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   220
    public void
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   221
    multaddMe( int iv, int addend ) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   222
        long v = iv;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   223
        long p;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   224
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   225
        // unroll 0th iteration, doing addition.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   226
        p = v * ((long)data[0]&0xffffffffL) + ((long)addend&0xffffffffL);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   227
        data[0] = (int)p;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   228
        p >>>= 32;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   229
        for( int i=1; i < nWords; i++ ) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   230
            p += v * ((long)data[i]&0xffffffffL);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   231
            data[i] = (int)p;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   232
            p >>>= 32;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   233
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   234
        if ( p != 0L){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   235
            data[nWords] = (int)p; // will fail noisily if illegal!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   236
            nWords++;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   237
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   238
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   239
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   240
    /*
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   241
     * Multiply a OldFDBigIntForTest by another OldFDBigIntForTest.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   242
     * Result is a new OldFDBigIntForTest.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   243
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   244
    public OldFDBigIntForTest
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   245
    mult( OldFDBigIntForTest other ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   246
        // crudely guess adequate size for r
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   247
        int r[] = new int[ nWords + other.nWords ];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   248
        int i;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   249
        // I think I am promised zeros...
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   250
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   251
        for( i = 0; i < this.nWords; i++ ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   252
            long v = (long)this.data[i] & 0xffffffffL; // UNSIGNED CONVERSION
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   253
            long p = 0L;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   254
            int j;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   255
            for( j = 0; j < other.nWords; j++ ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   256
                p += ((long)r[i+j]&0xffffffffL) + v*((long)other.data[j]&0xffffffffL); // UNSIGNED CONVERSIONS ALL 'ROUND.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   257
                r[i+j] = (int)p;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   258
                p >>>= 32;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   259
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   260
            r[i+j] = (int)p;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   261
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   262
        // compute how much of r we actually needed for all that.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   263
        for ( i = r.length-1; i> 0; i--)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   264
            if ( r[i] != 0 )
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   265
                break;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   266
        return new OldFDBigIntForTest( r, i+1 );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   267
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   268
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   269
    /*
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   270
     * Add one OldFDBigIntForTest to another. Return a OldFDBigIntForTest
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   271
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   272
    public OldFDBigIntForTest
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   273
    add( OldFDBigIntForTest other ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   274
        int i;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   275
        int a[], b[];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   276
        int n, m;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   277
        long c = 0L;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   278
        // arrange such that a.nWords >= b.nWords;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   279
        // n = a.nWords, m = b.nWords
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   280
        if ( this.nWords >= other.nWords ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   281
            a = this.data;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   282
            n = this.nWords;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   283
            b = other.data;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   284
            m = other.nWords;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   285
        } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   286
            a = other.data;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   287
            n = other.nWords;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   288
            b = this.data;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   289
            m = this.nWords;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   290
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   291
        int r[] = new int[ n ];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   292
        for ( i = 0; i < n; i++ ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   293
            c += (long)a[i] & 0xffffffffL;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   294
            if ( i < m ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   295
                c += (long)b[i] & 0xffffffffL;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   296
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   297
            r[i] = (int) c;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   298
            c >>= 32; // signed shift.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   299
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   300
        if ( c != 0L ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   301
            // oops -- carry out -- need longer result.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   302
            int s[] = new int[ r.length+1 ];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   303
            System.arraycopy( r, 0, s, 0, r.length );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   304
            s[i++] = (int)c;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   305
            return new OldFDBigIntForTest( s, i );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   306
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   307
        return new OldFDBigIntForTest( r, i );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   308
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   309
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   310
    /*
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   311
     * Subtract one OldFDBigIntForTest from another. Return a OldFDBigIntForTest
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   312
     * Assert that the result is positive.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   313
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   314
    public OldFDBigIntForTest
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   315
    sub( OldFDBigIntForTest other ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   316
        int r[] = new int[ this.nWords ];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   317
        int i;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   318
        int n = this.nWords;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   319
        int m = other.nWords;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   320
        int nzeros = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   321
        long c = 0L;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   322
        for ( i = 0; i < n; i++ ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   323
            c += (long)this.data[i] & 0xffffffffL;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   324
            if ( i < m ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   325
                c -= (long)other.data[i] & 0xffffffffL;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   326
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   327
            if ( ( r[i] = (int) c ) == 0 )
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   328
                nzeros++;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   329
            else
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   330
                nzeros = 0;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   331
            c >>= 32; // signed shift
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   332
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   333
        assert c == 0L : c; // borrow out of subtract
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   334
        assert dataInRangeIsZero(i, m, other); // negative result of subtract
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   335
        return new OldFDBigIntForTest( r, n-nzeros );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   336
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   337
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   338
    private static boolean dataInRangeIsZero(int i, int m, OldFDBigIntForTest other) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   339
        while ( i < m )
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   340
            if (other.data[i++] != 0)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   341
                return false;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   342
        return true;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   343
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   344
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   345
    /*
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   346
     * Compare OldFDBigIntForTest with another OldFDBigIntForTest. Return an integer
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   347
     * >0: this > other
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   348
     *  0: this == other
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   349
     * <0: this < other
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   350
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   351
    public int
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   352
    cmp( OldFDBigIntForTest other ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   353
        int i;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   354
        if ( this.nWords > other.nWords ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   355
            // if any of my high-order words is non-zero,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   356
            // then the answer is evident
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   357
            int j = other.nWords-1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   358
            for ( i = this.nWords-1; i > j ; i-- )
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   359
                if ( this.data[i] != 0 ) return 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   360
        }else if ( this.nWords < other.nWords ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   361
            // if any of other's high-order words is non-zero,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   362
            // then the answer is evident
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   363
            int j = this.nWords-1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   364
            for ( i = other.nWords-1; i > j ; i-- )
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   365
                if ( other.data[i] != 0 ) return -1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   366
        } else{
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   367
            i = this.nWords-1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   368
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   369
        for ( ; i > 0 ; i-- )
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   370
            if ( this.data[i] != other.data[i] )
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   371
                break;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   372
        // careful! want unsigned compare!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   373
        // use brute force here.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   374
        int a = this.data[i];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   375
        int b = other.data[i];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   376
        if ( a < 0 ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   377
            // a is really big, unsigned
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   378
            if ( b < 0 ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   379
                return a-b; // both big, negative
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   380
            } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   381
                return 1; // b not big, answer is obvious;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   382
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   383
        } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   384
            // a is not really big
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   385
            if ( b < 0 ) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   386
                // but b is really big
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   387
                return -1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   388
            } else {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   389
                return a - b;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   390
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   391
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   392
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   393
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   394
    /*
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   395
     * Compute
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   396
     * q = (int)( this / S )
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   397
     * this = 10 * ( this mod S )
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   398
     * Return q.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   399
     * This is the iteration step of digit development for output.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   400
     * We assume that S has been normalized, as above, and that
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   401
     * "this" has been lshift'ed accordingly.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   402
     * Also assume, of course, that the result, q, can be expressed
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   403
     * as an integer, 0 <= q < 10.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   404
     */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   405
    public int
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   406
    quoRemIteration( OldFDBigIntForTest S )throws IllegalArgumentException {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   407
        // ensure that this and S have the same number of
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   408
        // digits. If S is properly normalized and q < 10 then
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   409
        // this must be so.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   410
        if ( nWords != S.nWords ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   411
            throw new IllegalArgumentException("disparate values");
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   412
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   413
        // estimate q the obvious way. We will usually be
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   414
        // right. If not, then we're only off by a little and
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   415
        // will re-add.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   416
        int n = nWords-1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   417
        long q = ((long)data[n]&0xffffffffL) / (long)S.data[n];
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   418
        long diff = 0L;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   419
        for ( int i = 0; i <= n ; i++ ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   420
            diff += ((long)data[i]&0xffffffffL) -  q*((long)S.data[i]&0xffffffffL);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   421
            data[i] = (int)diff;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   422
            diff >>= 32; // N.B. SIGNED shift.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   423
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   424
        if ( diff != 0L ) {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   425
            // damn, damn, damn. q is too big.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   426
            // add S back in until this turns +. This should
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   427
            // not be very many times!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   428
            long sum = 0L;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   429
            while ( sum ==  0L ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   430
                sum = 0L;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   431
                for ( int i = 0; i <= n; i++ ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   432
                    sum += ((long)data[i]&0xffffffffL) +  ((long)S.data[i]&0xffffffffL);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   433
                    data[i] = (int) sum;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   434
                    sum >>= 32; // Signed or unsigned, answer is 0 or 1
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   435
                }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   436
                /*
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   437
                 * Originally the following line read
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   438
                 * "if ( sum !=0 && sum != -1 )"
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   439
                 * but that would be wrong, because of the
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   440
                 * treatment of the two values as entirely unsigned,
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   441
                 * it would be impossible for a carry-out to be interpreted
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   442
                 * as -1 -- it would have to be a single-bit carry-out, or
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   443
                 * +1.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   444
                 */
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   445
                assert sum == 0 || sum == 1 : sum; // carry out of division correction
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   446
                q -= 1;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   447
            }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   448
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   449
        // finally, we can multiply this by 10.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   450
        // it cannot overflow, right, as the high-order word has
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   451
        // at least 4 high-order zeros!
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   452
        long p = 0L;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   453
        for ( int i = 0; i <= n; i++ ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   454
            p += 10*((long)data[i]&0xffffffffL);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   455
            data[i] = (int)p;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   456
            p >>= 32; // SIGNED shift.
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   457
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   458
        assert p == 0L : p; // Carry out of *10
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   459
        return (int)q;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   460
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   461
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   462
    public long
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   463
    longValue(){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   464
        // if this can be represented as a long, return the value
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   465
        assert this.nWords > 0 : this.nWords; // longValue confused
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   466
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   467
        if (this.nWords == 1)
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   468
            return ((long)data[0]&0xffffffffL);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   469
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   470
        assert dataInRangeIsZero(2, this.nWords, this); // value too big
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   471
        assert data[1] >= 0;  // value too big
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   472
        return ((long)(data[1]) << 32) | ((long)data[0]&0xffffffffL);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   473
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   474
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   475
    public String
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   476
    toString() {
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   477
        StringBuffer r = new StringBuffer(30);
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   478
        r.append('[');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   479
        int i = Math.min( nWords-1, data.length-1) ;
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   480
        if ( nWords > data.length ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   481
            r.append( "("+data.length+"<"+nWords+"!)" );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   482
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   483
        for( ; i> 0 ; i-- ){
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   484
            r.append( Integer.toHexString( data[i] ) );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   485
            r.append(' ');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   486
        }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   487
        r.append( Integer.toHexString( data[0] ) );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   488
        r.append(']');
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   489
        return new String( r );
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   490
    }
b6ef7bd945ce 7032154: Performance tuning of sun.misc.FloatingDecimal/FormattedFloatingDecimal
bpb
parents:
diff changeset
   491
}