od: omit some duplicate code

On x86-64 (for example) print_long, print_long_long, and
print_intmax all behave identically, so give GCC enough info so
that it generates code for just one of these functions.
* src/od.c (enum size_spec): Arrange for enum values to
be the same if they represent types that behave the same.
(width_bytes, ISPEC_TO_FORMAT, decode_one_format):
Match the enum size_spec changes.
This commit is contained in:
Paul Eggert committed 2025-06-28 21:00:41 -07:00
1 parent 3ad59e19bc
commit 56aa549a06
1 file changed
+55 -70
+55 -70
View File
@@ -67,15 +67,17 @@ enum size_spec
{ {
NO_SIZE = 0, NO_SIZE = 0,
CHAR, CHAR,
SHORT, SHORT = CHAR + (UCHAR_MAX < USHRT_MAX),
INT, INT = SHORT + (USHRT_MAX < UINT_MAX),
LONG, LONG = INT + (UINT_MAX < ULONG_MAX),
LONG_LONG, LONG_LONG = LONG + (ULONG_MAX < ULLONG_MAX),
INTMAX, INTMAX = LONG_LONG + (ULLONG_MAX < UINTMAX_MAX),
FLOAT_HALF, FLOAT_HALF, /* Used only if (FLOAT16_SUPPORTED || BF16_SUPPORTED). */
FLOAT_SINGLE, FLOAT_SINGLE = FLOAT_HALF + (FLOAT16_SUPPORTED || BF16_SUPPORTED),
FLOAT_DOUBLE, FLOAT_DOUBLE = FLOAT_SINGLE + (FLT_MANT_DIG < DBL_MANT_DIG
FLOAT_LONG_DOUBLE, || FLT_MAX_EXP < DBL_MAX_EXP),
FLOAT_LONG_DOUBLE = FLOAT_DOUBLE + (DBL_MANT_DIG < LDBL_MANT_DIG
|| DBL_MAX_EXP < LDBL_MAX_EXP),
N_SIZE_SPECS N_SIZE_SPECS
}; };
@@ -130,14 +132,24 @@ static const int width_bytes[] =
{ {
-1, -1,
sizeof (char), sizeof (char),
#if UCHAR_MAX < USHRT_MAX
sizeof (short int), sizeof (short int),
#endif
#if USHRT_MAX < UINT_MAX
sizeof (int), sizeof (int),
#endif
#if UINT_MAX < ULONG_MAX
sizeof (long int), sizeof (long int),
#endif
#if ULONG_MAX < ULLONG_MAX
sizeof (unsigned long long int), sizeof (unsigned long long int),
#endif
#if ULLONG_MAX < UINTMAX_MAX
sizeof (uintmax_t), sizeof (uintmax_t),
#endif
#if BF16_SUPPORTED #if BF16_SUPPORTED
sizeof (bfloat16), sizeof (bfloat16),
#else #elif FLOAT16_SUPPORTED
sizeof (float16), sizeof (float16),
#endif #endif
sizeof (float), sizeof (float),
@@ -244,7 +256,7 @@ static bool have_read_stdin;
/* Map the size in bytes to a type identifier. /* Map the size in bytes to a type identifier.
When two types have the same machine layout: When two types have the same machine layout:
- Prefer unsigned int to higher ranked types, as its format is shorter. - Prefer unsigned int to other types, as its format is shorter.
- Prefer unsigned long to higher-ranked types, as it is older. - Prefer unsigned long to higher-ranked types, as it is older.
- Prefer uintmax_t to unsigned long long int; this wins if %lld - Prefer uintmax_t to unsigned long long int; this wins if %lld
does not work but %jd does (e.g., MS-Windows). */ does not work but %jd does (e.g., MS-Windows). */
@@ -750,9 +762,9 @@ decode_one_format (char const *s_orig, char const *s, char const **next,
} }
#define ISPEC_TO_FORMAT(Spec, Min_fmt, Long_fmt, Long_long_fmt, Max_fmt) \ #define ISPEC_TO_FORMAT(Spec, Min_fmt, Long_fmt, Long_long_fmt, Max_fmt) \
((Spec) == INTMAX ? (Max_fmt) \ (LONG < INTMAX && (Spec) == INTMAX ? (Max_fmt) \
: (Spec) == LONG_LONG ? (Long_long_fmt) \ : LONG < LONG_LONG && (Spec) == LONG_LONG ? (Long_long_fmt) \
: (Spec) == LONG ? (Long_fmt) \ : INT < LONG && (Spec) == LONG ? (Long_fmt) \
: (Min_fmt)) : (Min_fmt))
size_spec = integral_type_size[size]; size_spec = integral_type_size[size];
@@ -791,39 +803,18 @@ decode_one_format (char const *s_orig, char const *s, char const **next,
unreachable (); unreachable ();
} }
switch (+size_spec) /* Prefer INT, prefer LONG to longer types,
{ and prefer INTMAX to LONG_LONG. */
case CHAR: print_function
print_function = (fmt == SIGNED_DECIMAL = (size_spec == INT ? print_int
? print_s_char : size_spec == SHORT ? (fmt == SIGNED_DECIMAL
: print_char); ? print_s_short : print_short)
break; : size_spec == CHAR ? (fmt == SIGNED_DECIMAL
? print_s_char : print_char)
case SHORT: : size_spec == LONG ? print_long
print_function = (fmt == SIGNED_DECIMAL : size_spec == INTMAX ? print_intmax
? print_s_short : size_spec == LONG_LONG ? print_long_long
: print_short); : (affirm (false), nullptr));
break;
case INT:
print_function = print_int;
break;
case LONG:
print_function = print_long;
break;
case LONG_LONG:
print_function = print_long_long;
break;
case INTMAX:
print_function = print_intmax;
break;
default:
affirm (false);
}
break; break;
case 'f': case 'f':
@@ -872,10 +863,7 @@ decode_one_format (char const *s_orig, char const *s, char const **next,
else else
{ {
if (ARRAY_CARDINALITY (fp_type_size) <= size if (ARRAY_CARDINALITY (fp_type_size) <= size
|| fp_type_size[size] == NO_SIZE || fp_type_size[size] == NO_SIZE)
|| (! FLOAT16_SUPPORTED && BF16_SUPPORTED
&& size == sizeof (bfloat16))
)
{ {
error (0, 0, error (0, 0,
_("invalid type string %s;\n" _("invalid type string %s;\n"
@@ -904,32 +892,29 @@ decode_one_format (char const *s_orig, char const *s, char const **next,
idx_t decimal_point_len = idx_t decimal_point_len =
(locale->decimal_point[0] ? strlen (locale->decimal_point) : 1); (locale->decimal_point[0] ? strlen (locale->decimal_point) : 1);
switch (+size_spec) if (size_spec == FLOAT_DOUBLE)
{
print_function = print_double;
field_width = DBL_STRLEN_BOUND_L (decimal_point_len);
}
else if (size_spec == FLOAT_SINGLE)
{
print_function = print_float;
field_width = FLT_STRLEN_BOUND_L (decimal_point_len);
}
else if (size_spec == FLOAT_HALF)
{ {
case FLOAT_HALF:
print_function = fmt == BFLOATING_POINT print_function = fmt == BFLOATING_POINT
? print_bfloat : print_halffloat; ? print_bfloat : print_halffloat;
field_width = FLT_STRLEN_BOUND_L (decimal_point_len); field_width = FLT_STRLEN_BOUND_L (decimal_point_len);
break; }
else if (size_spec == FLOAT_LONG_DOUBLE)
case FLOAT_SINGLE: {
print_function = print_float;
field_width = FLT_STRLEN_BOUND_L (decimal_point_len);
break;
case FLOAT_DOUBLE:
print_function = print_double;
field_width = DBL_STRLEN_BOUND_L (decimal_point_len);
break;
case FLOAT_LONG_DOUBLE:
print_function = print_long_double; print_function = print_long_double;
field_width = LDBL_STRLEN_BOUND_L (decimal_point_len); field_width = LDBL_STRLEN_BOUND_L (decimal_point_len);
break;
default:
affirm (false);
} }
else
affirm (false);
break; break;
} }