< prev index next >

src/java.base/share/classes/java/math/BigDecimal.java

Print this page
rev 17358 : 8182487: Add Unsafe.objectFieldOffset(Class, String)
Reviewed-by: dsimms, twisti, bchristi, mgerdin


4049      * least significant digits.
4050      *
4051      * <p>If the scales of val[0] and val[1] differ, rescale
4052      * (non-destructively) the lower-scaled {@code BigDecimal} so
4053      * they match.  That is, the lower-scaled reference will be
4054      * replaced by a reference to a new object with the same scale as
4055      * the other {@code BigDecimal}.
4056      *
4057      * @param  val array of two elements referring to the two
4058      *         {@code BigDecimal}s to be aligned.
4059      */
4060     private static void matchScale(BigDecimal[] val) {
4061         if (val[0].scale < val[1].scale) {
4062             val[0] = val[0].setScale(val[1].scale, ROUND_UNNECESSARY);
4063         } else if (val[1].scale < val[0].scale) {
4064             val[1] = val[1].setScale(val[0].scale, ROUND_UNNECESSARY);
4065         }
4066     }
4067 
4068     private static class UnsafeHolder {
4069         private static final jdk.internal.misc.Unsafe unsafe;
4070         private static final long intCompactOffset;
4071         private static final long intValOffset;
4072         static {
4073             try {
4074                 unsafe = jdk.internal.misc.Unsafe.getUnsafe();
4075                 intCompactOffset = unsafe.objectFieldOffset
4076                     (BigDecimal.class.getDeclaredField("intCompact"));
4077                 intValOffset = unsafe.objectFieldOffset
4078                     (BigDecimal.class.getDeclaredField("intVal"));
4079             } catch (Exception ex) {
4080                 throw new ExceptionInInitializerError(ex);
4081             }
4082         }
4083         static void setIntCompact(BigDecimal bd, long val) {
4084             unsafe.putLong(bd, intCompactOffset, val);
4085         }
4086 
4087         static void setIntValVolatile(BigDecimal bd, BigInteger val) {
4088             unsafe.putObjectVolatile(bd, intValOffset, val);
4089         }
4090     }
4091 
4092     /**
4093      * Reconstitute the {@code BigDecimal} instance from a stream (that is,
4094      * deserialize it).
4095      *
4096      * @param s the stream being read.
4097      */
4098     private void readObject(java.io.ObjectInputStream s)
4099         throws java.io.IOException, ClassNotFoundException {
4100         // Read in all fields
4101         s.defaultReadObject();
4102         // validate possibly bad fields




4049      * least significant digits.
4050      *
4051      * <p>If the scales of val[0] and val[1] differ, rescale
4052      * (non-destructively) the lower-scaled {@code BigDecimal} so
4053      * they match.  That is, the lower-scaled reference will be
4054      * replaced by a reference to a new object with the same scale as
4055      * the other {@code BigDecimal}.
4056      *
4057      * @param  val array of two elements referring to the two
4058      *         {@code BigDecimal}s to be aligned.
4059      */
4060     private static void matchScale(BigDecimal[] val) {
4061         if (val[0].scale < val[1].scale) {
4062             val[0] = val[0].setScale(val[1].scale, ROUND_UNNECESSARY);
4063         } else if (val[1].scale < val[0].scale) {
4064             val[1] = val[1].setScale(val[0].scale, ROUND_UNNECESSARY);
4065         }
4066     }
4067 
4068     private static class UnsafeHolder {
4069         private static final jdk.internal.misc.Unsafe unsafe
4070                 = jdk.internal.misc.Unsafe.getUnsafe();
4071         private static final long intCompactOffset
4072                 = unsafe.objectFieldOffset(BigDecimal.class, "intCompact");
4073         private static final long intValOffset
4074                 = unsafe.objectFieldOffset(BigDecimal.class, "intVal");
4075 







4076         static void setIntCompact(BigDecimal bd, long val) {
4077             unsafe.putLong(bd, intCompactOffset, val);
4078         }
4079 
4080         static void setIntValVolatile(BigDecimal bd, BigInteger val) {
4081             unsafe.putObjectVolatile(bd, intValOffset, val);
4082         }
4083     }
4084 
4085     /**
4086      * Reconstitute the {@code BigDecimal} instance from a stream (that is,
4087      * deserialize it).
4088      *
4089      * @param s the stream being read.
4090      */
4091     private void readObject(java.io.ObjectInputStream s)
4092         throws java.io.IOException, ClassNotFoundException {
4093         // Read in all fields
4094         s.defaultReadObject();
4095         // validate possibly bad fields


< prev index next >