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

Print this page
rev 12972 : 8140606: Update library code to use internal Unsafe
Reviewed-by: duke


3709      * least significant digits.
3710      *
3711      * <p>If the scales of val[0] and val[1] differ, rescale
3712      * (non-destructively) the lower-scaled {@code BigDecimal} so
3713      * they match.  That is, the lower-scaled reference will be
3714      * replaced by a reference to a new object with the same scale as
3715      * the other {@code BigDecimal}.
3716      *
3717      * @param  val array of two elements referring to the two
3718      *         {@code BigDecimal}s to be aligned.
3719      */
3720     private static void matchScale(BigDecimal[] val) {
3721         if (val[0].scale < val[1].scale) {
3722             val[0] = val[0].setScale(val[1].scale, ROUND_UNNECESSARY);
3723         } else if (val[1].scale < val[0].scale) {
3724             val[1] = val[1].setScale(val[0].scale, ROUND_UNNECESSARY);
3725         }
3726     }
3727 
3728     private static class UnsafeHolder {
3729         private static final sun.misc.Unsafe unsafe;
3730         private static final long intCompactOffset;
3731         private static final long intValOffset;
3732         static {
3733             try {
3734                 unsafe = sun.misc.Unsafe.getUnsafe();
3735                 intCompactOffset = unsafe.objectFieldOffset
3736                     (BigDecimal.class.getDeclaredField("intCompact"));
3737                 intValOffset = unsafe.objectFieldOffset
3738                     (BigDecimal.class.getDeclaredField("intVal"));
3739             } catch (Exception ex) {
3740                 throw new ExceptionInInitializerError(ex);
3741             }
3742         }
3743         static void setIntCompact(BigDecimal bd, long val) {
3744             unsafe.putLong(bd, intCompactOffset, val);
3745         }
3746 
3747         static void setIntValVolatile(BigDecimal bd, BigInteger val) {
3748             unsafe.putObjectVolatile(bd, intValOffset, val);
3749         }
3750     }
3751 
3752     /**
3753      * Reconstitute the {@code BigDecimal} instance from a stream (that is,
3754      * deserialize it).




3709      * least significant digits.
3710      *
3711      * <p>If the scales of val[0] and val[1] differ, rescale
3712      * (non-destructively) the lower-scaled {@code BigDecimal} so
3713      * they match.  That is, the lower-scaled reference will be
3714      * replaced by a reference to a new object with the same scale as
3715      * the other {@code BigDecimal}.
3716      *
3717      * @param  val array of two elements referring to the two
3718      *         {@code BigDecimal}s to be aligned.
3719      */
3720     private static void matchScale(BigDecimal[] val) {
3721         if (val[0].scale < val[1].scale) {
3722             val[0] = val[0].setScale(val[1].scale, ROUND_UNNECESSARY);
3723         } else if (val[1].scale < val[0].scale) {
3724             val[1] = val[1].setScale(val[0].scale, ROUND_UNNECESSARY);
3725         }
3726     }
3727 
3728     private static class UnsafeHolder {
3729         private static final jdk.internal.misc.Unsafe unsafe;
3730         private static final long intCompactOffset;
3731         private static final long intValOffset;
3732         static {
3733             try {
3734                 unsafe = jdk.internal.misc.Unsafe.getUnsafe();
3735                 intCompactOffset = unsafe.objectFieldOffset
3736                     (BigDecimal.class.getDeclaredField("intCompact"));
3737                 intValOffset = unsafe.objectFieldOffset
3738                     (BigDecimal.class.getDeclaredField("intVal"));
3739             } catch (Exception ex) {
3740                 throw new ExceptionInInitializerError(ex);
3741             }
3742         }
3743         static void setIntCompact(BigDecimal bd, long val) {
3744             unsafe.putLong(bd, intCompactOffset, val);
3745         }
3746 
3747         static void setIntValVolatile(BigDecimal bd, BigInteger val) {
3748             unsafe.putObjectVolatile(bd, intValOffset, val);
3749         }
3750     }
3751 
3752     /**
3753      * Reconstitute the {@code BigDecimal} instance from a stream (that is,
3754      * deserialize it).