Search in sources :

Example 1 with HotSpotIntrinsicCandidate

use of jdk.internal.HotSpotIntrinsicCandidate in project Bytecoder by mirkosertic.

the class Math method fma.

/**
 * Returns the fused multiply add of the three arguments; that is,
 * returns the exact product of the first two arguments summed
 * with the third argument and then rounded once to the nearest
 * {@code double}.
 *
 * The rounding is done using the {@linkplain
 * java.math.RoundingMode#HALF_EVEN round to nearest even
 * rounding mode}.
 *
 * In contrast, if {@code a * b + c} is evaluated as a regular
 * floating-point expression, two rounding errors are involved,
 * the first for the multiply operation, the second for the
 * addition operation.
 *
 * <p>Special cases:
 * <ul>
 * <li> If any argument is NaN, the result is NaN.
 *
 * <li> If one of the first two arguments is infinite and the
 * other is zero, the result is NaN.
 *
 * <li> If the exact product of the first two arguments is infinite
 * (in other words, at least one of the arguments is infinite and
 * the other is neither zero nor NaN) and the third argument is an
 * infinity of the opposite sign, the result is NaN.
 *
 * </ul>
 *
 * <p>Note that {@code fma(a, 1.0, c)} returns the same
 * result as ({@code a + c}).  However,
 * {@code fma(a, b, +0.0)} does <em>not</em> always return the
 * same result as ({@code a * b}) since
 * {@code fma(-0.0, +0.0, +0.0)} is {@code +0.0} while
 * ({@code -0.0 * +0.0}) is {@code -0.0}; {@code fma(a, b, -0.0)} is
 * equivalent to ({@code a * b}) however.
 *
 * @apiNote This method corresponds to the fusedMultiplyAdd
 * operation defined in IEEE 754-2008.
 *
 * @param a a value
 * @param b a value
 * @param c a value
 *
 * @return (<i>a</i>&nbsp;&times;&nbsp;<i>b</i>&nbsp;+&nbsp;<i>c</i>)
 * computed, as if with unlimited range and precision, and rounded
 * once to the nearest {@code double} value
 *
 * @since 9
 */
@HotSpotIntrinsicCandidate
public static double fma(double a, double b, double c) {
    // arithmetic is not supported by BigDecimal.
    if (Double.isNaN(a) || Double.isNaN(b) || Double.isNaN(c)) {
        return Double.NaN;
    } else {
        // All inputs non-NaN
        boolean infiniteA = Double.isInfinite(a);
        boolean infiniteB = Double.isInfinite(b);
        boolean infiniteC = Double.isInfinite(c);
        double result;
        if (infiniteA || infiniteB || infiniteC) {
            if (infiniteA && b == 0.0 || infiniteB && a == 0.0) {
                return Double.NaN;
            }
            // Store product in a double field to cause an
            // overflow even if non-strictfp evaluation is being
            // used.
            double product = a * b;
            if (Double.isInfinite(product) && !infiniteA && !infiniteB) {
                // spurious NaN if added to infinite c.
                assert Double.isInfinite(c);
                return c;
            } else {
                result = product + c;
                assert !Double.isFinite(result);
                return result;
            }
        } else {
            // All inputs finite
            BigDecimal product = (new BigDecimal(a)).multiply(new BigDecimal(b));
            if (c == 0.0) {
                // b is zero.
                if (a == 0.0 || b == 0.0) {
                    return a * b + c;
                } else {
                    // sign bit".
                    return product.doubleValue();
                }
            } else {
                return product.add(new BigDecimal(c)).doubleValue();
            }
        }
    }
}
Also used : BigDecimal(java.math.BigDecimal) HotSpotIntrinsicCandidate(jdk.internal.HotSpotIntrinsicCandidate)

Example 2 with HotSpotIntrinsicCandidate

use of jdk.internal.HotSpotIntrinsicCandidate in project Bytecoder by mirkosertic.

the class Method method invoke.

/**
 * Invokes the underlying method represented by this {@code Method}
 * object, on the specified object with the specified parameters.
 * Individual parameters are automatically unwrapped to match
 * primitive formal parameters, and both primitive and reference
 * parameters are subject to method invocation conversions as
 * necessary.
 *
 * <p>If the underlying method is static, then the specified {@code obj}
 * argument is ignored. It may be null.
 *
 * <p>If the number of formal parameters required by the underlying method is
 * 0, the supplied {@code args} array may be of length 0 or null.
 *
 * <p>If the underlying method is an instance method, it is invoked
 * using dynamic method lookup as documented in The Java Language
 * Specification, Second Edition, section 15.12.4.4; in particular,
 * overriding based on the runtime type of the target object will occur.
 *
 * <p>If the underlying method is static, the class that declared
 * the method is initialized if it has not already been initialized.
 *
 * <p>If the method completes normally, the value it returns is
 * returned to the caller of invoke; if the value has a primitive
 * type, it is first appropriately wrapped in an object. However,
 * if the value has the type of an array of a primitive type, the
 * elements of the array are <i>not</i> wrapped in objects; in
 * other words, an array of primitive type is returned.  If the
 * underlying method return type is void, the invocation returns
 * null.
 *
 * @param obj  the object the underlying method is invoked from
 * @param args the arguments used for the method call
 * @return the result of dispatching the method represented by
 * this object on {@code obj} with parameters
 * {@code args}
 *
 * @exception IllegalAccessException    if this {@code Method} object
 *              is enforcing Java language access control and the underlying
 *              method is inaccessible.
 * @exception IllegalArgumentException  if the method is an
 *              instance method and the specified object argument
 *              is not an instance of the class or interface
 *              declaring the underlying method (or of a subclass
 *              or implementor thereof); if the number of actual
 *              and formal parameters differ; if an unwrapping
 *              conversion for primitive arguments fails; or if,
 *              after possible unwrapping, a parameter value
 *              cannot be converted to the corresponding formal
 *              parameter type by a method invocation conversion.
 * @exception InvocationTargetException if the underlying method
 *              throws an exception.
 * @exception NullPointerException      if the specified object is null
 *              and the method is an instance method.
 * @exception ExceptionInInitializerError if the initialization
 * provoked by this method fails.
 */
@CallerSensitive
// to ensure Reflection.getCallerClass optimization
@ForceInline
@HotSpotIntrinsicCandidate
public Object invoke(Object obj, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    if (!override) {
        Class<?> caller = Reflection.getCallerClass();
        checkAccess(caller, clazz, Modifier.isStatic(modifiers) ? null : obj.getClass(), modifiers);
    }
    // read volatile
    MethodAccessor ma = methodAccessor;
    if (ma == null) {
        ma = acquireMethodAccessor();
    }
    return ma.invoke(obj, args);
}
Also used : MethodAccessor(jdk.internal.reflect.MethodAccessor) ForceInline(jdk.internal.vm.annotation.ForceInline) CallerSensitive(jdk.internal.reflect.CallerSensitive) HotSpotIntrinsicCandidate(jdk.internal.HotSpotIntrinsicCandidate)

Aggregations

HotSpotIntrinsicCandidate (jdk.internal.HotSpotIntrinsicCandidate)2 BigDecimal (java.math.BigDecimal)1 CallerSensitive (jdk.internal.reflect.CallerSensitive)1 MethodAccessor (jdk.internal.reflect.MethodAccessor)1 ForceInline (jdk.internal.vm.annotation.ForceInline)1