The calculate_crc and calculate_reversed_crc functions assumed initial
crc of 0 and only operated on 8 bits of data. This commit extends those
functions to take explicit data arg and data size as well, so that it
can compute CRC for >8 bits with a separate initial CRC.
These functions are also made public so that they can be called from
different modules. This will be used in the future to implement CRC
const folding.

gcc/ChangeLog:

        * expr.cc (calculate_crc): Modified function and made it public.
        (assemble_crc_table): Updated function call.
        (calculate_reversed_crc): Modified function and made it public.
        (assemble_reversed_crc_table): Updated function call.
        * expr.h (calculate_crc): Added new function prototype.
        (calculate_reversed_crc): Added new function prototype.

Signed-off-by: Shreesh Adiga <[email protected]>
---
 gcc/expr.cc | 36 +++++++++++++++++++++---------------
 gcc/expr.h  | 14 ++++++++++++++
 2 files changed, 35 insertions(+), 15 deletions(-)

diff --git a/gcc/expr.cc b/gcc/expr.cc
index 0a7013e3a25..5d6ad2c6f85 100644
--- a/gcc/expr.cc
+++ b/gcc/expr.cc
@@ -14581,17 +14581,19 @@ gf2n_poly_long_div_quotient (unsigned HOST_WIDE_INT 
polynomial,
   return quotient;
 }
 
-/* Calculate CRC for the initial CRC and given POLYNOMIAL.
-   CRC_BITS is CRC size.  */
+/* Calculate CRC for the initial CRC, DATA and given POLYNOMIAL.
+   CRC_BITS is CRC size and DATA_BITS is the data size.  */
 
-static unsigned HOST_WIDE_INT
+unsigned HOST_WIDE_INT
 calculate_crc (unsigned HOST_WIDE_INT crc,
+              unsigned HOST_WIDE_INT data,
               unsigned HOST_WIDE_INT polynomial,
-              unsigned short crc_bits)
+              unsigned short crc_bits,
+              unsigned short data_bits)
 {
   unsigned HOST_WIDE_INT msb = HOST_WIDE_INT_1U << (crc_bits - 1);
-  crc = crc << (crc_bits - 8);
-  for (short i = 8; i > 0; --i)
+  crc ^= (data << (crc_bits - data_bits));
+  for (short i = data_bits; i > 0; --i)
     {
       if (crc & msb)
        crc = (crc << 1) ^ polynomial;
@@ -14599,7 +14601,7 @@ calculate_crc (unsigned HOST_WIDE_INT crc,
        crc <<= 1;
     }
   /* Zero out bits in crc beyond the specified number of crc_bits.  */
-  if (crc_bits < sizeof (crc) * CHAR_BIT)
+  if (crc_bits < HOST_BITS_PER_WIDE_INT)
     crc &= (HOST_WIDE_INT_1U << crc_bits) - 1;
   return crc;
 }
@@ -14620,7 +14622,7 @@ assemble_crc_table (unsigned HOST_WIDE_INT polynom, 
unsigned short crc_bits)
   vec_alloc (initial_values, table_el_n);
   for (size_t i = 0; i < table_el_n; ++i)
     {
-      unsigned HOST_WIDE_INT crc = calculate_crc (i, polynom, crc_bits);
+      unsigned HOST_WIDE_INT crc = calculate_crc (0, i, polynom, crc_bits, 8);
       tree element = build_int_cstu (make_unsigned_type (crc_bits), crc);
       vec_safe_push (initial_values, element);
     }
@@ -14654,16 +14656,19 @@ generate_crc_table (unsigned HOST_WIDE_INT polynom, 
unsigned short crc_bits)
   return assemble_crc_table (polynom, crc_bits);
 }
 
-/* Calculate CRC for the initial CRC and given POLYNOMIAL.
-   CRC_BITS is CRC size.  */
+/* Calculate CRC for the initial CRC, DATA and given POLYNOMIAL.
+   CRC_BITS is CRC size and DATA_BITS is the DATA size.  */
 
-static unsigned HOST_WIDE_INT
+unsigned HOST_WIDE_INT
 calculate_reversed_crc (unsigned HOST_WIDE_INT crc,
+                       unsigned HOST_WIDE_INT data,
                        unsigned HOST_WIDE_INT polynomial,
-                       unsigned short crc_bits)
+                       unsigned short crc_bits,
+                       unsigned short data_bits)
 {
   unsigned HOST_WIDE_INT rev_polynom = reflect_hwi (polynomial, crc_bits);
-  for (int j = 0; j < 8; j++)
+  crc ^= data;
+  for (int j = 0; j < data_bits; j++)
     {
       if (crc & 1)
        crc = (crc >> 1) ^ rev_polynom;
@@ -14671,7 +14676,7 @@ calculate_reversed_crc (unsigned HOST_WIDE_INT crc,
        crc >>= 1;
     }
   /* Zero out bits in crc beyond the specified number of crc_bits.  */
-  if (crc_bits < sizeof (crc) * CHAR_BIT)
+  if (crc_bits < HOST_BITS_PER_WIDE_INT)
     crc &= (HOST_WIDE_INT_1U << crc_bits) - 1;
   return crc;
 }
@@ -14692,7 +14697,8 @@ assemble_reversed_crc_table (unsigned HOST_WIDE_INT 
polynom, unsigned short crc_
   vec_alloc (initial_values, table_el_n);
   for (size_t i = 0; i < table_el_n; ++i)
     {
-      unsigned HOST_WIDE_INT crc = calculate_reversed_crc (i, polynom, 
crc_bits);
+      unsigned HOST_WIDE_INT crc = calculate_reversed_crc (0, i, polynom,
+                                                          crc_bits, 8);
       tree element = build_int_cstu (make_unsigned_type (crc_bits), crc);
       vec_safe_push (initial_values, element);
     }
diff --git a/gcc/expr.h b/gcc/expr.h
index a54984ea2f8..0f9b3942eb2 100644
--- a/gcc/expr.h
+++ b/gcc/expr.h
@@ -406,6 +406,20 @@ extern void generate_reflecting_code_standard (rtx *);
 extern void expand_crc_table_based (rtx, rtx, rtx, rtx, machine_mode);
 extern void expand_reversed_crc_table_based (rtx, rtx, rtx, rtx, machine_mode);
 
+/* Calculate CRC for a given initial CRC, DATA, POLYNOMIAL,
+   CRC_SIZE in bits and DATA_SIZE in bits.  */
+extern unsigned HOST_WIDE_INT
+calculate_crc (unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
+              unsigned HOST_WIDE_INT, unsigned short, unsigned short);
+
+/* Calculate reversed CRC for a given initial CRC, DATA, POLYNOMIAL,
+   CRC_SIZE in bits and DATA_SIZE in bits.  */
+extern unsigned HOST_WIDE_INT
+calculate_reversed_crc (unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
+                       unsigned HOST_WIDE_INT, unsigned short,
+                       unsigned short);
+
+
 /* Cache of the "extended" flag in the target's _BitInt description
    for use during expand.  */
 extern int bitint_extended;
-- 
2.54.0

Reply via email to