ruby-changes:60989
From: Nobuyoshi <ko1@a...>
Date: Sun, 3 May 2020 15:44:41 +0900 (JST)
Subject: [ruby-changes:60989] e49ecaed57 (master): Optimize sin/cos
https://git.ruby-lang.org/ruby.git/commit/?id=e49ecaed57 From e49ecaed57181c10bf01e1f84b9eead65a759386 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Sun, 3 May 2020 14:38:19 +0900 Subject: Optimize sin/cos GCC/Clang can optimize to calculate `sin(x)` and `cos(x)` at once, when the both are closely called on the same argument. Similar optimization is possible for `__sinpi(x)` and `__cospi(x)` if available, which calculate arguments in radian, i.e. `sin(x*M_PI)` and `cos(x*M_PI)` respectively. diff --git a/complex.c b/complex.c index 2a37c42..95b1abb 100644 --- a/complex.c +++ b/complex.c @@ -661,8 +661,9 @@ f_complex_polar(VALUE klass, VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/complex.c#L661 y = DBL2NUM(imag); } else { - y = f_mul(x, DBL2NUM(sin(arg))); - x = f_mul(x, DBL2NUM(cos(arg))); + const double ax = sin(arg), ay = cos(arg); + y = f_mul(x, DBL2NUM(ax)); + x = f_mul(x, DBL2NUM(ay)); if (canonicalization && f_zero_p(y)) return x; } return nucomp_s_new_internal(klass, x, y); @@ -672,6 +673,16 @@ f_complex_polar(VALUE klass, VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/complex.c#L673 f_mul(x, m_sin(y))); } +#ifdef HAVE___COSPI +# define cospi(x) __cospi(x) +#else +# define cospi(x) cos((x) * M_PI) +#endif +#ifdef HAVE___SINPI +# define sinpi(x) __sinpi(x) +#else +# define sinpi(x) sin((x) * M_PI) +#endif /* returns a Complex or Float of ang*PI-rotated abs */ VALUE rb_dbl_complex_new_polar_pi(double abs, double ang) @@ -689,8 +700,8 @@ rb_dbl_complex_new_polar_pi(double abs, double ang) https://github.com/ruby/ruby/blob/trunk/complex.c#L700 return DBL2NUM(abs); } else { - ang *= M_PI; - return rb_complex_new(DBL2NUM(abs * cos(ang)), DBL2NUM(abs * sin(ang))); + const double real = abs * cospi(ang), imag = abs * sinpi(ang); + return rb_complex_new(DBL2NUM(real), DBL2NUM(imag)); } } diff --git a/configure.ac b/configure.ac index e16c3e4..f78eb38 100644 --- a/configure.ac +++ b/configure.ac @@ -1991,6 +1991,8 @@ AC_CHECK_FUNCS(utimensat) https://github.com/ruby/ruby/blob/trunk/configure.ac#L1991 AC_CHECK_FUNCS(utimes) AC_CHECK_FUNCS(wait4) AC_CHECK_FUNCS(waitpid) +AC_CHECK_FUNCS(__cospi) +AC_CHECK_FUNCS(__sinpi) AS_IF([test "x$ac_cv_member_struct_statx_stx_btime" = xyes], [AC_CHECK_FUNCS(statx)]) -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/