-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn3160-add-min-max-to-c.tex
129 lines (101 loc) · 5.11 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
126
127
128
129
% SPDX-License-Identifier: CC0-1.0
\documentclass[a4paper,10pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage[]{mdframed}
\usepackage{listings}
\usepackage{url}
\usepackage{xcolor}
\usepackage{hyperref}
%opening
\title{N3160: Add min, max for integers to C}
\author{Christoph Grüninger\footnote{Unaffiliated, foss@grueninger.de}}
\date{2023-08-19}
\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} exists, 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}, Cairo \cite{Cairo}.
Stack Overflow has a question where to find C's \lstinline{min}/\lstinline{max} function 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}$ functions 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 following functions should be added to \lstinline{stdlib.h} implementing above definition:
\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}
As a consequence, the function-like macros \lstinline{min} and \lstinline{max} can be implemented.
% clause 7.1.4 in N3096
\section{Impact}
The introduction of the functions will conflict with existing code already using the names.
In most cases functions with the same name probably do exactly the same as described in this proposal.
Other uses of the names are imaginable like returning the smallest or biggest element from a set or vector.
This would lead to name clashes and compile errors.
Using a library version-test macro can help with backwards compatible code. We do not see the need of
a dedicated test macro \lstinline{__has_c_min_max} .
\section{Proposed wording}
We propose to add a new sub-section 7.24.6.2:
\begin{mdframed}
\subsection*{7.24.6.2 The \lstinline{min}, \lstinline{lmin}, \lstinline{llmin}, \lstinline{max}, and \lstinline{lmax}, \lstinline{llmax} functions}
\subsubsection*{Synopsis}
\begin{lstlisting}
#include <stdlib.h>;
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}
\subsubsection*{Description}
The \lstinline{min}, \lstinline{lmin}, and \lstinline{llmin} functions compute the smaller value of the two
integers \lstinline{a} and \lstinline{b}.
The \lstinline{max}, \lstinline{lmax}, and \lstinline{llmax} functions compute the bigger value of the two
integers \lstinline{a} and \lstinline{b}.
\subsubsection*{Returns}
The \lstinline{min}, \lstinline{lmin}, and \lstinline{llmin} functions return the smaller value.
The \lstinline{max}, \lstinline{lmax}, and \lstinline{llmax} functions return the bigger value.
\end{mdframed}
\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 Martin Uecker for encouragement and feedback on the first draft.
\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}