1062 * throwing an exception if the value overflows an {@code int}. 1063 * 1064 * @param value the long value 1065 * @return the argument as an int 1066 * @throws ArithmeticException if the {@code argument} overflows an int 1067 * @since 1.8 1068 */ 1069 public static int toIntExact(long value) { 1070 if ((int)value != value) { 1071 throw new ArithmeticException("integer overflow"); 1072 } 1073 return (int)value; 1074 } 1075 1076 /** 1077 * Returns the exact mathematical product of the arguments. 1078 * 1079 * @param x the first value 1080 * @param y the second value 1081 * @return the result 1082 */ 1083 public static long multiplyFull(int x, int y) { 1084 return (long)x * (long)y; 1085 } 1086 1087 /** 1088 * Returns as a {@code long} the most significant 64 bits of the 128-bit 1089 * product of two 64-bit factors. 1090 * 1091 * @param x the first value 1092 * @param y the second value 1093 * @return the result 1094 */ 1095 public static long multiplyHigh(long x, long y) { 1096 if (x < 0 || y < 0) { 1097 // Use technique from section 8-2 of Henry S. Warren, Jr., 1098 // Hacker's Delight (2nd ed.) (Addison Wesley, 2013), 173-174. 1099 long x1 = x >> 32; 1100 long x2 = x & 0xFFFFFFFFL; 1101 long y1 = y >> 32; 1102 long y2 = y & 0xFFFFFFFFL; 1103 long z2 = x2 * y2; 1104 long t = x1 * y2 + (z2 >>> 32); 1105 long z1 = t & 0xFFFFFFFFL; 1106 long z0 = t >> 32; 1107 z1 += x2 * y1; 1108 return x1 * y1 + z0 + (z1 >> 32); 1109 } else { 1110 // Use Karatsuba technique with two base 2^32 digits. 1111 long x1 = x >>> 32; 1112 long y1 = y >>> 32; 1113 long x2 = x & 0xFFFFFFFFL; | 1062 * throwing an exception if the value overflows an {@code int}. 1063 * 1064 * @param value the long value 1065 * @return the argument as an int 1066 * @throws ArithmeticException if the {@code argument} overflows an int 1067 * @since 1.8 1068 */ 1069 public static int toIntExact(long value) { 1070 if ((int)value != value) { 1071 throw new ArithmeticException("integer overflow"); 1072 } 1073 return (int)value; 1074 } 1075 1076 /** 1077 * Returns the exact mathematical product of the arguments. 1078 * 1079 * @param x the first value 1080 * @param y the second value 1081 * @return the result 1082 * @since 9 1083 */ 1084 public static long multiplyFull(int x, int y) { 1085 return (long)x * (long)y; 1086 } 1087 1088 /** 1089 * Returns as a {@code long} the most significant 64 bits of the 128-bit 1090 * product of two 64-bit factors. 1091 * 1092 * @param x the first value 1093 * @param y the second value 1094 * @return the result 1095 * @since 9 1096 */ 1097 public static long multiplyHigh(long x, long y) { 1098 if (x < 0 || y < 0) { 1099 // Use technique from section 8-2 of Henry S. Warren, Jr., 1100 // Hacker's Delight (2nd ed.) (Addison Wesley, 2013), 173-174. 1101 long x1 = x >> 32; 1102 long x2 = x & 0xFFFFFFFFL; 1103 long y1 = y >> 32; 1104 long y2 = y & 0xFFFFFFFFL; 1105 long z2 = x2 * y2; 1106 long t = x1 * y2 + (z2 >>> 32); 1107 long z1 = t & 0xFFFFFFFFL; 1108 long z0 = t >> 32; 1109 z1 += x2 * y1; 1110 return x1 * y1 + z0 + (z1 >> 32); 1111 } else { 1112 // Use Karatsuba technique with two base 2^32 digits. 1113 long x1 = x >>> 32; 1114 long y1 = y >>> 32; 1115 long x2 = x & 0xFFFFFFFFL; |