]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
cksum,wc: clean up hw capability checking
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 14 Jun 2023 21:52:37 +0000 (14:52 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 14 Jun 2023 21:54:46 +0000 (14:54 -0700)
* src/cksum.c (cksum_pclmul) [!CRCTAB && !USE_PCLMUL_CRC32]:
Remove macro.
(cksum_fp): No longer file-scope.
(pclmul_supported): Define only if USE_PCLMUL_CRC32.
This omits the debug output "using generic hardware support"
for simplicity and consistency with wc’s output.
(crc_sum_stream) [!USE_PCLMUL_32]: No need for static function pointer.
* src/wc.c (wc_lines_p) [USE_AVX2_WC_LINECOUNT]: No longer file-scope.
(wc) [USE_AVX2_WC_LINECOUNT]: Check for avx2 support at most once,
which was surely the code’s original intent.
(wc) [!USE_AVX2_WC_LINECOUNT]: No need for static function pointer.

src/cksum.c
src/wc.c

index 352a0ba3af8d65bf50707120d8414a0b9ed67daa..26bb29bdb6b5abc7093819d1855ac5eb5212f9da 100644 (file)
@@ -141,23 +141,14 @@ main (void)
 # include "error.h"
 
 # include "cksum.h"
-# if !USE_PCLMUL_CRC32
-#  define cksum_pclmul cksum_slice8
-# endif /* USE_PCLMUL_CRC32 */
 
 /* Number of bytes to read at once.  */
 # define BUFLEN (1 << 16)
 
-
-static bool
-cksum_slice8 (FILE *fp, uint_fast32_t *crc_out, uintmax_t *length_out);
-static bool
-  (*cksum_fp)(FILE *, uint_fast32_t *, uintmax_t *);
-
+# if USE_PCLMUL_CRC32
 static bool
 pclmul_supported (void)
 {
-# if USE_PCLMUL_CRC32
   bool pclmul_enabled = (0 < __builtin_cpu_supports ("pclmul")
                          && 0 < __builtin_cpu_supports ("avx"));
 
@@ -168,12 +159,8 @@ pclmul_supported (void)
             : _("pclmul support not detected")));
 
   return pclmul_enabled;
-# else
-  if (cksum_debug)
-    error (0, 0, "%s", _("using generic hardware support"));
-  return false;
-# endif /* USE_PCLMUL_CRC32 */
 }
+# endif /* USE_PCLMUL_CRC32 */
 
 static bool
 cksum_slice8 (FILE *fp, uint_fast32_t *crc_out, uintmax_t *length_out)
@@ -238,13 +225,13 @@ crc_sum_stream (FILE *stream, void *resstream, uintmax_t *length)
   uintmax_t total_bytes = 0;
   uint_fast32_t crc = 0;
 
+# if USE_PCLMUL_CRC32
+  static bool (*cksum_fp) (FILE *, uint_fast32_t *, uintmax_t *);
   if (! cksum_fp)
-    {
-       if (pclmul_supported ())
-         cksum_fp = cksum_pclmul;
-       else
-         cksum_fp = cksum_slice8;
-    }
+    cksum_fp = pclmul_supported () ? cksum_pclmul : cksum_slice8;
+#else
+  bool (*cksum_fp) (FILE *, uint_fast32_t *, uintmax_t *) = cksum_slice8;
+#endif
 
   if (! cksum_fp (stream, &crc, &total_bytes))
     return -1;
index ebe83af4dfe587e84e263e667ba6a3dac4f2576b..30214655c74f8d337030857e10570398a408da75 100644 (file)
--- a/src/wc.c
+++ b/src/wc.c
 /* Size of atomic reads. */
 #define BUFFER_SIZE (16 * 1024)
 
-static bool
-wc_lines (char const *file, int fd, uintmax_t *lines_out,
-          uintmax_t *bytes_out);
 #ifdef USE_AVX2_WC_LINECOUNT
 /* From wc_avx2.c */
 extern bool
 wc_lines_avx2 (char const *file, int fd, uintmax_t *lines_out,
                uintmax_t *bytes_out);
 #endif
-static bool
-(*wc_lines_p) (char const *file, int fd, uintmax_t *lines_out,
-                uintmax_t *bytes_out) = wc_lines;
 
 static bool debug;
 
@@ -441,8 +435,12 @@ wc (int fd, char const *file_x, struct fstatus *fstatus, off_t current_pos)
   else if (!count_chars && !count_complicated)
     {
 #ifdef USE_AVX2_WC_LINECOUNT
-      if (avx2_supported ())
-        wc_lines_p = wc_lines_avx2;
+      static bool (*wc_lines_p) (char const *, int, uintmax_t *, uintmax_t *);
+      if (!wc_lines_p)
+        wc_lines_p = avx2_supported () ? wc_lines_avx2 : wc_lines;
+#else
+      bool (*wc_lines_p) (char const *, int, uintmax_t *, uintmax_t *)
+        = wc_lines;
 #endif
 
       /* Use a separate loop when counting only lines or lines and bytes --