1 9 package org.jscience.mathematics.numbers; 10 11 17 final class Calculus { 18 19 22 private Calculus() { 23 } 24 25 static final long MASK_63 = 0x7FFFFFFFFFFFFFFFL; 26 27 static final long MASK_32 = 0xFFFFFFFFL; 28 29 static final long MASK_31 = 0x7FFFFFFFL; 30 31 static final long MASK_8 = 0xFFL; 32 33 37 static int add(long[] x, int xSize, long[] y, int ySize, long[] z) { 38 long sum = 0; 39 int i = 0; 40 while (i < ySize) { 41 sum += x[i] + y[i]; 42 z[i++] = sum & MASK_63; 43 sum >>>= 63; 44 } 45 while (true) { 46 if (sum == 0) { 47 while (i < xSize) { 48 z[i] = x[i++]; 49 } 50 return xSize; 51 } 52 if (i == xSize) { 53 z[xSize] = sum; 54 return xSize + 1; 55 } 56 sum += x[i]; 57 z[i++] = sum & MASK_63; 58 sum >>>= 63; 59 } 60 } 61 62 66 static int subtract(long[] x, int xSize, long[] y, int ySize, 67 long[] z) { 68 long diff = 0; 69 int i = 0; 70 while (i < ySize) { 71 diff += x[i] - y[i]; 72 z[i++] = diff & MASK_63; 73 diff >>= 63; } 75 while (diff != 0) { 76 diff += x[i]; 77 z[i++] = diff & MASK_63; 78 diff >>= 63; } 80 while (i < xSize) { 82 z[i] = x[i++]; 83 } 84 for (int j=xSize; j > 0;) { 86 if (z[--j] != 0) return j + 1; 87 } 88 return 0; 89 } 90 91 95 static int compare(long[] left, long[] right, int size) { 96 for (int i = size; --i >= 0;) { 97 if (left[i] > right[i]) 98 return 1; 99 if (left[i] < right[i]) 100 return -1; 101 } 102 return 0; 103 } 104 105 109 static int shiftLeft(int wordShift, int bitShift, long[] x, 110 int xSize, long[] z) { 111 final int shiftRight = 63 - bitShift; 112 int i = xSize; 113 int j = xSize + wordShift; 114 long tmp = x[--i]; 115 long high = tmp >>> shiftRight; 116 if (high != 0) { 117 z[j] = high; 118 } 119 while (i > 0) { 120 z[--j] = ((tmp << bitShift) & MASK_63) 121 | ((tmp = x[--i]) >>> shiftRight); 122 } 123 z[--j] = (tmp << bitShift) & MASK_63; 124 while (j > 0) { 125 z[--j] = 0; 126 } 127 return (high != 0) ? xSize + wordShift + 1 : xSize 128 + wordShift; 129 } 130 131 135 static int shiftRight(int wordShift, int bitShift, long[] x, 136 int xSize, long[] z) { 137 final int shiftLeft = 63 - bitShift; 138 int i = wordShift; 139 int j = 0; 140 long tmp = x[i]; 141 while (i < xSize - 1) { 142 z[j++] = (tmp >>> bitShift) | ((tmp = x[++i]) << shiftLeft) 143 & MASK_63; 144 } 145 tmp >>>= bitShift; 146 z[j] = tmp; 147 return (tmp != 0) ? j + 1 : j; 148 } 149 150 154 static int multiply(long[] x, int xSize, long y, long[] z) { 155 return multiply(x, xSize, y, z, 0); 156 } 157 158 162 static int multiply(long[] x, int xSize, long[] y, int ySize, long[] z) { 163 int zSize = 0; 164 for (int i = 0; i < ySize;) { 165 zSize = multiply(x, xSize, y[i], z, i++); 166 } 167 return zSize; 168 } 169 170 private static int multiply(long[] x, int xSize, long k, long[] z, 172 int shift) { 173 174 final long kl = k & MASK_32; final long kh = k >> 32; 177 long carry = 0; for (int i = 0, j = shift; i < xSize;) { 179 180 long zz = (shift == 0) ? carry : z[j] + carry; carry = zz >>> 63; 183 zz &= MASK_63; 185 final long w = x[i++]; 187 final long wl = w & MASK_32; final long wh = w >> 32; 190 long tmp = wl * kl; carry += tmp >>> 63; 193 zz += tmp & MASK_63; carry += zz >>> 63; 195 zz &= MASK_63; 196 197 tmp = wl * kh + wh * kl; carry += tmp >>> 31; 200 zz += (tmp << 32) & MASK_63; carry += zz >>> 63; 202 z[j++] = zz & MASK_63; 203 204 carry += (wh * kh) << 1; 206 207 } 208 int size = shift + xSize; 209 z[size] = carry; 210 if (carry == 0) return size; 211 return ++size; 212 } 213 214 218 static long divide(long[] x, int xSize, int y, long[] z) { 219 long r = 0; 220 for (int i = xSize; i > 0;) { 221 long w = x[--i]; 222 223 long wh = (r << 31) | (w >>> 32); 224 long qh = wh / y; 225 r = wh - qh * y; 226 227 long wl = (r << 32) | (w & MASK_32); 228 long ql = wl / y; 229 r = wl - ql * y; 230 231 z[i] = (qh << 32) | ql; 232 } 233 return r; 234 } 235 236 } | Popular Tags |