glibc/sysdeps
Adhemerval Zanella Netto 34b9f8bc17 math: Improve fmod
This uses a new algorithm similar to already proposed earlier [1].
With x = mx * 2^ex and y = my * 2^ey (mx, my, ex, ey being integers),
the simplest implementation is:

   mx * 2^ex == 2 * mx * 2^(ex - 1)

   while (ex > ey)
     {
       mx *= 2;
       --ex;
       mx %= my;
     }

With mx/my being mantissa of double floating pointer, on each step the
argument reduction can be improved 11 (which is sizeo of uint64_t minus
MANTISSA_WIDTH plus the signal bit):

   while (ex > ey)
     {
       mx << 11;
       ex -= 11;
       mx %= my;
     }  */

The implementation uses builtin clz and ctz, along with shifts to
convert hx/hy back to doubles.  Different than the original patch,
this path assume modulo/divide operation is slow, so use multiplication
with invert values.

I see the following performance improvements using fmod benchtests
(result only show the 'mean' result):

  Architecture     | Input           | master   | patch
  -----------------|-----------------|----------|--------
  x86_64 (Ryzen 9) | subnormals      | 19.1584  | 12.5049
  x86_64 (Ryzen 9) | normal          | 1016.51  | 296.939
  x86_64 (Ryzen 9) | close-exponents | 18.4428  | 16.0244
  aarch64 (N1)     | subnormal       | 11.153   | 6.81778
  aarch64 (N1)     | normal          | 528.649  | 155.62
  aarch64 (N1)     | close-exponents | 11.4517  | 8.21306

I also see similar improvements on arm-linux-gnueabihf when running on
the N1 aarch64 chips, where it a lot of soft-fp implementation (for
modulo, clz, ctz, and multiplication):

  Architecture     | Input           | master   | patch
  -----------------|-----------------|----------|--------
  armhf (N1)       | subnormal       | 15.908   | 15.1083
  armhf (N1)       | normal          | 837.525  | 244.833
  armhf (N1)       | close-exponents | 16.2111  | 21.8182

Instead of using the math_private.h definitions, I used the
math_config.h instead which is used on newer math implementations.

Co-authored-by: kirill <kirill.okhotnikov@gmail.com>

[1] https://sourceware.org/pipermail/libc-alpha/2020-November/119794.html
Reviewed-by: Wilco Dijkstra  <Wilco.Dijkstra@arm.com>
2023-04-03 16:36:24 -03:00
..
aarch64 aarch64: update libm test ulps 2023-02-24 10:55:38 +00:00
alpha alpha: Remove strncmp optimization 2023-03-02 16:41:43 -03:00
arc ARC:fpu: add extra capability check before use of sqrt and fma builtins 2023-02-17 16:17:58 -03:00
arm Update arm libm-tests-ulps 2023-03-30 10:50:50 -03:00
csky C-SKY: Strip hard float abi from hard float feature. 2023-02-07 16:34:24 +08:00
generic Remove --enable-tunables configure option 2023-03-29 14:33:06 -03:00
gnu Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
hppa hppa: Drop 16-byte pthread lock alignment 2023-03-26 21:16:22 +00:00
htl htl: Remove ./sysdeps/htl/bits/types/struct___pthread_mutex.h 2023-02-12 17:28:32 +01:00
hurd Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
i386 hurd: Move rtld-strncpy-c.c out of mach/hurd/ 2023-04-03 01:10:23 +02:00
ia64 ia64: Regenerate ulps 2023-01-24 11:21:38 -05:00
ieee754 math: Improve fmod 2023-04-03 16:36:24 -03:00
loongarch LoongArch: Add get_rounding_mode. 2023-03-13 09:20:42 +08:00
m68k string: Remove string_private.h 2023-02-17 15:56:54 -03:00
mach hurd: Add vm_param.h for x86_64 2023-04-03 01:24:13 +02:00
microblaze Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
mips Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
nios2 Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
nptl Remove --enable-tunables configure option 2023-03-29 14:33:06 -03:00
or1k Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
posix system: Add "--" after "-c" for sh (BZ #28519) 2023-03-28 10:12:30 -03:00
powerpc Remove --enable-tunables configure option 2023-03-29 14:33:06 -03:00
pthread rt: fix shm_open not set ENAMETOOLONG when name exceeds {_POSIX_PATH_MAX} 2023-03-08 10:11:54 -03:00
riscv riscv: Add string-fza.h and string-fzi.h 2023-02-06 16:19:35 -03:00
s390 Remove --enable-tunables configure option 2023-03-29 14:33:06 -03:00
sh sh: Add string-fzb.h 2023-02-06 16:19:35 -03:00
sparc Remove --enable-tunables configure option 2023-03-29 14:33:06 -03:00
unix x86: Set FSGSBASE to active if enabled by kernel 2023-04-03 11:36:48 -07:00
wordsize-32 Update copyright dates with scripts/update-copyrights 2023-01-06 21:14:39 +00:00
wordsize-64 C2x strtol binary constant handling 2023-02-16 23:02:40 +00:00
x86 x86: Set FSGSBASE to active if enabled by kernel 2023-04-03 11:36:48 -07:00
x86_64 x86_64: Fix asm constraints in feraiseexcept (bug 30305) 2023-04-03 18:40:52 +02:00