-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn3160-add-min-max-to-c.tex
125 lines (96 loc) · 4.99 KB
/
n3160-add-min-max-to-c.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
% SPDX-License-Identifier: CC0-1.0
\documentclass[a4paper,10pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage[]{mdframed}
\usepackage{listings}
\usepackage{url}
\usepackage{xcolor}
\usepackage{hyperref}
\KOMAoptions{headings=small}
%opening
\title{N3160: Add min, max for integers to C}
\author{Christoph Grüninger\footnote{Unaffiliated, foss@grueninger.de}}
\date{2025-02-03}
\begin{document}
\lstset{language=C,basicstyle=\ttfamily}
\maketitle
\noindent
Document: N3160\\
Audience: WG14\\
Proposal category: New feature\\
Document hosted at \url{https://github.com/gruenich/n3160-add-min-max-to-c}
\section{Summary of proposed changes}
Add the functions \lstinline{min} and \lstinline{max} for integers to the C programming language.
\section{Rationale}
For floats and doubles, \lstinline{fmin} and \lstinline{fmax} exist -- but not for integer types. Other
programming languages provide them including C++ by \lstinline{std::min} and \lstinline{std::max}. Several
C libraries provide them as macros, too, e.g., GNU C library \cite{GLibc}, Linux \cite{Linux},
SuperLU \cite{SuperLU}, and Cairo \cite{Cairo}.
Stack Overflow has a question where to find C's \lstinline{min} / \lstinline{max} functions with more
than 400 up-votes \cite{Stackoverflow}, indicating that many C programmers expect this function to be
part of the C language.
Some may argue these two functions are trivial to implement. C provides other functions that would be
trivial to implement like \lstinline{abs} and \lstinline{fabs}.
\section{Proposal}
Add the $\operatorname{min}$ and $\operatorname{max}$ macros for integers to \lstinline{stdlib.h}.
The formal definitions of the two functions are
\begin{equation*}
\operatorname{min}(a, b) := \begin{cases}b & a > b\\ a & \text{else}\end{cases}
\end{equation*}
and
\begin{equation*}
\operatorname{max}(a, b) := \begin{cases}b & b > a\\ a & \text{else}\end{cases}.
\end{equation*}
The macros should be added to \lstinline{stdlib.h} implementing above definition.
\section{Discussion}
We are not sure what approach to follow. We see three alternatives:
\begin{enumerate}
\item[A.] Introduces macros \lstinline{min} / \lstinline{max} with their obvious names. The
libraries cited in the introduction proof these macro as helpful. It might cause major
disruptions as libraries are actively use these names.
\item[B.] Introduce macros \lstinline{stdc_min} / \lstinline{stdc_max} using the prefix \lstinline{stdc_} to
avoid name clashes with existing code. This is inconsistent with \lstinline{fmin} / \lstinline{fmax}.
\item[C.] Stay with the status quo and do not introduce any macro to calculate minimum and maximum for
integers. The disadvantages are too severe compared to the limited benefit.
\end{enumerate}
There is a bonus discussion to have functions similar to what C already provides for floats and doubles:
\begin{enumerate}
\item[BONUS.] Should we provide the following functions?
\begin{lstlisting}
int min (int a, int b);
int max (int a, int b);
long int lmin (long int a, long int b);
long int lmax (long int a, long int b);
long long int llmin (long long int a, long long int b);
long long int llmax (long long int a, long long int b);
\end{lstlisting}
Depending on the discussion of A. and B. they might also use the prefix \lstinline{stdc_}.
\end{enumerate}
As a consequence, the function-like macros \lstinline{min} and \lstinline{max} can be implemented.
% clause 7.1.4 in N3096
\section{Impact}
Using a library version-test macro like a dedicated test macro \lstinline{__has_c_min_max} can
help with the transition to keep code compatible to C implementations providing the macros and
those not providing them.
\section{Looking for help}
If someone is interested to chip in with experience, WG14 seniority, or as a champion to present the proposal in a committee meeting, we welcome additional authors to this proposal.
\section{Acknowledgments}
We would like to thank JeanHeyd Meneide for helpful discussions and stimuli.
Martin Uecker gave encourigin feedback on the first draft.
Thanks to ISO C++ Standards Committee Panel Discussion on CppCon 2024 for their request to formulate
issues as proposals to the committee, it made me pick up this topic after some time of inactivity.
\begin{thebibliography}{9}
\bibitem{GLibc}
GNU C Library source code containing macros for MIN and MAX, \url{https://sourceware.org/git/?p=glibc.git;a=blob;f=misc/sys/param.h;hb=HEAD}.
\bibitem{Linux}
Linux source code containing macros for MIN and MAX, \url{https://github.com/torvalds/linux/blob/master/include/linux/minmax.h}.
\bibitem{SuperLU}
SuperLU source code containing macros for MIN and MAX,\\ \url{https://github.com/xiaoyeli/superlu/blob/master/SRC/slu_util.h}.
\bibitem{Cairo}
Cairo source code containing macros for MIN and MAX,\\ \url{https://cgit.freedesktop.org/cairo/tree/perf/cairo-perf.h}.
\bibitem{Stackoverflow}
Stackoverflow question \emph{MIN and MAX in C},\\ \url{https://stackoverflow.com/questions/3437404/min-and-max-in-c}.
\end{thebibliography}
\end{document}