From 03d1588fca2bffe92b08bb4f4ba69273f4843431 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Wed, 16 Dec 2020 23:52:24 -0800 Subject: [PATCH] idx: simplify IDX_MAX, remove IDX_WIDTH MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * lib/idx.h (IDX_MAX): Simplify by removing obsolete reference to UNSIGNED_IDX_T. (IDX_WIDTH): Remove, since it’s not used and its value arguably should be PTRDIFF_WIDTH anyway. --- ChangeLog | 8 ++++++++ lib/idx.h | 33 ++++++++++++++------------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/ChangeLog b/ChangeLog index d50539adca..445e2c0744 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2020-12-17 Paul Eggert + + idx: simplify IDX_MAX, remove IDX_WIDTH + * lib/idx.h (IDX_MAX): Simplify by removing obsolete reference + to UNSIGNED_IDX_T. + (IDX_WIDTH): Remove, since it’s not used and its value + arguably should be PTRDIFF_WIDTH anyway. + 2020-12-16 Bruno Haible posix_spawn_file_actions_addfchdir-tests: Rename test. diff --git a/lib/idx.h b/lib/idx.h index 0095467dcd..ad7d31a2bc 100644 --- a/lib/idx.h +++ b/lib/idx.h @@ -21,11 +21,12 @@ /* Get ptrdiff_t. */ #include -/* Get PTRDIFF_WIDTH. */ +/* Get PTRDIFF_MAX. */ #include /* The type 'idx_t' holds an (array) index or an (object) size. - Its implementation is a signed integer type, capable of holding the values + Its implementation promotes to a signed integer type, + which can hold the values 0..2^63-1 (on 64-bit platforms) or 0..2^31-1 (on 32-bit platforms). @@ -87,32 +88,26 @@ or to the C standard. Several programming languages (Ada, Haskell, Common Lisp, Pascal) already have range types. Such range types may help producing good code and good warnings. The type 'idx_t' could - then be typedef'ed to a (signed!) range type. */ + then be typedef'ed to a range type that is signed after promotion. */ -#if 0 -/* In the future, idx_t could be typedef'ed to a signed range type. */ -/* Note: The clang "extended integer types", supported in Clang 11 or newer +/* In the future, idx_t could be typedef'ed to a signed range type. + The clang "extended integer types", supported in Clang 11 or newer , are a special case of range types. However, these types don't support binary operators with plain integer types (e.g. expressions such as x > 1). Therefore, they don't behave like signed types (and not like unsigned types either). So, we cannot use them here. */ -typedef idx_t; -#else -/* Use the signed type 'ptrdiff_t' by default. */ + +/* Use the signed type 'ptrdiff_t'. */ /* Note: ISO C does not mandate that 'size_t' and 'ptrdiff_t' have the same - size, but it is so an all platforms we have seen since 1990. */ + size, but it is so on all platforms we have seen since 1990. */ typedef ptrdiff_t idx_t; -#endif /* IDX_MAX is the maximum value of an idx_t. */ -#if defined UNSIGNED_IDX_T -# define IDX_MAX SIZE_MAX -#else -# define IDX_MAX PTRDIFF_MAX -#endif - -/* IDX_WIDTH is the number of bits in an idx_t (31 or 63). */ -#define IDX_WIDTH (PTRDIFF_WIDTH - 1) +#define IDX_MAX PTRDIFF_MAX + +/* So far no need has been found for an IDX_WIDTH macro. + Perhaps there should be another macro IDX_VALUE_BITS that does not + count the sign bit and is therefore one less than PTRDIFF_WIDTH. */ #endif /* _IDX_H */ -- 2.39.5