Ignore:
Timestamp:
Feb 16, 2018, 4:22:25 PM (8 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
5964127
Parents:
c71b256 (diff), 62cd621 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:/u/cforall/software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/papers/general/Paper.tex

    rc71b256 r7c782af  
    44\usepackage{epic,eepic}
    55\usepackage{xspace,calc,comment}
    6 \usepackage{upquote}                                                                    % switch curled `'" to straight
    7 \usepackage{listings}                                                                   % format program code
     6\usepackage{upquote}                                            % switch curled `'" to straight
     7\usepackage{listings}                                           % format program code
     8\usepackage{enumitem}
     9\usepackage[flushmargin]{footmisc}                      % support label/reference in footnote
    810\usepackage{rotating}
    911\usepackage[usenames]{color}
    10 \usepackage{pslatex}                                    % reduce size of san serif font
     12\usepackage{pslatex}                                            % reduce size of san serif font
    1113\usepackage[plainpages=false,pdfpagelabels,pdfpagemode=UseNone,pagebackref=true,breaklinks=true,colorlinks=true,linkcolor=blue,citecolor=blue,urlcolor=blue]{hyperref}
    1214
    1315\setlength{\textheight}{9in}
    1416%\oddsidemargin 0.0in
    15 \renewcommand{\topfraction}{0.8}                % float must be greater than X of the page before it is forced onto its own page
    16 \renewcommand{\bottomfraction}{0.8}             % float must be greater than X of the page before it is forced onto its own page
    17 \renewcommand{\floatpagefraction}{0.8}  % float must be greater than X of the page before it is forced onto its own page
    18 \renewcommand{\textfraction}{0.0}               % the entire page maybe devoted to floats with no text on the page at all
    19 
    20 \lefthyphenmin=4                                                % hyphen only after 4 characters
     17\renewcommand{\topfraction}{0.8}                        % float must be greater than X of the page before it is forced onto its own page
     18\renewcommand{\bottomfraction}{0.8}                     % float must be greater than X of the page before it is forced onto its own page
     19\renewcommand{\floatpagefraction}{0.8}          % float must be greater than X of the page before it is forced onto its own page
     20\renewcommand{\textfraction}{0.0}                       % the entire page maybe devoted to floats with no text on the page at all
     21
     22\lefthyphenmin=4                                                        % hyphen only after 4 characters
    2123\righthyphenmin=4
    2224
     
    2426
    2527\newcommand{\CFAIcon}{\textsf{C}\raisebox{\depth}{\rotatebox{180}{\textsf{A}}}\xspace} % Cforall symbolic name
    26 \newcommand{\CFA}{\protect\CFAIcon} % safe for section/caption
    27 \newcommand{\CFL}{\textrm{Cforall}\xspace} % Cforall symbolic name
    28 \newcommand{\Celeven}{\textrm{C11}\xspace} % C11 symbolic name
     28\newcommand{\CFA}{\protect\CFAIcon}             % safe for section/caption
     29\newcommand{\CFL}{\textrm{Cforall}\xspace}      % Cforall symbolic name
     30\newcommand{\Celeven}{\textrm{C11}\xspace}      % C11 symbolic name
    2931\newcommand{\CC}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}\xspace} % C++ symbolic name
    3032\newcommand{\CCeleven}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}11\xspace} % C++11 symbolic name
     
    5658\newcommand{\LstCommentStyle}[1]{{\lst@basicstyle{\lst@commentstyle{#1}}}}
    5759
    58 \newlength{\gcolumnposn}                                % temporary hack because lstlisting does not handle tabs correctly
     60\newlength{\gcolumnposn}                                        % temporary hack because lstlisting does not handle tabs correctly
    5961\newlength{\columnposn}
    6062\setlength{\gcolumnposn}{2.75in}
     
    7274
    7375% Latin abbreviation
    74 \newcommand{\abbrevFont}{\textit}       % set empty for no italics
     76\newcommand{\abbrevFont}{\textit}                       % set empty for no italics
    7577\newcommand{\EG}{\abbrevFont{e}.\abbrevFont{g}.}
    7678\newcommand*{\eg}{%
     
    103105
    104106\newenvironment{cquote}{%
    105         \list{}{\lstset{resetmargins=true,aboveskip=0pt,belowskip=0pt}\topsep=4pt\parsep=0pt\leftmargin=\parindent\rightmargin\leftmargin}%
     107        \list{}{\lstset{resetmargins=true,aboveskip=0pt,belowskip=0pt}\topsep=4pt\parsep=0pt\leftmargin=\parindentlnth\rightmargin\leftmargin}%
    106108        \item\relax
    107109}{%
     
    187189
    188190
    189 \section{Introduction and Background}
     191\section{Introduction}
    190192
    191193The C programming language is a foundational technology for modern computing with millions of lines of code implementing everything from commercial operating-systems to hobby projects.
     
    193195The TIOBE~\cite{TIOBE} ranks the top 5 most popular programming languages as: Java 16\%, \Textbf{C 7\%}, \Textbf{\CC 5\%}, \Csharp 4\%, Python 4\% = 36\%, where the next 50 languages are less than 3\% each with a long tail.
    194196The top 3 rankings over the past 30 years are:
    195 \lstDeleteShortInline@%
    196197\begin{center}
    197198\setlength{\tabcolsep}{10pt}
     199\lstDeleteShortInline@%
    198200\begin{tabular}{@{}rccccccc@{}}
    199201                & 2017  & 2012  & 2007  & 2002  & 1997  & 1992  & 1987          \\ \hline
     
    202204\CC             & 3             & 3             & 3             & 3             & 2             & 2             & 4                     \\
    203205\end{tabular}
     206\lstMakeShortInline@%
    204207\end{center}
    205 \lstMakeShortInline@%
    206208Love it or hate it, C is extremely popular, highly used, and one of the few systems languages.
    207209In many cases, \CC is often used solely as a better C.
     
    224226The new constructs are empirically compared with both standard C and \CC; the results show the new design is comparable in performance.
    225227
    226 
    227 \subsection{Polymorphic Functions}
     228\section{Polymorphic Functions}
     229
     230\CFA introduces both ad-hoc and parametric polymorphism to C, with a design originally formalized by Ditchfield~\cite{Ditchfield92}, and first implemented by Bilson~\cite{Bilson03}.
     231
     232\subsection{Name Overloading}
     233
     234C already has a limited form of ad-hoc polymorphism in the form of its basic arithmetic operators, which apply to a variety of different types using identical syntax.
     235\CFA extends the built-in operator overloading by allowing users to define overloads for any function, not just operators, and even any variable; Section~\ref{sec:libraries} includes a number of examples of how this overloading simplifies \CFA programming relative to C.
     236Code generation for these overloaded functions and variables is implemented by the usual approach of mangling the identifier names to include a representation of their type, while \CFA decides which overload to apply based on the same ``usual arithmetic conversions'' used in C to disambiguate operator overloads.
     237As an example:
     238
     239\begin{cfa}
     240int max(int a, int b) { return a < b ? b : a; }  // (1)
     241double max(double a, double b) { return a < b ? b : a; }  // (2)
     242
     243int max = INT_MAX;     // (3)
     244double max = DBL_MAX;  // (4)
     245
     246max(7, -max);   $\C{// uses (1) and (3), by matching int from constant 7}$
     247max(max, 3.14); $\C{// uses (2) and (4), by matching double from constant 3.14}$
     248
     249//max(max, -max);  $\C{// ERROR: ambiguous}$
     250int m = max(max, -max); $\C{// uses (1) once and (3) twice, by matching return type}$
     251\end{cfa}
     252
     253\Celeven did add @_Generic@ expressions, which can be used in preprocessor macros to provide a form of ad-hoc polymorphism; however, this polymorphism is both functionally and ergonomically inferior to \CFA name overloading.
     254The macro wrapping the generic expression imposes some limitations; as an example, it could not implement the example above, because the variables @max@ would collide with the functions @max@.
     255Ergonomic limitations of @_Generic@ include the necessity to put a fixed list of supported types in a single place and manually dispatch to appropriate overloads, as well as possible namespace pollution from the functions dispatched to, which must all have distinct names.
     256
     257\subsection{\texorpdfstring{\LstKeywordStyle{forall} Functions}{forall Functions}}
    228258\label{sec:poly-fns}
    229259
    230 \CFA{}\hspace{1pt}'s polymorphism was originally formalized by Ditchfield~\cite{Ditchfield92}, and first implemented by Bilson~\cite{Bilson03}.
    231260The signature feature of \CFA is parametric-polymorphic functions~\cite{forceone:impl,Cormack90,Duggan96} with functions generalized using a @forall@ clause (giving the language its name):
    232261\begin{lstlisting}
     
    257286Crucial to the design of a new programming language are the libraries to access thousands of external software features.
    258287Like \CC, \CFA inherits a massive compatible library-base, where other programming languages must rewrite or provide fragile inter-language communication with C.
    259 A simple example is leveraging the existing type-unsafe (@void *@) C @bsearch@ to binary search a sorted floating-point array:
     288A simple example is leveraging the existing type-unsafe (@void *@) C @bsearch@ to binary search a sorted float array:
    260289\begin{lstlisting}
    261290void * bsearch( const void * key, const void * base, size_t nmemb, size_t size,
     
    263292int comp( const void * t1, const void * t2 ) { return *(double *)t1 < *(double *)t2 ? -1 :
    264293                                *(double *)t2 < *(double *)t1 ? 1 : 0; }
    265 double key = 5.0, vals[10] = { /* 10 sorted floating-point values */ };
     294double key = 5.0, vals[10] = { /* 10 sorted float values */ };
    266295double * val = (double *)bsearch( &key, vals, 10, sizeof(vals[0]), comp );      $\C{// search sorted array}$
    267296\end{lstlisting}
     
    304333Hence, programmers can easily form local environments, adding and modifying appropriate functions, to maximize reuse of other existing functions and types.
    305334
    306 Finally, \CFA allows variable overloading:
    307 \begin{lstlisting}
    308 short int MAX = ...;   int MAX = ...;  double MAX = ...;
    309 short int s = MAX;    int i = MAX;    double d = MAX;   $\C{// select correct MAX}$
    310 \end{lstlisting}
    311 Here, the single name @MAX@ replaces all the C type-specific names: @SHRT_MAX@, @INT_MAX@, @DBL_MAX@.
     335%% Redundant with Section~\ref{sec:libraries} %%
     336
     337% Finally, \CFA allows variable overloading:
     338% \begin{lstlisting}
     339% short int MAX = ...;   int MAX = ...;  double MAX = ...;
     340% short int s = MAX;    int i = MAX;    double d = MAX;   $\C{// select correct MAX}$
     341% \end{lstlisting}
     342% Here, the single name @MAX@ replaces all the C type-specific names: @SHRT_MAX@, @INT_MAX@, @DBL_MAX@.
    312343
    313344\subsection{Traits}
     
    505536In many languages, functions can return at most one value;
    506537however, many operations have multiple outcomes, some exceptional.
    507 Consider C's @div@ and @remquo@ functions, which return the quotient and remainder for a division of integer and floating-point values, respectively.
     538Consider C's @div@ and @remquo@ functions, which return the quotient and remainder for a division of integer and float values, respectively.
    508539\begin{lstlisting}
    509540typedef struct { int quo, rem; } div_t;         $\C{// from include stdlib.h}$
     
    936967
    937968\section{Control Structures}
     969
     970\CFA identifies missing and problematic control structures in C, and extends and modifies these control structures to increase functionality and safety.
    938971
    939972
     
    10441077The implicit targets of the current @continue@ and @break@, \ie the closest enclosing loop or @switch@, change as certain constructs are added or removed.
    10451078
     1079
    10461080\subsection{\texorpdfstring{Enhanced \LstKeywordStyle{switch} Statement}{Enhanced switch Statement}}
    10471081
    1048 \CFA also fixes a number of ergonomic defecits in the @switch@ statements of standard C.
    1049 C can specify a number of equivalent cases by using the default ``fall-through'' semantics of @case@ clauses, \eg @case 1: case 2: case 3:@ -- this syntax is cluttered, however, so \CFA includes a more concise list syntax, @case 1, 2, 3:@.
    1050 For contiguous ranges, \CFA provides an even more concise range syntax as well, @case 1~3:@; lists of ranges are also allowed in case selectors.
    1051 
     1082There are a number of deficiencies with the C @switch@ statements: enumerating @case@ lists, placement of @case@ clauses, scope of the switch body, and fall through between case clauses.
     1083
     1084C has no shorthand for specifying a list of case values, whether the list is non-contiguous or contiguous\footnote{C provides this mechanism via fall through.}.
     1085\CFA provides a shorthand for a non-contiguous list:
     1086\begin{cquote}
     1087\lstDeleteShortInline@%
     1088\begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{}}
     1089\multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}     & \multicolumn{1}{c}{\textbf{C}}        \\
     1090\begin{cfa}
     1091case 2, 10, 34, 42:
     1092\end{cfa}
     1093&
     1094\begin{cfa}
     1095case 2: case 10: case 34: case 42:
     1096\end{cfa}
     1097\end{tabular}
     1098\lstMakeShortInline@%
     1099\end{cquote}
     1100for a contiguous list:\footnote{gcc provides the same mechanism with awkward syntax, \lstinline@2 ... 42@, where spaces are required around the ellipse.}
     1101\begin{cquote}
     1102\lstDeleteShortInline@%
     1103\begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{}}
     1104\multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}     & \multicolumn{1}{c}{\textbf{C}}        \\
     1105\begin{cfa}
     1106case 2~42:
     1107\end{cfa}
     1108&
     1109\begin{cfa}
     1110case 2: case 3: ... case 41: case 42:
     1111\end{cfa}
     1112\end{tabular}
     1113\lstMakeShortInline@%
     1114\end{cquote}
     1115and a combination:
     1116\begin{cfa}
     1117case -12~-4, -1~5, 14~21, 34~42:
     1118\end{cfa}
     1119
     1120C allows placement of @case@ clauses \emph{within} statements nested in the @switch@ body (see Duff's device~\cite{Duff83});
     1121\begin{cfa}
     1122switch ( i ) {
     1123  case 0:
     1124        for ( int i = 0; i < 10; i += 1 ) {
     1125                ...
     1126  `case 1:`             // no initialization of loop index
     1127                ...
     1128        }
     1129}
     1130\end{cfa}
     1131\CFA precludes this form of transfer into a control structure because it causes undefined behaviour, especially with respect to missed initialization, and provides very limited functionality.
     1132
     1133C allows placement of declaration within the @switch@ body and unreachable code at the start, resulting in undefined behaviour:
     1134\begin{cfa}
     1135switch ( x ) {
     1136        `int y = 1;`                            $\C{// unreachable initialization}$
     1137        `x = 7;`                                        $\C{// unreachable code without label/branch}$
     1138  case 0:
     1139        ...
     1140        `int z = 0;`                            $\C{// unreachable initialization, cannot appear after case}$
     1141        z = 2;
     1142  case 1:
     1143        `x = z;`                                        $\C{// without fall through, z is undefined}$
     1144}
     1145\end{cfa}
     1146\CFA allows the declaration of local variables, \eg @y@, at the start of the @switch@ with scope across the entire @switch@ body, \ie all @case@ clauses, but no statements.
     1147\CFA disallows the declaration of local variable, \eg @z@, directly within the @switch@ body, because a declaration cannot occur immediately after a @case@ since a label can only be attached to a statement, and the use of @z@ is undefined in @case 1@ as neither storage allocation nor initialization may have occurred.
     1148
     1149C @switch@ provides multiple entry points into the statement body, but once an entry point is selected, control continues across \emph{all} @case@ clauses until the end of the @switch@ body, called \newterm{fall through};
     1150@case@ clauses are made disjoint by the @break@ statement.
     1151While the ability to fall through \emph{is} a useful form of control flow, it does not match well with programmer intuition, resulting in many errors from missing @break@ statements.
     1152\CFA provides a new control structure, @choose@, which mimics @switch@, but reverses the meaning of fall through:
     1153\begin{cquote}
     1154\lstDeleteShortInline@%
     1155\begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{}}
     1156\multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}     & \multicolumn{1}{c}{\textbf{C}}        \\
     1157\begin{cfa}
     1158`choose` ( day ) {
     1159  case Mon~Thu:
     1160        // program
     1161
     1162  case Fri:
     1163        // program
     1164        wallet += pay;
     1165        `fallthrough;`
     1166  case Sat:
     1167        // party
     1168        wallet -= party;
     1169
     1170  case Sun:
     1171        // rest
     1172
     1173  default:
     1174        // error
     1175}
     1176\end{cfa}
     1177&
     1178\begin{cfa}
     1179switch ( day ) {
     1180  case Mon: case Tue: case Wed: case Thu:
     1181        // program
     1182        `break;`
     1183  case Fri:
     1184        // program
     1185        wallet += pay;
     1186
     1187  case Sat:
     1188        // party
     1189        wallet -= party;
     1190        `break;`
     1191  case Sun:
     1192        // rest
     1193        `break;`
     1194  default:
     1195        // error
     1196}
     1197\end{cfa}
     1198\end{tabular}
     1199\lstMakeShortInline@%
     1200\end{cquote}
     1201Collectively, these enhancements reduce programmer burden and increase readability and safety.
     1202
     1203\begin{comment}
    10521204Forgotten @break@ statements at the end of @switch@ cases are a persistent sort of programmer error in C, and the @break@ statements themselves introduce visual clutter and an un-C-like keyword-based block delimiter.
    10531205\CFA addresses this error by introducing a @choose@ statement, which works identically to a @switch@ except that its default end-of-case behaviour is to break rather than to fall through for all non-empty cases.
     
    10701222}
    10711223\end{cfa}
     1224\end{comment}
     1225
    10721226
    10731227\subsection{\texorpdfstring{\LstKeywordStyle{with} Clause / Statement}{with Clause / Statement}}
     
    12061360\end{cfa}
    12071361
    1208 
    1209 \subsection{Exception Handling ???}
    1210 
     1362% \subsection{Exception Handling ???}
    12111363
    12121364\section{Declarations}
     
    12551407\lstDeleteShortInline@%
    12561408\lstset{moredelim=**[is][\color{blue}]{+}{+}}
    1257 \begin{tabular}{@{}l@{\hspace{3em}}l@{}}
    1258 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
     1409\begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{}}
     1410\multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}     & \multicolumn{1}{c}{\textbf{C}}        \\
    12591411\begin{cfa}
    12601412+[5] *+ `int` x1;
    12611413+* [5]+ `int` x2;
    1262 +[* [5] int]+ f`( int p )`;
     1414`[* [5] int]` f+( int p )+;
    12631415\end{cfa}
    12641416&
     
    12661418`int` +*+ x1 +[5]+;
    12671419`int` +(*+x2+)[5]+;
    1268 +int (*+f`( int p )`+)[5]+;
     1420`int (*`f+( int p )+`)[5]`;
    12691421\end{cfa}
    12701422\end{tabular}
     
    12771429\begin{cquote}
    12781430\lstDeleteShortInline@%
    1279 \begin{tabular}{@{}l@{\hspace{3em}}l@{}}
    1280 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
     1431\begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{}}
     1432\multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}     & \multicolumn{1}{c}{\textbf{C}}        \\
    12811433\begin{cfa}
    12821434`*` int x, y;
     
    12921444\begin{cquote}
    12931445\lstDeleteShortInline@%
    1294 \begin{tabular}{@{}l@{\hspace{3em}}l@{}}
    1295 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
     1446\begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{}}
     1447\multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}     & \multicolumn{1}{c}{\textbf{C}}        \\
    12961448\begin{cfa}
    12971449`*` int x;
     
    13101462\begin{cquote}
    13111463\lstDeleteShortInline@%
    1312 \begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}}
    1313 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\
     1464\begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{\hspace{\parindentlnth}}l@{}}
     1465\multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}     & \multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{C}}      \\
    13141466\begin{cfa}
    13151467[ 5 ] int z;
     
    13511503\begin{cquote}
    13521504\lstDeleteShortInline@%
    1353 \begin{tabular}{@{}l@{\hspace{1em}}l@{\hspace{1em}}l@{}}
    1354 \multicolumn{1}{c@{\hspace{1em}}}{\textbf{\CFA}}        & \multicolumn{1}{c@{\hspace{1em}}}{\textbf{C}} \\
     1505\begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{\hspace{\parindentlnth}}l@{}}
     1506\multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}     & \multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{C}}      \\
    13551507\begin{cfa}
    13561508const * const int x;
     
    13741526\begin{cquote}
    13751527\lstDeleteShortInline@%
    1376 \begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}}
    1377 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\
     1528\begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{\hspace{\parindentlnth}}l@{}}
     1529\multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}     & \multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{C}}      \\
    13781530\begin{cfa}
    13791531extern [ 5 ] int x;
     
    13971549\begin{cquote}
    13981550\lstDeleteShortInline@%
    1399 \begin{tabular}{@{}l@{\hspace{3em}}l@{}}
    1400 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
     1551\begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{}}
     1552\multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}     & \multicolumn{1}{c}{\textbf{C}}        \\
    14011553\begin{cfa}
    14021554y = (* int)x;
     
    14151567Therefore, a programmer has the option of either continuing to use traditional C declarations or take advantage of the new style.
    14161568Clearly, both styles need to be supported for some time due to existing C-style header-files, particularly for UNIX-like systems.
     1569
     1570The syntax of the new routine prototype declaration follows directly from the new routine definition syntax;
     1571as well, parameter names are optional, \eg:
     1572\begin{cfa}
     1573[ int x ] f ();                                                 $\C{// returning int with no parameters}$
     1574[ * int ] g (int y);                                    $\C{// returning pointer to int with int parameter}$
     1575[ ] h ( int, char );                                    $\C{// returning no result with int and char parameters}$
     1576[ * int, int ] j ( int );                               $\C{// returning pointer to int and int, with int parameter}$
     1577\end{cfa}
     1578This syntax allows a prototype declaration to be created by cutting and pasting source text from the routine definition header (or vice versa).
     1579Like C, it is possible to declare multiple routine-prototypes in a single declaration, where the return type is distributed across \emph{all} routine names in the declaration list, \eg:
     1580\begin{cquote}
     1581\lstDeleteShortInline@%
     1582\begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{}}
     1583\multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}     & \multicolumn{1}{c}{\textbf{C}}        \\
     1584\begin{cfa}
     1585[double] foo(), foo( int ), foo( double ) {...}
     1586\end{cfa}
     1587&
     1588\begin{cfa}
     1589double foo1(), foo2( int ), foo3( double );
     1590\end{cfa}
     1591\end{tabular}
     1592\lstMakeShortInline@%
     1593\end{cquote}
     1594\CFA allows the last routine in the list to define its body.
     1595
     1596Declaration qualifiers can only appear at the start of a \CFA routine declaration,\footref{StorageClassSpecifier} \eg:
     1597\begin{cfa}
     1598extern [ int ] f ( int );
     1599static [ int ] g ( int );
     1600\end{cfa}
     1601
     1602The syntax for pointers to \CFA routines specifies the pointer name on the right, \eg:
     1603\begin{cfa}
     1604* [ int x ] () fp;                                              $\C{// pointer to routine returning int with no parameters}$
     1605* [ * int ] (int y) gp;                                 $\C{// pointer to routine returning pointer to int with int parameter}$
     1606* [ ] (int,char) hp;                                    $\C{// pointer to routine returning no result with int and char parameters}$
     1607* [ * int,int ] ( int ) jp;                             $\C{// pointer to routine returning pointer to int and int, with int parameter}$
     1608\end{cfa}
     1609While parameter names are optional, \emph{a routine name cannot be specified};
     1610for example, the following is incorrect:
     1611\begin{cfa}
     1612* [ int x ] f () fp;                                    $\C{// routine name "f" is not allowed}$
     1613\end{cfa}
    14171614
    14181615
     
    15931790In addition to the expressive power, \lstinline|@=| provides a simple path for migrating legacy C code to \CFA, by providing a mechanism to incrementally convert initializers; the \CFA design team decided to introduce a new syntax for this escape hatch because we believe that our RAII implementation will handle the vast majority of code in a desirable way, and we wished to maintain familiar syntax for this common case.
    15941791
     1792
     1793\subsection{Type Nesting}
     1794
     1795\CFA allows \newterm{type nesting}, and type qualification of the nested types (see Figure~\ref{f:TypeNestingQualification}), where as C hoists (refactors) nested types into the enclosing scope and has no type qualification.
     1796\begin{figure}
     1797\centering
     1798\lstDeleteShortInline@%
     1799\begin{tabular}{@{}l@{\hspace{3em}}l|l@{}}
     1800\multicolumn{1}{c@{\hspace{3em}}}{\textbf{C Type Nesting}}      & \multicolumn{1}{c}{\textbf{C Implicit Hoisting}}      & \multicolumn{1}{|c}{\textbf{\CFA}}    \\
     1801\hline
     1802\begin{cfa}
     1803struct S {
     1804        enum C { R, G, B };
     1805        struct T {
     1806                union U { int i, j; };
     1807                enum C c;
     1808                short int i, j;
     1809        };
     1810        struct T t;
     1811} s;
     1812
     1813int rtn() {
     1814        s.t.c = R;
     1815        struct T t = { R, 1, 2 };
     1816        enum C c;
     1817        union U u;
     1818}
     1819\end{cfa}
     1820&
     1821\begin{cfa}
     1822enum C { R, G, B };
     1823union U { int i, j; };
     1824struct T {
     1825        enum C c;
     1826        short int i, j;
     1827};
     1828struct S {
     1829        struct T t;
     1830} s;
     1831       
     1832
     1833
     1834
     1835
     1836
     1837
     1838\end{cfa}
     1839&
     1840\begin{cfa}
     1841struct S {
     1842        enum C { R, G, B };
     1843        struct T {
     1844                union U { int i, j; };
     1845                enum C c;
     1846                short int i, j;
     1847        };
     1848        struct T t;
     1849} s;
     1850
     1851int rtn() {
     1852        s.t.c = `S.`R;  // type qualification
     1853        struct `S.`T t = { `S.`R, 1, 2 };
     1854        enum `S.`C c;
     1855        union `S.T.`U u;
     1856}
     1857\end{cfa}
     1858\end{tabular}
     1859\lstMakeShortInline@%
     1860\caption{Type Nesting / Qualification}
     1861\label{f:TypeNestingQualification}
     1862\end{figure}
     1863In the left example in C, types @C@, @U@ and @T@ are implicitly hoisted outside of type @S@ into the containing block scope.
     1864In the right example in \CFA, the types are not hoisted and accessed using the field-selection operator ``@.@'' for type qualification, as does Java, rather than the \CC type-selection operator ``@::@''.
     1865
     1866
    15951867\subsection{Default Parameters}
    15961868
     
    15981870\section{Literals}
    15991871
    1600 C already includes limited polymorphism for literals -- @0@ can be either an integer or a pointer literal, depending on context, while the syntactic forms of literals of the various integer and floating-point types are very similar, differing from each other only in suffix.
     1872C already includes limited polymorphism for literals -- @0@ can be either an integer or a pointer literal, depending on context, while the syntactic forms of literals of the various integer and float types are very similar, differing from each other only in suffix.
    16011873In keeping with the general \CFA approach of adding features while respecting ``the C way'' of doing things, we have extended both C's polymorphic zero and typed literal syntax to interoperate with user-defined types, while maintaining a backwards-compatible semantics.
    16021874
     
    16221894struct Weight { double stones; };
    16231895
    1624 void ?{}( Weight & w ) { w.stones = 0; } $\C{// operations}$
     1896void ?{}( Weight & w ) { w.stones = 0; }        $\C{// operations}$
    16251897void ?{}( Weight & w, double w ) { w.stones = w; }
    16261898Weight ?+?( Weight l, Weight r ) { return (Weight){ l.stones + r.stones }; }
     
    16311903
    16321904int main() {
    1633         Weight w, hw = { 14 };                  $\C{// 14 stone}$
     1905        Weight w, hw = { 14 };                                  $\C{// 14 stone}$
    16341906        w = 11@`st@ + 1@`lb@;
    16351907        w = 70.3@`kg@;
    16361908        w = 155@`lb@;
    1637         w = 0x_9b_u@`lb@;                               $\C{// hexadecimal unsigned weight (155)}$
    1638         w = 0_233@`lb@;                                 $\C{// octal weight (155)}$
     1909        w = 0x_9b_u@`lb@;                                               $\C{// hexadecimal unsigned weight (155)}$
     1910        w = 0_233@`lb@;                                                 $\C{// octal weight (155)}$
    16391911        w = 5@`st@ + 8@`kg@ + 25@`lb@ + hw;
    16401912}
    16411913\end{cfa}
    16421914}%
     1915
     1916
     1917\section{Libraries}
     1918\label{sec:libraries}
     1919
     1920As stated in Section~\ref{sec:poly-fns}, \CFA inherits a large corpus of library code, where other programming languages must rewrite or provide fragile inter-language communication with C.
     1921\CFA has replacement libraries condensing hundreds of existing C names into tens of \CFA overloaded names, all without rewriting the actual computations.
     1922In many cases, the interface is an inline wrapper providing overloading during compilation but zero cost at runtime.
     1923The following sections give a glimpse of the interface reduction to many C libraries.
     1924In many cases, @signed@/@unsigned@ @char@ and @short@ routines are available (but not shown) to ensure expression computations remain in a single type, as conversions can distort results.
     1925
     1926
     1927\subsection{Limits}
     1928
     1929C library @limits.h@ provides lower and upper bound constants for the basic types.
     1930\CFA name overloading is used to condense these typed constants, \eg:
     1931\begin{cquote}
     1932\lstDeleteShortInline@%
     1933\begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{}}
     1934\multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{Definition}}       & \multicolumn{1}{c}{\textbf{Usage}}    \\
     1935\begin{cfa}
     1936const short int `MIN` = -32768;
     1937const int `MIN` = -2147483648;
     1938const long int `MIN` = -9223372036854775808L;
     1939\end{cfa}
     1940&
     1941\begin{cfa}
     1942short int si = `MIN`;
     1943int i = `MIN`;
     1944long int li = `MIN`;
     1945\end{cfa}
     1946\end{tabular}
     1947\lstMakeShortInline@%
     1948\end{cquote}
     1949The result is a significant reduction in names to access typed constants, \eg:
     1950\begin{cquote}
     1951\lstDeleteShortInline@%
     1952\begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{}}
     1953\multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}     & \multicolumn{1}{c}{\textbf{C}}        \\
     1954\begin{cfa}
     1955MIN
     1956MAX
     1957M_PI
     1958M_E
     1959\end{cfa}
     1960&
     1961\begin{cfa}
     1962SCHAR_MIN, CHAR_MIN, SHRT_MIN, INT_MIN, LONG_MIN, LLONG_MIN,
     1963SCHAR_MAX, UCHAR_MAX, SHRT_MAX, INT_MAX, LONG_MAX, LLONG_MAX,
     1964M_PI, M_PIl, M_CPI, M_CPIl,
     1965M_E, M_El, M_CE, M_CEl
     1966\end{cfa}
     1967\end{tabular}
     1968\lstMakeShortInline@%
     1969\end{cquote}
     1970
     1971
     1972\subsection{Math}
     1973
     1974C library @math.h@ provides many mathematical routines.
     1975\CFA routine overloading is used to condense these mathematical routines, \eg:
     1976\begin{cquote}
     1977\lstDeleteShortInline@%
     1978\begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{}}
     1979\multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{Definition}}       & \multicolumn{1}{c}{\textbf{Usage}}    \\
     1980\begin{cfa}
     1981float `log`( float x );
     1982double `log`( double );
     1983double _Complex `log`( double _Complex x );
     1984\end{cfa}
     1985&
     1986\begin{cfa}
     1987float f = `log`( 3.5 );
     1988double d = `log`( 3.5 );
     1989double _Complex dc = `log`( 3.5+0.5I );
     1990\end{cfa}
     1991\end{tabular}
     1992\lstMakeShortInline@%
     1993\end{cquote}
     1994The result is a significant reduction in names to access math routines, \eg:
     1995\begin{cquote}
     1996\lstDeleteShortInline@%
     1997\begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{}}
     1998\multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}     & \multicolumn{1}{c}{\textbf{C}}        \\
     1999\begin{cfa}
     2000log
     2001sqrt
     2002sin
     2003\end{cfa}
     2004&
     2005\begin{cfa}
     2006logf, log, logl, clogf, clog, clogl
     2007sqrtf, sqrt, sqrtl, csqrtf, csqrt, csqrtl
     2008sinf, sin, sinl, csinf, csin, csinl
     2009\end{cfa}
     2010\end{tabular}
     2011\lstMakeShortInline@%
     2012\end{cquote}
     2013While \Celeven has type-generic math~\cite[\S~7.25]{C11} in @tgmath.h@ to provide a similar mechanism, these macros are limited, matching a routine name with a single set of floating type(s).
     2014For example, it is not possible to overload @atan@ for both one and two arguments;
     2015instead the names @atan@ and @atan2@ are required.
     2016The key observation is that only a restricted set of type-generic macros are provided for a limited set of routine names, which do not generalize across the type system, as in \CFA.
     2017
     2018
     2019\subsection{Standard}
     2020
     2021C library @stdlib.h@ provides many general routines.
     2022\CFA routine overloading is used to condense these utility routines, \eg:
     2023\begin{cquote}
     2024\lstDeleteShortInline@%
     2025\begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{}}
     2026\multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{Definition}}       & \multicolumn{1}{c}{\textbf{Usage}}    \\
     2027\begin{cfa}
     2028unsigned int `abs`( int );
     2029double `abs`( double );
     2030double abs( double _Complex );
     2031\end{cfa}
     2032&
     2033\begin{cfa}
     2034unsigned int i = `abs`( -1 );
     2035double d = `abs`( -1.5 );
     2036double d = `abs`( -1.5+0.5I );
     2037\end{cfa}
     2038\end{tabular}
     2039\lstMakeShortInline@%
     2040\end{cquote}
     2041The result is a significant reduction in names to access utility routines, \eg:
     2042\begin{cquote}
     2043\lstDeleteShortInline@%
     2044\begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{}}
     2045\multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}     & \multicolumn{1}{c}{\textbf{C}}        \\
     2046\begin{cfa}
     2047abs
     2048strto
     2049random
     2050\end{cfa}
     2051&
     2052\begin{cfa}
     2053abs, labs, llabs, fabsf, fabs, fabsl, cabsf, cabs, cabsl
     2054strtol, strtoul, strtoll, strtoull, strtof, strtod, strtold
     2055srand48, mrand48, lrand48, drand48
     2056\end{cfa}
     2057\end{tabular}
     2058\lstMakeShortInline@%
     2059\end{cquote}
     2060In additon, there are polymorphic routines, like @min@ and @max@, which work on any type with operators @?<?@ or @?>?@.
     2061
     2062The following shows one example where \CFA \emph{extends} an existing standard C interface to reduce complexity and provide safety.
     2063C/\Celeven provide a number of complex and overlapping storage-management operation to support the following capabilities:
     2064\begin{description}[itemsep=2pt,parsep=0pt]
     2065\item[fill]
     2066after allocation the storage is filled with a specified character.
     2067\item[resize]
     2068an existing allocation is decreased or increased in size.
     2069In either case, new storage may or may not be allocated and, if there is a new allocation, as much data from the existing allocation is copied.
     2070For an increase in storage size, new storage after the copied data may be filled.
     2071\item[alignment]
     2072an allocation starts on a specified memory boundary, \eg, an address multiple of 64 or 128 for cache-line purposes.
     2073\item[array]
     2074the allocation size is scaled to the specified number of array elements.
     2075An array may be filled, resized, or aligned.
     2076\end{description}
     2077Table~\ref{t:StorageManagementOperations} shows the capabilities provided by C/\Celeven allocation-routines and how all the capabilities can be combined into two \CFA routines.
     2078
     2079\CFA storage-management routines extend the C equivalents by overloading, providing shallow type-safety, and removing the need to specify the base allocation-size.
     2080The following example contrasts \CFA and C storage-allocation operation performing the same operations with the same type safety:
     2081\begin{cquote}
     2082\begin{cfa}[aboveskip=0pt]
     2083size_t  dim = 10;                                                       $\C{// array dimension}$
     2084char fill = '\xff';                                                     $\C{// initialization fill value}$
     2085int * ip;
     2086\end{cfa}
     2087\lstDeleteShortInline@%
     2088\begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{}}
     2089\multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}     & \multicolumn{1}{c}{\textbf{C}}        \\
     2090\begin{cfa}
     2091ip = alloc();
     2092ip = alloc( fill );
     2093ip = alloc( dim );
     2094ip = alloc( dim, fill );
     2095ip = alloc( ip, 2 * dim );
     2096ip = alloc( ip, 4 * dim, fill );
     2097
     2098ip = align_alloc( 16 );
     2099ip = align_alloc( 16, fill );
     2100ip = align_alloc( 16, dim );
     2101ip = align_alloc( 16, dim, fill );
     2102\end{cfa}
     2103&
     2104\begin{cfa}
     2105ip = (int *)malloc( sizeof( int ) );
     2106ip = (int *)malloc( sizeof( int ) ); memset( ip, fill, sizeof( int ) );
     2107ip = (int *)malloc( dim * sizeof( int ) );
     2108ip = (int *)malloc( sizeof( int ) ); memset( ip, fill, dim * sizeof( int ) );
     2109ip = (int *)realloc( ip, 2 * dim * sizeof( int ) );
     2110ip = (int *)realloc( ip, 4 * dim * sizeof( int ) ); memset( ip, fill, 4 * dim * sizeof( int ) );
     2111
     2112ip = memalign( 16, sizeof( int ) );
     2113ip = memalign( 16, sizeof( int ) ); memset( ip, fill, sizeof( int ) );
     2114ip = memalign( 16, dim * sizeof( int ) );
     2115ip = memalign( 16, dim * sizeof( int ) ); memset( ip, fill, dim * sizeof( int ) );
     2116\end{cfa}
     2117\end{tabular}
     2118\lstMakeShortInline@%
     2119\end{cquote}
     2120Variadic @new@ (see Section~\ref{sec:variadic-tuples}) cannot support the same overloading because extra parameters are for initialization.
     2121Hence, there are @new@ and @anew@ routines for single and array variables, and the fill value is the arguments to the constructor, \eg:
     2122\begin{cfa}
     2123struct S { int i, j; };
     2124void ?{}( S & s, int i, int j ) { s.i = i; s.j = j; }
     2125S * s = new( 2, 3 );                                            $\C{// allocate storage and run constructor}$
     2126S * as = anew( dim, 2, 3 );                                     $\C{// each array element initialized to 2, 3}$
     2127\end{cfa}
     2128Note, \CC can only initialization array elements via the default constructor.
     2129
     2130Finally, the \CFA memory-allocator has \newterm{sticky properties} for dynamic storage: fill and alignment are remembered with an object's storage in the heap.
     2131When a @realloc@ is performed, the sticky properties are respected, so that new storage is correctly aligned and initialized with the fill character.
     2132
     2133\begin{table}
     2134\centering
     2135\lstDeleteShortInline@%
     2136\lstMakeShortInline~%
     2137\begin{tabular}{@{}r|r|l|l|l|l@{}}
     2138\multicolumn{1}{c}{}&           & \multicolumn{1}{c|}{fill}     & resize        & alignment     & array \\
     2139\hline
     2140C               & ~malloc~                      & no                    & no            & no            & no    \\
     2141                & ~calloc~                      & yes (0 only)  & no            & no            & yes   \\
     2142                & ~realloc~                     & no/copy               & yes           & no            & no    \\
     2143                & ~memalign~            & no                    & no            & yes           & no    \\
     2144                & ~posix_memalign~      & no                    & no            & yes           & no    \\
     2145\hline
     2146C11             & ~aligned_alloc~       & no                    & no            & yes           & no    \\
     2147\hline
     2148\CFA    & ~alloc~                       & yes/copy              & no/yes        & no            & yes   \\
     2149                & ~align_alloc~         & yes                   & no            & yes           & yes   \\
     2150\end{tabular}
     2151\lstDeleteShortInline~%
     2152\lstMakeShortInline@%
     2153\caption{Storage-Management Operations}
     2154\label{t:StorageManagementOperations}
     2155\end{table}
     2156
     2157
     2158\subsection{I/O}
     2159\label{s:IOLibrary}
     2160
     2161The goal of \CFA I/O is to simplify the common cases, while fully supporting polymorphism and user defined types in a consistent way.
     2162The approach combines ideas from \CC and Python.
     2163The \CFA header file for the I/O library is @fstream@.
     2164
     2165The common case is printing out a sequence of variables separated by whitespace.
     2166\begin{cquote}
     2167\lstDeleteShortInline@%
     2168\begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{}}
     2169\multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}     & \multicolumn{1}{c}{\textbf{\CC}}      \\
     2170\begin{cfa}
     2171int x = 1, y = 2, z = 3;
     2172sout | x `|` y `|` z | endl;
     2173\end{cfa}
     2174&
     2175\begin{cfa}
     2176
     2177cout << x `<< " "` << y `<< " "` << z << endl;
     2178\end{cfa}
     2179\\
     2180\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
     21811` `2` `3
     2182\end{cfa}
     2183&
     2184\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
     21851 2 3
     2186\end{cfa}
     2187\end{tabular}
     2188\lstMakeShortInline@%
     2189\end{cquote}
     2190The \CFA form has half the characters of the \CC form, and is similar to Python I/O with respect to implicit separators.
     2191Similar simplification occurs for tuple I/O, which prints all tuple values separated by ``\lstinline[showspaces=true]@, @''.
     2192\begin{cfa}
     2193[int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 4, [ 5, 6 ] ];
     2194sout | t1 | t2 | endl;                                  $\C{// print tuples}$
     2195\end{cfa}
     2196\begin{cfa}[showspaces=true,aboveskip=0pt]
     21971`, `2`, `3 4`, `5`, `6
     2198\end{cfa}
     2199Finally, \CFA uses the logical-or operator for I/O as it is the lowest-priority overloadable operator, other than assignment.
     2200Therefore, fewer output expressions require parenthesis.
     2201\begin{cquote}
     2202\lstDeleteShortInline@%
     2203\begin{tabular}{@{}ll@{}}
     2204\textbf{\CFA:}
     2205&
     2206\begin{cfa}
     2207sout | x * 3 | y + 1 | z << 2 | x == y | (x | y) | (x || y) | (x > z ? 1 : 2) | endl;
     2208\end{cfa}
     2209\\
     2210\textbf{\CC:}
     2211&
     2212\begin{cfa}
     2213cout << x * 3 << y + 1 << `(`z << 2`)` << `(`x == y`)` << (x | y) << (x || y) << (x > z ? 1 : 2) << endl;
     2214\end{cfa}
     2215\\
     2216\textbf{output:}
     2217&
     2218\begin{cfa}[showspaces=true,aboveskip=0pt]
     22193 3 12 0 3 1 2
     2220\end{cfa}
     2221\end{tabular}
     2222\lstMakeShortInline@%
     2223\end{cquote}
     2224There is a weak similarity between the \CFA logical-or operator and the Shell pipe-operator for moving data, where data flows in the correct direction for input but the opposite direction for output.
     2225
     2226The implicit separator character (space/blank) is a separator not a terminator.
     2227The rules for implicitly adding the separator are:
     2228\begin{itemize}[itemsep=2pt,parsep=0pt]
     2229\item
     2230A separator does not appear at the start or end of a line.
     2231\item
     2232A separator does not appear before or after a character literal or variable.
     2233\item
     2234A separator does not appear before or after a null (empty) C string, which is a local mechanism to disable insertion of the separator character.
     2235\item
     2236A separator does not appear before a C string starting with the characters: \lstinline[mathescape=off,basicstyle=\tt]@([{=$@
     2237\item
     2238A seperator does not appear after a C string ending with the characters: \lstinline[basicstyle=\tt]@,.;!?)]}%@
     2239\item
     2240{\lstset{language=CFA,deletedelim=**[is][]{`}{`}}
     2241A seperator does not appear before or after a C string begining/ending with the quote or whitespace characters: \lstinline[basicstyle=\tt,showspaces=true]@`'": \t\v\f\r\n@
     2242}%
     2243\item
     2244There are routines to set and get the separator string, and manipulators to toggle separation on and off in the middle of output.
     2245\end{itemize}
     2246
     2247
     2248\subsection{Multi-precision Integers}
     2249\label{s:MultiPrecisionIntegers}
     2250
     2251\CFA has an interface to the GMP multi-precision signed-integers~\cite{GMP}, similar to the \CC interface provided by GMP.
     2252The \CFA interface wraps GMP routines into operator routines to make programming with multi-precision integers identical to using fixed-sized integers.
     2253The \CFA type name for multi-precision signed-integers is @Int@ and the header file is @gmp@.
     2254The following multi-precision factorial programs contrast using GMP with the \CFA and C interfaces.
     2255\begin{cquote}
     2256\lstDeleteShortInline@%
     2257\begin{tabular}{@{}l@{\hspace{\parindentlnth}}@{\hspace{\parindentlnth}}l@{}}
     2258\multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}     & \multicolumn{1}{@{\hspace{\parindentlnth}}c}{\textbf{C}}      \\
     2259\begin{cfa}
     2260#include <gmp>
     2261int main( void ) {
     2262        sout | "Factorial Numbers" | endl;
     2263        Int fact = 1;
     2264
     2265        sout | 0 | fact | endl;
     2266        for ( unsigned int i = 1; i <= 40; i += 1 ) {
     2267                fact *= i;
     2268                sout | i | fact | endl;
     2269        }
     2270}
     2271\end{cfa}
     2272&
     2273\begin{cfa}
     2274#include <gmp.h>
     2275int main( void ) {
     2276        `gmp_printf`( "Factorial Numbers\n" );
     2277        `mpz_t` fact;
     2278        `mpz_init_set_ui`( fact, 1 );
     2279        `gmp_printf`( "%d %Zd\n", 0, fact );
     2280        for ( unsigned int i = 1; i <= 40; i += 1 ) {
     2281                `mpz_mul_ui`( fact, fact, i );
     2282                `gmp_printf`( "%d %Zd\n", i, fact );
     2283        }
     2284}
     2285\end{cfa}
     2286\end{tabular}
     2287\lstMakeShortInline@%
     2288\end{cquote}
     2289
    16432290
    16442291\section{Evaluation}
     
    17042351
    17052352\begin{table}
     2353\centering
    17062354\caption{Properties of benchmark code}
    17072355\label{tab:eval}
Note: See TracChangeset for help on using the changeset viewer.