Skip to content

Add single-precision atan and atan2 to xls_go_math. - #5566

Open
sh-mug wants to merge 1 commit into
google:mainfrom
sh-mug:go-math-atan
Open

sh-mug wants to merge 1 commit into
google:mainfrom
sh-mug:go-math-atan

Conversation

@sh-mug

@sh-mug sh-mug commented Nov 26, 2026

Copy link
Copy Markdown

Ports Go's math.Atan and math.Atan2 (which are based on Cephes atan.c) to DSLX as fp_atan_32 and fp_atan2_32, alongside the existing fp_sincos_32 port. Like the other xls_go_math functions, subnormal inputs and outputs are flushed to zero and a canonical NaN is returned.

Differences from the Go implementation:

  • Coefficients and constants are rounded to single precision; division uses fpdiv_2x32.
  • fp_atan_32 selects the reduced argument (x, -1/x or (x-1)/(x+1)) before evaluating xatan, so the three range-reduction cases share one divider and one xatan evaluation (two fpdiv_2x32 in total, counting the one in xatan's rational approximation).
  • The low-order bits of pi/2 and pi/4 (Morebits) are added to xatan's result before the high part, so they are not rounded away in single precision.
  • fp_atan2_32 picks the x < 0 quadrant correction from the sign of y rather than the sign of atan(y/x), so the result has the right sign when y/x underflows to zero, and adds pi as PI_LO + PI.

Accuracy: on 10M random inputs per function (half uniform bit patterns, half uniform in [-4, 4]), every result is within 1 ulp of libm's double-precision result rounded to float. This can be reproduced by running the testbenches with --max_ulp_error=1 --num_samples=10000000.

Testing:

  • DSLX unit tests for all special cases listed in Go's documentation, range-reduction boundaries, all quadrants, and y/x underflow/overflow.
  • fp_atan_32_test and fp_atan2_32_test: random-sampling JIT testbenches comparing against std::atan/std::atan2 with a 2 ulp tolerance.

Fixes #5448.

Ports Go's math.Atan and math.Atan2 (which are based on Cephes atan.c) to
DSLX as fp_atan_32 and fp_atan2_32, alongside the existing fp_sincos_32
port. Like the other xls_go_math functions, subnormal inputs and outputs
are flushed to zero and a canonical NaN is returned.

Differences from the Go implementation:
 - Coefficients and constants are rounded to single precision; division
   uses fpdiv_2x32.
 - fp_atan_32 selects the reduced argument (x, -1/x or (x-1)/(x+1)) before
   evaluating xatan, so the three range-reduction cases share one divider
   and one xatan evaluation (two fpdiv_2x32 in total, counting the one in
   xatan's rational approximation).
 - The low-order bits of pi/2 and pi/4 (Morebits) are added to xatan's
   result before the high part, so they are not rounded away in single
   precision.
 - fp_atan2_32 picks the x < 0 quadrant correction from the sign of y
   rather than the sign of atan(y/x), so the result has the right sign when
   y/x underflows to zero, and adds pi as PI_LO + PI.

Accuracy: on 10M random inputs per function (half uniform bit patterns,
half uniform in [-4, 4]), every result is within 1 ulp of libm's
double-precision result rounded to float. This can be reproduced by running
the testbenches with --max_ulp_error=1 --num_samples=10000000.

Testing:
 - DSLX unit tests for all special cases listed in Go's documentation,
   range-reduction boundaries, all quadrants, and y/x underflow/overflow.
 - fp_atan_32_test and fp_atan2_32_test: random-sampling JIT testbenches
   comparing against std::atan/std::atan2 with a 2 ulp tolerance.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[enhancement] atan/atan2 support

1 participant