Example #1
0
        /// <summary cref="XMath.IEEERemainder(float, float)"/>
        public static float IEEERemainder(float x, float y)
        {
            if (y == 0.0f ||
                XMath.IsInfinity(x) ||
                XMath.IsNaN(x) ||
                XMath.IsNaN(y))
            {
                return(float.NaN);
            }

            if (XMath.IsInfinity(y))
            {
                return(x);
            }

            return(x - (y * XMath.RoundToEven(x * XMath.Rcp(y))));
        }
Example #2
0
        public static double IEEERemainder(double x, double y)
        {
            if (y == 0.0 ||
                XMath.IsInfinity(x) ||
                XMath.IsNaN(x) ||
                XMath.IsNaN(y))
            {
                return(double.NaN);
            }

            if (XMath.IsInfinity(y))
            {
                return(x);
            }

            return(x - (y * XMath.RoundToEven(x * XMath.Rcp(y))));
        }
Example #3
0
        public static double Rem(double x, double y)
        {
            if (y == 0.0 ||
                XMath.IsInfinity(x) ||
                XMath.IsNaN(x) ||
                XMath.IsNaN(y))
            {
                return(double.NaN);
            }

            if (XMath.IsInfinity(y))
            {
                return(x);
            }

            var xDivY  = XMath.Abs(x * XMath.Rcp(y));
            var result = (xDivY - Floor(xDivY)) * XMath.Abs(y);

            return(Utilities.Select(x < 0.0, -result, result));
        }
Example #4
0
        public static float Rem(float x, float y)
        {
            if (y == 0.0f ||
                XMath.IsInfinity(x) ||
                XMath.IsNaN(x) ||
                XMath.IsNaN(y))
            {
                return(float.NaN);
            }

            if (XMath.IsInfinity(y))
            {
                return(x);
            }

            var xDivY  = XMath.Abs(x * XMath.Rcp(y));
            var result = (xDivY - Floor(xDivY)) * XMath.Abs(y);

            return(Utilities.Select(x < 0.0f, -result, result));
        }
Example #5
0
        public static float Pow(float @base, float exp)
        {
            if (exp == 0.0f)
            {
                // Rule #2
                // Ignoring Rule #1
                return(1.0f);
            }
            else if (@base == 1.0f)
            {
                // Rule #14 but ignoring second part about y = NaN
                // Ignoring Rule #1
                return(1.0f);
            }
            else if (XMath.IsNaN(@base) || XMath.IsNaN(exp))
            {
                // Rule #1
                return(float.NaN);
            }
            else if (@base == float.NegativeInfinity)
            {
                if (exp < 0.0f)
                {
                    // Rule #3
                    return(0.0f);
                }
                else if (IsOddInteger(exp))
                {
                    // Rule #4
                    return(float.NegativeInfinity);
                }
                else
                {
                    // Rule #5
                    return(float.PositiveInfinity);
                }
            }
            else if (@base < 0.0f && !IsInteger(exp) && !XMath.IsInfinity(exp))
            {
                // Rule #6
                return(float.NaN);
            }
            else if (@base == -1.0f && XMath.IsInfinity(exp))
            {
                // Rule #7
                return(1.0f);
            }
            else if (-1.0f < @base && @base < 1.0f && exp == float.NegativeInfinity)
            {
                // Rule #8
                return(float.PositiveInfinity);
            }
            else if (-1.0f < @base && @base < 1.0f && exp == float.PositiveInfinity)
            {
                // Rule #9
                return(0.0f);
            }
            else if ((@base < -1.0f || @base > 1.0f) && exp == float.NegativeInfinity)
            {
                // Rule #10
                return(0.0f);
            }
            else if ((@base < -1.0f || @base > 1.0f) && exp == float.PositiveInfinity)
            {
                // Rule #11
                return(float.PositiveInfinity);
            }
            else if (@base == 0.0f)
            {
                if (exp < 0.0f)
                {
                    // Rule #12
                    return(float.PositiveInfinity);
                }
                else
                {
                    // Rule #13
                    // NB: exp == 0.0 already handled by Rule #2
                    return(0.0f);
                }
            }
            else if (@base == float.PositiveInfinity)
            {
                if (exp < 0.0f)
                {
                    // Rule #15
                    return(0.0f);
                }
                else
                {
                    // Rule #16
                    // NB: exp == 0.0 already handled by Rule #2
                    return(float.PositiveInfinity);
                }
            }

            if (@base < 0.0)
            {
                // 'exp' is an integer, due to Rule #6.
                var sign = IsOddInteger(exp) ? -1.0f : 1.0f;
                return(sign * Exp(exp * Log(XMath.Abs(@base))));
            }

            return(Exp(exp * Log(@base)));
        }
Example #6
0
        public static float Pow(float @base, float exp)
        {
            // TODO: The second condition of !XMath.IsNaN(), for the first two if/else conditions,
            // can be removed after fixing https://github.com/m4rs-mt/ILGPU/issues/93
            if (exp == 0.0f && !XMath.IsNaN(exp))
            {
                // Rule #2
                // Ignoring Rule #1
                return(1.0f);
            }
            else if (@base == 1.0f && !XMath.IsNaN(@base))
            {
                // Rule #14 but ignoring second part about y = NaN
                // Ignoring Rule #1
                return(1.0f);
            }
            else if (XMath.IsNaN(@base) || XMath.IsNaN(exp))
            {
                // Rule #1
                return(float.NaN);
            }
            else if (@base == float.NegativeInfinity)
            {
                if (exp < 0.0f)
                {
                    // Rule #3
                    return(0.0f);
                }
                else if (IsOddInteger(exp))
                {
                    // Rule #4
                    return(float.NegativeInfinity);
                }
                else
                {
                    // Rule #5
                    return(float.PositiveInfinity);
                }
            }
            else if (@base < 0.0f && !IsInteger(exp) && !XMath.IsInfinity(exp))
            {
                // Rule #6
                return(float.NaN);
            }
            else if (@base == -1.0f && XMath.IsInfinity(exp))
            {
                // Rule #7
                return(1.0f);
            }
            else if (-1.0f < @base && @base < 1.0f && exp == float.NegativeInfinity)
            {
                // Rule #8
                return(float.PositiveInfinity);
            }
            else if (-1.0f < @base && @base < 1.0f && exp == float.PositiveInfinity)
            {
                // Rule #9
                return(0.0f);
            }
            else if ((@base < -1.0f || @base > 1.0f) && exp == float.NegativeInfinity)
            {
                // Rule #10
                return(0.0f);
            }
            else if ((@base < -1.0f || @base > 1.0f) && exp == float.PositiveInfinity)
            {
                // Rule #11
                return(float.PositiveInfinity);
            }
            else if (@base == 0.0f)
            {
                if (exp < 0.0f)
                {
                    // Rule #12
                    return(float.PositiveInfinity);
                }
                else
                {
                    // Rule #13
                    // NB: exp == 0.0 already handled by Rule #2
                    return(0.0f);
                }
            }
            else if (@base == float.PositiveInfinity)
            {
                if (exp < 0.0f)
                {
                    // Rule #14
                    return(0.0f);
                }
                else
                {
                    // Rule #15
                    // NB: exp == 0.0 already handled by Rule #2
                    return(float.PositiveInfinity);
                }
            }

            if (@base < 0.0)
            {
                // 'exp' is an integer, due to Rule #6.
                var sign = IsOddInteger(exp) ? -1.0f : 1.0f;
                return(sign * Exp(exp * Log(XMath.Abs(@base))));
            }

            return(Exp(exp * Log(@base)));
        }