Issue 130429
Summary Add operators _Widthof, _Minof, _Maxof
Labels new issue
Assignees
Reporter alejandro-colomar
    Hi,

I'm going to submit a proposal to the C Committee to add these three operators with their obvious meaning.
We could maybe implement them as `__builtin_widthof`, `__builtin_minof`, `__builtin_maxof` for now, and then present that as prior art.  I've already shown a draft to the committee, and nobody has sent any messages opposing, and instead, I've seen two members already be in favor.

The proposal is below.  Also, here's a link to my git repo where I keep track of the proposal revisions:
<https://www.alejandro-colomar.es/src/alx/alx/wg14/alx-0002.git/>

Cheers,
Alex

---

```
Name
	alx-0002r1 - Add operators _Widthof, _Minof, _Maxof

Principles
	-   Codify existing practice to address evident deficiencies.

Category
	New operators.

Author
	Alejandro Colomar <a...@kernel.org>

	Cc: Martin Uecker <uec...@tugraz.at>

History
	<https://www.alejandro-colomar.es/src/alx/alx/wg14/alx-0002.git/>

	r0 (2025-03-08):
	-  Initial draft.

	r1 (2025-03-08):
	-  Fix typos.
	-  Add link to git repository in History.

Description
	Many projects need to invent their own macros for getting
	information about a type.  Basic ones are WIDTHOF(), MAXOF(),
	and MINOF(), which get the width of a type, the maximum value of
	a type, and the minimum value of a type, respectively.

	Here are my own implementations:

	#define WIDTHOF(x) (sizeof(x) * CHAR_BIT)
	#define SMAXOF(T)   ((T) (((((T) 1 << (WIDTHOF(T) - 2)) - 1) << 1) + 1))
	#define UMAXOF(T)   ((T) -1)
	#define IS_SIGNED(x) ((typeof(x)) -1 < 1)
	#define MAXOF(T)    ((T) (IS_SIGNED(T) ? SMAXOF(T) : UMAXOF(T))
	#define MINOF(T)    ((T) ~type_max(T))

	As can be seen from my implementations, they are hard to write
	correctly, and review.  Also, now that we have bit-precise
	integers, one may want to get these to work with them, which
	would make it even harder to implement them.

	Also, since these are used more often in macros, we have the
	added problem of macros expanding arguments more than once
	(sometimes way more than once; sometimes resulting in macro
	 expansions in the order of megabytes of text).

	If these three were keywords, true operators, the compiler would
	evaluate arguments exactly once, and would return the right
	value, even for bit-precise integers.

	Since these macros are typically used with type names --not
	expressions--, this proposal suggests only allowing a type name
	as the operand.  It is left as a matter of QoI if compilers want
	to provide support for unary-expressions as with sizeof.

Proposed wording
	Based on N3467.

    6.4.2  Keywords
	Add _Widthof, _Minof, _Maxof.

    6.5.4.1  Unary operators :: General
	@@ Syntax, p1, unary-_expression_
	 alignof ( type-name )
	+_Widthof ( type-name )
	+_Minof ( type-name )
	+_Maxof ( type-name )

    6.5.4.5 :: The sizeof, _Lengthof, and alignof operators
	@@ Title
	-The sizeof, _Lengthof, and alignof operators
	+The sizeof, _Lengthof, alignof, _Widthof, _Minof, and _Minof operators

	@@ Constraints, append to p1
	+The _Widthof, _Minof, and _Maxof operators
	+shall be applied to a parenthesized integer type.

	@@ Semantics, new p after p5
	+The _Widthof operator yields the width (in bits) of its operand type.
	+The operand is not evaluated,
	+and the result is an integer constant _expression_.

	@@ Semantics, new p after p5
	+The _Minof and _Maxof operators yield
	+the minimum and maximum value, respectively,
	+that can be represented by the type of the operand type.
	+The operand is not evaluated,
	+and the result is an integer constant _expression_.

    6.6.1 Constant expressions :: General
	@@ Semantics, p8
	 alignof expressions,
	+_Widthof expressions,
	+_Minof expressions,
	+_Maxof expressions,
	 and floating,
	...
	 _Lengthof operator,
	-or alignof operator.
	+alignof operator,
	+_Widthof operator,
	+_Minor operator,
	+or _Maxof operator.

	Update footnote 115?

	@@ p10
	 whose results are integer constant expressions,
	-and alignof expressions.
	+alignof expressions,
	+_Widthof expressions,
	+_Minof expressions,
	+and _Maxof expressions.
	...
	 _Lengthof operator,
	-or alignof operator.
	+alignof operator,
	+_Widthof operator,
	+_Minor operator,
	+or _Maxof operator.

    6.7.7.3 Declarators :: Array declarators
	@@ Semantics, p5
	 Where a size _expression_ is part of the operand of
	-an alignof operator,
	+an alignof operator,
	+a _Widthof operator,
	+a _Minof operator,
	+or a _Maxof operator,
	 that _expression_ is not evaluated.

    6.9.1  External definitions :: General
	@@ Constraints, p3
	 --  part of the operand of an alignof operator
	     whose result is an integer constant _expression_;
	+--  part of the operand of a _Widthof operator;
	+--  part of the operand of a _Minof operator;
	+--  part of the operand of a _Maxof operator;
	 --  part of the controlling _expression_ of a generic selection;

	@@ Semantics, p5
	 or alignof operator
	-whose result is an integer constant _expression_),
	+whose result is an integer constant _expression_,
	+or part of a
	+_Widthof,
	+_Minof,
	+_Maxof
	+operator),
	 somewhere in the entire program

    A.2.2  Language syntax summary :: Lexical grammar :: Keywords
	Update.

    A.3.1  Language syntax summary :: Phrase structure grammar :: Expressions
	Update 6.5.4.1.

    J.3.16  Portability issues :: I-d behavior :: Architecture
	Update 7.

    J.6.2  P. I. :: Reserved IDs and keywords :: Rule based identifiers
	Update 2.
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to