Changeset cd310f7
- Timestamp:
- Feb 14, 2018, 9:19:14 AM (7 years ago)
- 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
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/papers/general/Paper.tex
r8f67d44 rcd310f7 1791 1791 struct Weight { double stones; }; 1792 1792 1793 void ?{}( Weight & w ) { w.stones = 0; } 1793 void ?{}( Weight & w ) { w.stones = 0; } $\C{// operations}$ 1794 1794 void ?{}( Weight & w, double w ) { w.stones = w; } 1795 1795 Weight ?+?( Weight l, Weight r ) { return (Weight){ l.stones + r.stones }; } … … 1800 1800 1801 1801 int main() { 1802 Weight w, hw = { 14 }; $\C{// 14 stone}$1802 Weight w, hw = { 14 }; $\C{// 14 stone}$ 1803 1803 w = 11@`st@ + 1@`lb@; 1804 1804 w = 70.3@`kg@; 1805 1805 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)}$ 1808 1808 w = 5@`st@ + 8@`kg@ + 25@`lb@ + hw; 1809 1809 } … … 1813 1813 1814 1814 \section{Libraries} 1815 1816 As 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. 1818 In many cases, the interface is an inline wrapper providing overloading during compilation but zero cost at runtime. 1819 The following sections give a gleams of the interface reduction to many C libraries. 1820 In 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 1825 C 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} 1832 const short int `MIN` = -32768; 1833 const int `MIN` = -2147483648; 1834 const long int `MIN` = -9223372036854775808L; 1835 \end{cfa} 1836 & 1837 \begin{cfa} 1838 short int si = `MIN`; 1839 int i = `MIN`; 1840 long int li = `MIN`; 1841 \end{cfa} 1842 \end{tabular} 1843 \lstMakeShortInline@% 1844 \end{cquote} 1845 The 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} 1851 MIN, 1852 MAX, 1853 M_PI, 1854 M_E 1855 \end{cfa} 1856 & 1857 \begin{cfa} 1858 SCHAR_MIN, CHAR_MIN, SHRT_MIN, INT_MIN, LONG_MIN, LLONG_MIN, 1859 SCHAR_MAX, UCHAR_MAX, SHRT_MAX, INT_MAX, LONG_MAX, LLONG_MAX, 1860 M_PI, M_PIl, M_CPI, M_CPIl, 1861 M_E, M_El, M_CE, M_CEl 1862 \end{cfa} 1863 \end{tabular} 1864 \lstMakeShortInline@% 1865 \end{cquote} 1866 1867 1868 \subsection{Math} 1869 1870 C 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} 1877 float `log`( float x ); 1878 double `log`( double ); 1879 double _Complex `log`( double _Complex x ); 1880 \end{cfa} 1881 & 1882 \begin{cfa} 1883 float f = `log`( 3.5 ); 1884 double d = `log`( 3.5 ); 1885 double _Complex dc = `log`( 3.5+0.5I ); 1886 \end{cfa} 1887 \end{tabular} 1888 \lstMakeShortInline@% 1889 \end{cquote} 1890 The 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} 1896 log, 1897 sqrt, 1898 sin 1899 \end{cfa} 1900 & 1901 \begin{cfa} 1902 logf, log, logl, clogf, clog, clogl 1903 sqrtf, sqrt, sqrtl, csqrtf, csqrt, csqrtl 1904 sinf, sin, sinl, csinf, csin, csinl 1905 \end{cfa} 1906 \end{tabular} 1907 \lstMakeShortInline@% 1908 \end{cquote} 1909 1910 1911 \subsection{Standard} 1912 1913 Library 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} 1919 unsigned int `abs`( int ); 1920 double `abs`( double ); 1921 double cabs( double _Complex ); 1922 \end{cfa} 1923 & 1924 \begin{cfa} 1925 unsigned int i = `abs`( -1 ); 1926 double d = `abs`( -1.5 ); 1927 double d = `abs`( -1.5+0.5I ); 1928 \end{cfa} 1929 \end{tabular} 1930 \lstMakeShortInline@% 1931 \end{cquote} 1932 The 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} 1938 abs, 1939 strto, 1940 random 1941 \end{cfa} 1942 & 1943 \begin{cfa} 1944 abs, labs, llabs, fabsf, fabs, fabsl, cabsf, cabs, cabsl 1945 strtol, strtoul, strtoll, strtoull, strtof, strtod, strtold 1946 srand48, mrand48, lrand48, drand48 1947 \end{cfa} 1948 \end{tabular} 1949 \lstMakeShortInline@% 1950 \end{cquote} 1951 1952 The following shows one example where \CFA \emph{extends} an existing standard C interface to reduce complexity and provide safety. 1953 C/\Celeven provide a number of complex and overlapping storage-management operation to support the following capabilities: 1954 \begin{description} 1955 \item[fill] 1956 after allocation the storage is filled with a specified character. 1957 \item[resize] 1958 an existing allocation is decreased or increased in size. 1959 In 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. 1960 For an increase in storage size, new storage after the copied data may be filled. 1961 \item[alignment] 1962 an allocation starts on a specified memory boundary, \eg, an address multiple of 64 or 128 for cache-line purposes. 1963 \item[array] 1964 the allocation size is scaled to the specified number of array elements. 1965 An array may be filled, resized, or aligned. 1966 \end{description} 1967 Table~\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 1975 C & ~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 1981 C11 & ~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} 1991 It 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 1815 1993 1816 1994 \subsection{I/O} … … 1902 2080 There are routines to set and get the separator string, and manipulators to toggle separation on and off in the middle of output. 1903 2081 \end{list} 2082 The 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. 2089 The \CFA interface wraps GMP routines into operator routines to make programming with multi-precision integers identical to using fixed-sized integers. 2090 The \CFA type name for multi-precision signed-integers is @Int@ and the header file is @gmp@. 2091 The 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> 2098 int 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> 2112 int 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} 1904 2126 1905 2127
Note: See TracChangeset
for help on using the changeset viewer.