Changeset cd310f7


Ignore:
Timestamp:
Feb 14, 2018, 9:19:14 AM (7 years ago)
Author:
Peter A. Buhr <pabuhr@…>
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:
493d73a
Parents:
8f67d44
Message:

extend Library section with more examples

File:
1 edited

Legend:

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

    r8f67d44 rcd310f7  
    17911791struct Weight { double stones; };
    17921792
    1793 void ?{}( Weight & w ) { w.stones = 0; } $\C{// operations}$
     1793void ?{}( Weight & w ) { w.stones = 0; }        $\C{// operations}$
    17941794void ?{}( Weight & w, double w ) { w.stones = w; }
    17951795Weight ?+?( Weight l, Weight r ) { return (Weight){ l.stones + r.stones }; }
     
    18001800
    18011801int main() {
    1802         Weight w, hw = { 14 };                  $\C{// 14 stone}$
     1802        Weight w, hw = { 14 };                                  $\C{// 14 stone}$
    18031803        w = 11@`st@ + 1@`lb@;
    18041804        w = 70.3@`kg@;
    18051805        w = 155@`lb@;
    1806         w = 0x_9b_u@`lb@;                               $\C{// hexadecimal unsigned weight (155)}$
    1807         w = 0_233@`lb@;                                 $\C{// octal weight (155)}$
     1806        w = 0x_9b_u@`lb@;                                               $\C{// hexadecimal unsigned weight (155)}$
     1807        w = 0_233@`lb@;                                                 $\C{// octal weight (155)}$
    18081808        w = 5@`st@ + 8@`kg@ + 25@`lb@ + hw;
    18091809}
     
    18131813
    18141814\section{Libraries}
     1815
     1816As stated in Section~\ref{sec:poly-fns}, \CFA inherits a massive corpus of library code, where other programming languages must rewrite or provide fragile inter-language communication with C.
     1817\CFA has replacement libraries condensing hundreds of existing C functions into tens of \CFA overloaded functions, all without rewriting the actual computations.
     1818In many cases, the interface is an inline wrapper providing overloading during compilation but zero cost at runtime.
     1819The following sections give a gleams of the interface reduction to many C libraries.
     1820In 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.
     1821
     1822
     1823\subsection{Limits}
     1824
     1825C library @limits.h@ provides lower and upper bound constants for the basic types.
     1826\CFA name overloading is used to overload these constants, \eg:
     1827\begin{cquote}
     1828\lstDeleteShortInline@%
     1829\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
     1830\multicolumn{1}{c@{\hspace{3em}}}{\textbf{Definition}}  & \multicolumn{1}{c}{\textbf{Usage}}    \\
     1831\begin{cfa}
     1832const short int `MIN` = -32768;
     1833const int `MIN` = -2147483648;
     1834const long int `MIN` = -9223372036854775808L;
     1835\end{cfa}
     1836&
     1837\begin{cfa}
     1838short int si = `MIN`;
     1839int i = `MIN`;
     1840long int li = `MIN`;
     1841\end{cfa}
     1842\end{tabular}
     1843\lstMakeShortInline@%
     1844\end{cquote}
     1845The result is a significant reduction in names to access typed constants, \eg:
     1846\begin{cquote}
     1847\lstDeleteShortInline@%
     1848\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
     1849\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
     1850\begin{cfa}
     1851MIN,
     1852MAX,
     1853M_PI,
     1854M_E
     1855\end{cfa}
     1856&
     1857\begin{cfa}
     1858SCHAR_MIN, CHAR_MIN, SHRT_MIN, INT_MIN, LONG_MIN, LLONG_MIN,
     1859SCHAR_MAX, UCHAR_MAX, SHRT_MAX, INT_MAX, LONG_MAX, LLONG_MAX,
     1860M_PI, M_PIl, M_CPI, M_CPIl,
     1861M_E, M_El, M_CE, M_CEl
     1862\end{cfa}
     1863\end{tabular}
     1864\lstMakeShortInline@%
     1865\end{cquote}
     1866
     1867
     1868\subsection{Math}
     1869
     1870C library @math.h@ provides lower and upper bound constants for the basic types.
     1871\CFA name overloading is used to overload these constants, \eg:
     1872\begin{cquote}
     1873\lstDeleteShortInline@%
     1874\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
     1875\multicolumn{1}{c@{\hspace{3em}}}{\textbf{Definition}}  & \multicolumn{1}{c}{\textbf{Usage}}    \\
     1876\begin{cfa}
     1877float `log`( float x );
     1878double `log`( double );
     1879double _Complex `log`( double _Complex x );
     1880\end{cfa}
     1881&
     1882\begin{cfa}
     1883float f = `log`( 3.5 );
     1884double d = `log`( 3.5 );
     1885double _Complex dc = `log`( 3.5+0.5I );
     1886\end{cfa}
     1887\end{tabular}
     1888\lstMakeShortInline@%
     1889\end{cquote}
     1890The result is a significant reduction in names to access math routines, \eg:
     1891\begin{cquote}
     1892\lstDeleteShortInline@%
     1893\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
     1894\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
     1895\begin{cfa}
     1896log,
     1897sqrt,
     1898sin
     1899\end{cfa}
     1900&
     1901\begin{cfa}
     1902logf, log, logl, clogf, clog, clogl
     1903sqrtf, sqrt, sqrtl, csqrtf, csqrt, csqrtl
     1904sinf, sin, sinl, csinf, csin, csinl
     1905\end{cfa}
     1906\end{tabular}
     1907\lstMakeShortInline@%
     1908\end{cquote}
     1909
     1910
     1911\subsection{Standard}
     1912
     1913Library routines (@stdlib.h@) with multiple types.
     1914\begin{cquote}
     1915\lstDeleteShortInline@%
     1916\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
     1917\multicolumn{1}{c@{\hspace{3em}}}{\textbf{Definition}}  & \multicolumn{1}{c}{\textbf{Usage}}    \\
     1918\begin{cfa}
     1919unsigned int `abs`( int );
     1920double `abs`( double );
     1921double cabs( double _Complex );
     1922\end{cfa}
     1923&
     1924\begin{cfa}
     1925unsigned int i = `abs`( -1 );
     1926double d = `abs`( -1.5 );
     1927double d = `abs`( -1.5+0.5I );
     1928\end{cfa}
     1929\end{tabular}
     1930\lstMakeShortInline@%
     1931\end{cquote}
     1932The result is a significant reduction in names to access math routines, \eg:
     1933\begin{cquote}
     1934\lstDeleteShortInline@%
     1935\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
     1936\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
     1937\begin{cfa}
     1938abs,
     1939strto,
     1940random
     1941\end{cfa}
     1942&
     1943\begin{cfa}
     1944abs, labs, llabs, fabsf, fabs, fabsl, cabsf, cabs, cabsl
     1945strtol, strtoul, strtoll, strtoull, strtof, strtod, strtold
     1946srand48, mrand48, lrand48, drand48
     1947\end{cfa}
     1948\end{tabular}
     1949\lstMakeShortInline@%
     1950\end{cquote}
     1951
     1952The following shows one example where \CFA \emph{extends} an existing standard C interface to reduce complexity and provide safety.
     1953C/\Celeven provide a number of complex and overlapping storage-management operation to support the following capabilities:
     1954\begin{description}
     1955\item[fill]
     1956after allocation the storage is filled with a specified character.
     1957\item[resize]
     1958an existing allocation is decreased or increased in size.
     1959In 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.
     1960For an increase in storage size, new storage after the copied data may be filled.
     1961\item[alignment]
     1962an allocation starts on a specified memory boundary, \eg, an address multiple of 64 or 128 for cache-line purposes.
     1963\item[array]
     1964the allocation size is scaled to the specified number of array elements.
     1965An array may be filled, resized, or aligned.
     1966\end{description}
     1967Table~\ref{t:StorageManagementOperations} shows the capabilities provided by C/\Celeven allocation-routines and how all the capabilities can be combined into two \CFA routines.
     1968\begin{table}[h]
     1969\centering
     1970\lstDeleteShortInline@%
     1971\lstMakeShortInline~%
     1972\begin{tabular}{@{}r|r|l|l|l|l@{}}
     1973\multicolumn{1}{c}{}&           & \multicolumn{1}{c|}{fill}     & resize        & alignment     & array \\
     1974\hline
     1975C               & ~malloc~                      & no                    & no            & no            & no    \\
     1976                & ~calloc~                      & yes (0 only)  & no            & no            & yes   \\
     1977                & ~realloc~                     & no/copy               & yes           & no            & no    \\
     1978                & ~memalign~            & no                    & no            & yes           & no    \\
     1979                & ~posix_memalign~      & no                    & no            & yes           & no    \\
     1980\hline
     1981C11             & ~aligned_alloc~       & no                    & no            & yes           & no    \\
     1982\hline
     1983\CFA    & ~alloc~                       & no/copy/yes   & no/yes        & no            & yes   \\
     1984                & ~align_alloc~         & no/yes                & no            & yes           & yes   \\
     1985\end{tabular}
     1986\lstDeleteShortInline~%
     1987\lstMakeShortInline@%
     1988\caption{Storage-Management Operations}
     1989\label{t:StorageManagementOperations}
     1990\end{table}
     1991It is impossible to resize with alignment because the underlying @realloc@ allocates storage if more space is needed, and it does not honour alignment from the original allocation.
     1992
    18151993
    18161994\subsection{I/O}
     
    19022080There are routines to set and get the separator string, and manipulators to toggle separation on and off in the middle of output.
    19032081\end{list}
     2082The storage-management routines extend their C equivalents by overloading, alternate names, providing shallow type-safety, and removing the need to specify the allocation size for non-array types.
     2083
     2084
     2085\subsection{Multi-precision Integers}
     2086\label{s:MultiPrecisionIntegers}
     2087
     2088\CFA has an interface to the GMP multi-precision signed-integers~\cite{GMP}, similar to the \CC interface provided by GMP.
     2089The \CFA interface wraps GMP routines into operator routines to make programming with multi-precision integers identical to using fixed-sized integers.
     2090The \CFA type name for multi-precision signed-integers is @Int@ and the header file is @gmp@.
     2091The following factorial programs contrast using GMP with the \CFA and C interfaces.
     2092\begin{cquote}
     2093\lstDeleteShortInline@%
     2094\begin{tabular}{@{}l@{\hspace{\parindentlnth}}@{\hspace{\parindentlnth}}l@{}}
     2095\multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}     & \multicolumn{1}{@{\hspace{\parindentlnth}}c}{\textbf{C}}      \\
     2096\begin{cfa}
     2097#include <gmp>
     2098int main( void ) {
     2099        sout | "Factorial Numbers" | endl;
     2100        Int fact = 1;
     2101
     2102        sout | 0 | fact | endl;
     2103        for ( unsigned int i = 1; i <= 40; i += 1 ) {
     2104                fact *= i;
     2105                sout | i | fact | endl;
     2106        }
     2107}
     2108\end{cfa}
     2109&
     2110\begin{cfa}
     2111#include <gmp.h>
     2112int main( void ) {
     2113        `gmp_printf`( "Factorial Numbers\n" );
     2114        `mpz_t` fact;
     2115        `mpz_init_set_ui`( fact, 1 );
     2116        `gmp_printf`( "%d %Zd\n", 0, fact );
     2117        for ( unsigned int i = 1; i <= 40; i += 1 ) {
     2118                `mpz_mul_ui`( fact, fact, i );
     2119                `gmp_printf`( "%d %Zd\n", i, fact );
     2120        }
     2121}
     2122\end{cfa}
     2123\end{tabular}
     2124\lstMakeShortInline@%
     2125\end{cquote}
    19042126
    19052127
Note: See TracChangeset for help on using the changeset viewer.