]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
timeout: remove dependence on libm
authorPádraig Brady <P@draigBrady.com>
Sat, 5 Apr 2025 20:48:05 +0000 (21:48 +0100)
committerPádraig Brady <P@draigBrady.com>
Sat, 5 Apr 2025 23:07:49 +0000 (00:07 +0100)
This was seen to add about 100,000 ns to the startup time,
on a 2.6 GHz i7-5600U with glibc 2.40.

* .gitignore: Remove /lib/fenv.h.
* bootstrap.conf: Remove fenv-rounding and signbit deps.
* src/local.mk: Remove fenv lib dependency.
* src/timeout.c (is_negative): A new helper function to
be equivalent of signbit in the underflow case.
(parse_duration): Remove the rounding up logic,
as a nanosecond here or there has no significance.

.gitignore
bootstrap.conf
src/local.mk
src/timeout.c

index 4dc39ae551c58cbde21cb461db4dc598c974aa74..f4a17ad0443c05bca76ed09f6cc08a3a4225e560 100644 (file)
@@ -67,7 +67,6 @@
 /lib/errno.h
 /lib/error.h
 /lib/fcntl.h
-/lib/fenv.h
 /lib/float.h
 /lib/fnmatch.h
 /lib/getopt-cdefs.h
index 3c133ef7148b39b2eb04b3039557246276e6cb9d..c99838e95a2e4a59cd6b9d25f567f0622dab45f5 100644 (file)
@@ -99,7 +99,6 @@ gnulib_modules="
   fdatasync
   fdopen
   fdutimensat
-  fenv-rounding
   file-has-acl
   file-type
   fileblocks
@@ -243,7 +242,6 @@ gnulib_modules="
   settime
   sig2str
   sigaction
-  signbit
   skipchars
   smack
   ssize_t
index 0a4e3af432ad866cc3be21f241fc87e7ef516236..fd9dc81c278f76b097eb280098cdce35b12e404d 100644 (file)
@@ -257,9 +257,6 @@ src_stat_LDADD += $(LIB_SELINUX)
 # for nvlist_lookup_uint64_array
 src_stat_LDADD += $(LIB_NVPAIR)
 
-# for fegetround, fesetround
-src_timeout_LDADD += $(FENV_ROUNDING_LIBM)
-
 # for gettime, settime, tempname, utimecmp, utimens
 copy_ldadd += $(CLOCK_TIME_LIB)
 src_date_LDADD += $(CLOCK_TIME_LIB)
index 5947c19421ae006c93804af3b3ebd9c635d328fc..fe8eb45596cf24e03aebf31eb8d0a8dabf5935d2 100644 (file)
@@ -45,9 +45,8 @@
    Written by Pádraig Brady.  */
 
 #include <config.h>
-#include <fenv.h>
+#include <ctype.h>
 #include <getopt.h>
-#include <math.h>
 #include <stdio.h>
 #include <sys/types.h>
 #include <signal.h>
@@ -354,32 +353,25 @@ apply_time_suffix (double *x, char suffix_char)
   return true;
 }
 
+ATTRIBUTE_PURE static bool
+is_negative (char const *num)
+{
+  while (*num && isspace (to_uchar (*num)))
+    num++;
+  return *num == '-';
+}
+
 static double
 parse_duration (char const *str)
 {
-  /* If possible tell strtod to round up, so that we always wait at
-     least as long as STR specifies.  Although Standard C requires
-     FENV_ACCESS ON, don't bother if using GCC as it warns.  */
-#ifdef FE_UPWARD
-# if !defined __GNUC__ && 199901 <= __STDC_VERSION__
-#  pragma STDC FENV_ACCESS ON
-# endif
-  int round = fegetround ();
-  fesetround (FE_UPWARD);
-#endif
-
   char *ep;
   double duration = cl_strtod (str, &ep);
 
-#ifdef FE_UPWARD
-  fesetround (round);
-#endif
-
   if (ep == str
       /* Nonnegative interval.  */
       || ! (0 <= duration)
       /* The interval did not underflow to -0.  */
-      || (signbit (duration) && errno == ERANGE)
+      || (errno == ERANGE && is_negative (str))
       /* No extra chars after the number and an optional s,m,h,d char.  */
       || (*ep && *(ep + 1))
       /* Check any suffix char and update timeout based on the suffix.  */
@@ -391,16 +383,10 @@ parse_duration (char const *str)
 
   /* Do not let the duration underflow to 0, as 0 disables the timeout.
      Use 2**-30 instead of 0; settimeout will round it up to 1 ns,
-     whereas 1e-9 might double-round to 2 ns.
+     whereas 1e-9 might double-round to 2 ns.  */
 
-     If FE_UPWARD is defined, this code is not needed on glibc as its
-     strtod rounds upward correctly.  Although this code might not be
-     needed on non-glibc platforms too, it's too much trouble to
-     worry about that.  */
-#if !defined FE_UPWARD || !defined __GLIBC__
   if (duration == 0 && errno == ERANGE)
     duration = 9.313225746154785e-10;
-#endif
 
   return duration;
 }