<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/130429>130429</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Add operators _Widthof, _Minof, _Maxof
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
alejandro-colomar
</td>
</tr>
</table>
<pre>
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 <alx@kernel.org>
Cc: Martin Uecker <uecker@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.
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzUWduO2zjSfhr1TUGCLLkPvugLtzs98f9PstlJguxdg5bKEqcpUiApy5qLffZFkTrZ7XQyAwwWOwgyMg91-Krqq5LCjOGFRLwPrh-C68cr1thS6Xsm8Hcmc63CTAlVMX21U3l3_54HySaI10G83gbJbQWF4rIAq8A0u4pbYFBrVSvDBC3aEmEDG1VV3FpEWmJ5TssGwZYaEVSNmlmlDbTclrTFNajdgavGQIVMcllEQbz-hpCpRuRQsW6HwKtaYIXS0o0KmIHgJn5-3jVcWC6fW57bUu2DmzhINqdbFZff2WBHtwF7pUGqlg4w6ayVUGs0XhuzpK3WXGlg2kYAhMQBgQmNLO_AlKqVwCDXbG8HFLIBg0GqVAQolMyAE8xkBxUawwo0oOpaGS6L4TCXxiLL6WevzCBKsK2CCqsdajNqJ2wk7NlB6cgH6kuJU1C4gR0K1UYAa2EUSSxRY5DcGmAguHwhi6sOCm5BY62gpX3YwgtiDVaz7AXU3vk0CtV44IYraYKUFAbpprS2dr-SpyB5ats2epVQEZogeTI6C5InJo7j322xWPofYRzHSVRwS1LSd96bTYmojc_CtcCjXw3D0D9QYP2feP2RVehWV4MwvYAQ1nk-y7rnb32qJBt4_uBywz25ZHAiP2kuM14LNF5YCAAblfN9B3jkxlIB1Jpll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