- Timestamp:
- Jan 27, 2024, 1:52:20 PM (20 months ago)
- Branches:
- master
- Children:
- dd1ebb1
- Parents:
- 01fb70a
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
r01fb70a rd1551a5 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Sun Jan 14 17:27:41202414 %% Update Count : 5 76413 %% Last Modified On : Mon Jan 22 11:59:04 2024 14 %% Update Count : 5961 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 46 46 %\input{common} % common CFA document macros 47 47 \usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref} 48 \urlstyle{sf} 48 49 \usepackage{breakurl} 49 50 … … 163 164 164 165 \Index*[C++]{\CC{}}~\cite{c++:v1} had a similar goal 30 years ago, allowing object-oriented programming to be incrementally added to C. 165 However, \CC currently has the disadvantages of a strong object-oriented bias, multiple legacy design-choices that are difficult to update, and active divergence of the language model from C, requiring significant effort and training to incrementally add \CC to a C -based project.166 However, \CC currently has the disadvantages of a strong object-oriented bias, multiple legacy design-choices that are difficult to update, and active divergence of the language model from C, requiring significant effort and training to incrementally add \CC to a C code-base. 166 167 In contrast, \CFA has 30 years of hindsight and a clean starting point. 167 168 … … 200 201 \end{center} 201 202 While \CFA I/O \see{\VRef{s:StreamIOLibrary}} looks similar to \Index*[C++]{\CC{}}, there are important differences, such as automatic spacing between variables and an implicit newline at the end of the expression list, similar to \Index*{Python}~\cite{Python}. 203 In general, \CFA programs are 10\% to 30\% shorter than their equivalent C/\CC counterparts. 202 204 203 205 … … 218 220 Even with all its problems, C continues to be popular because it allows writing software at virtually any level in a computer system without restriction. 219 221 For system programming, where direct access to hardware, storage management, and real-time issues are a requirement, C is the only language of choice. 220 The TIOBE index~\cite{TIOBE} for February 202 1ranks the top six most \emph{popular} programming languages as C 17.4\%, \Index*{Java} 12\%, Python 12\%, \Index*[C++]{\CC{}} 7.6\%, \Csharp 4\%, Visual Basic 3.8\% = 56.8\%, where the next 50 languages are less than 2\% each, with a long tail.222 The TIOBE index~\cite{TIOBE} for February 2023 ranks the top six most \emph{popular} programming languages as C 17.4\%, \Index*{Java} 12\%, Python 12\%, \Index*[C++]{\CC{}} 7.6\%, \Csharp 4\%, Visual Basic 3.8\% = 56.8\%, where the next 50 languages are less than 2\% each, with a long tail. 221 223 The top 4 rankings over the past 35 years are: 222 224 \begin{center} … … 238 240 however, it largely extended the C language, and did not address many of C's existing problems.\footnote{% 239 241 Two important existing problems addressed were changing the type of character literals from ©int© to ©char© and enumerator from ©int© to the type of its enumerators.} 240 \Index*{Fortran}~\cite{Fortran08}, \Index*{Cobol}~\cite{Cobol14}, and \Index*{Ada}~\cite{Ada1 2} are examples of programming languages that took an evolutionary approach, where modern language-features (\eg objects, concurrency) are added and problems fixed within the framework of the existing language.242 \Index*{Fortran}~\cite{Fortran08}, \Index*{Cobol}~\cite{Cobol14}, and \Index*{Ada}~\cite{Ada16} are examples of programming languages that took an evolutionary approach, where modern language-features (\eg objects, concurrency) are added and problems fixed within the framework of the existing language. 241 243 \Index*{Java}~\cite{Java8}, \Index*{Go}~\cite{Go}, \Index*{Rust}~\cite{Rust} and \Index*{D}~\cite{D} are examples of the revolutionary approach for modernizing C/\CC, resulting in a new language rather than an extension of the descendent. 242 244 These languages have different syntax and semantics from C, do not interoperate directly with C, and are not systems languages because of restrictive memory-management or garbage collection. … … 263 265 % extending the C type system with parametric polymorphism and overloading, as opposed to the \Index*[C++]{\CC{}} approach of object-oriented extensions. 264 266 \CFA{}\hspace{1pt}'s polymorphism was originally formalized by \Index*{Glen Ditchfield}\index{Ditchfield, Glen}~\cite{Ditchfield92}, and first implemented by \Index*{Richard Bilson}\index{Bilson, Richard}~\cite{Bilson03}. 265 However, at that time, there was little interest ingin extending C, so work did not continue.267 However, at that time, there was little interest in extending C, so work did not continue. 266 268 As the saying goes, ``\Index*{What goes around, comes around.}'', and there is now renewed interest in the C programming language because of the legacy code-base, so the \CFA project was restarted in 2015. 267 269 … … 272 274 \CFA is designed to integrate directly with existing C programs and libraries. 273 275 The most important feature of \Index{interoperability} is using the same \Index{calling convention}s, so there is no complex interface or overhead to call existing C routines. 274 This feature allows \CFA programmers to take advantage of the existing panoply of C libraries to access thousands of externalsoftware features.276 This feature allows \CFA programmers to take advantage of the existing panoply of C libraries to access thousands of software features. 275 277 Language developers often state that adequate \Index{library support} takes more work than designing and implementing the language itself. 276 278 Fortunately, \CFA, like \Index*[C++]{\CC{}}, starts with immediate access to all exiting C libraries, and in many cases, can easily wrap library routines with simpler and safer interfaces, at zero or very low cost. … … 320 322 However, it is necessary to differentiate between C and \CFA code because of name \Index{overload}ing, as for \CC. 321 323 For example, the C math-library provides the following routines for computing the absolute value of the basic types: ©abs©, ©labs©, ©llabs©, ©fabs©, ©fabsf©, ©fabsl©, ©cabsf©, ©cabs©, and ©cabsl©. 322 Whereas, \CFA wraps each ofthese routines into one overloaded name ©abs©:323 \begin{cfa} 324 char ®abs®( char ); 325 extern "C" { int ®abs®( int ); } §\C{// use default C routine for int}§326 long int ®abs®( long int ); 327 long long int ®abs®( long long int ); 328 float ®abs®( float ); 329 double ®abs®( double ); 330 long double ®abs®( long double ); 331 float _Complex ®abs®( float _Complex ); 332 double _Complex ®abs®( double _Complex ); 333 long double _Complex ®abs®( long double _Complex ); 324 Whereas, \CFA wraps these routines into one overloaded name ©abs©: 325 \begin{cfa} 326 unsigned char ®abs®( signed char ); §\C[3.5in]{// no C equivalent}§ 327 extern "C" { int ®abs®( int ); } §\C{// C abs}§ 328 unsigned long int ®abs®( long int ); §\C{// C labs}§ 329 unsigned long long int ®abs®( long long int ); §\C{// C llabs}§ 330 float ®abs®( float ); §\C{// C fabsf}§ 331 double ®abs®( double ); §\C{// C fabs}§ 332 long double ®abs®( long double ); §\C{// C fabsl}§ 333 float _Complex ®abs®( float _Complex ); §\C{// C cabsf}§ 334 double _Complex ®abs®( double _Complex ); §\C{// C cabs}§ 335 long double _Complex ®abs®( long double _Complex ); §\C{// C cabsl}\CRT§ 334 336 \end{cfa} 335 337 The problem is a \Index{name clash} between the C name ©abs© and the \CFA names ©abs©, resulting in two name linkages\index{C linkage}: ©extern "C"© and ©extern "Cforall"© (default). … … 339 341 340 342 This example illustrates a core idea in \CFA: \emph{the \Index{power of a name}}. 341 The name ``©abs©'' evokes the notion of absolute value ,and many mathematical types provide the notion of absolute value.342 Hence, knowing the name ©abs© is sufficient to apply it to any type where it is applicable.343 The name ``©abs©'' evokes the notion of absolute value and many mathematical types provide the notion of absolute value. 344 Hence, knowing the name ©abs© is sufficient to apply it to any applicable type. 343 345 The time savings and safety of using one name uniformly versus $N$ unique names cannot be underestimated. 344 346 345 347 346 348 \section{\CFA Compilation} 349 350 \CFA is a \newterm{transpiler}, meaning it reads in a programming language (\CFA) as input and generates another programming language (C) as output, whereas a \newterm{compiler} reads in a programming language and generates assembler/machine code. 351 Hence, \CFA is like the C preprocessor modifying a program and sending it on to another step for further transformation. 352 The order of transformation is C preprocessor, \CFA, and finally GNU C compiler, which also has a number of transformation steps, such as assembler and linker. 347 353 348 354 The command ©cfa© is used to compile a \CFA program and is based on the \Index{GNU} \Indexc{gcc} command, \eg: … … 444 450 which conditionally includes the correct header file, if the program is compiled using \Indexc{gcc} or \Indexc{cfa}. 445 451 446 The \CFA translator has multiplesteps.447 The following flags control how the translator works, the stages run, and printing within a stage.452 The \CFA \Index{transpiler} has multiple internal steps. 453 The following flags control how the \CFA transpiler works, the stages run, and printing within a stage. 448 454 The majority of these flags are used by \CFA developers, but some are occasionally useful to programmers. 449 Each option must be escaped with \Indexc{-XCFA}\index{trans lator option!-XCFA@{©-XCFA©}} to direct it to the compilerstep, similar to the ©-Xlinker© flag for the linker, \eg:455 Each option must be escaped with \Indexc{-XCFA}\index{transpiler option!-XCFA@{©-XCFA©}} to direct it to the \CFA compilation step, similar to the ©-Xlinker© flag for the linker, \eg: 450 456 \begin{lstlisting}[language=sh] 451 457 cfa §test§.cfa -CFA -XCFA -p # print translated code without printing the standard prelude … … 458 464 \begin{description}[topsep=5pt,itemsep=0pt,parsep=0pt] 459 465 \item 460 \Indexc{-c}\index{trans lator option!-c@{©-c©}}, \Indexc{--colors}\index{translator option!--colors@{©--colors©}} \, diagnostic color: ©never©, ©always©, \lstinline[deletekeywords=auto]{auto}461 \item 462 \Indexc{-g}\index{trans lator option!-g@{©-g©}}, \Indexc{--gdb}\index{translator option!--gdb@{©--gdb©}} \, wait for gdb to attach463 \item 464 \Indexc{-h}\index{trans lator option!-h@{©-h©}}, \Indexc{--help}\index{translator option!--help@{©--help©}} \, print translator help message465 \item 466 \Indexc{-i}\index{trans lator option!-i@{©-i©}}, \Indexc{--invariant}\index{translator option!--invariant@{©--invariant©}} \, invariant checking during AST passes467 \item 468 \Indexc{-l}\index{trans lator option!-l@{©-l©}}, \Indexc{--libcfa}\index{translator option!--libcfa@{©--libcfa©}} \, generate ©libcfa.c©469 \item 470 \Indexc{-L}\index{trans lator option!-L@{©-L©}}, \Indexc{--linemarks}\index{translator option!--linemarks@{©--linemarks©}} \, generate line marks471 \item 472 \Indexc{-m}\index{trans lator option!-m@{©-m©}}, \Indexc{--no-main}\index{translator option!--no-main@{©--no-main©}} \, do not replace main473 \item 474 \Indexc{-N}\index{trans lator option!-N@{©-N©}}, \Indexc{--no-linemarks}\index{translator option!--no-linemarks@{©--no-linemarks©}} \, do not generate line marks475 \item 476 \Indexc{-n}\index{trans lator option!-n@{©-n©}}, \Indexc{--no-prelude}\index{translator option!--no-prelude@{©--no-prelude©}} \, do not read prelude477 \item 478 \Indexc{-p}\index{trans lator option!-p@{©-p©}}, \Indexc{--prototypes}\index{translator option!--prototypes@{©--prototypes©}} \, do not generate prelude prototypes $\Rightarrow$ prelude not printed479 \item 480 \Indexc{-d}\index{trans lator option!-d@{©-d©}}, \Indexc{--deterministic-out}\index{translator option!--deterministic-out@{©--deterministic-out©}} \, only print deterministic output481 \item 482 \Indexc{-P}\index{trans lator option!-P@{©-P©}}, \Indexc{--print}\index{translator option!--print@{©--print©}} \, one of:466 \Indexc{-c}\index{transpiler option!-c@{©-c©}}, \Indexc{--colors}\index{transpiler option!--colors@{©--colors©}} \, diagnostic color: ©never©, ©always©, \lstinline[deletekeywords=auto]{auto} 467 \item 468 \Indexc{-g}\index{transpiler option!-g@{©-g©}}, \Indexc{--gdb}\index{transpiler option!--gdb@{©--gdb©}} \, wait for gdb to attach 469 \item 470 \Indexc{-h}\index{transpiler option!-h@{©-h©}}, \Indexc{--help}\index{transpiler option!--help@{©--help©}} \, print transpiler help message 471 \item 472 \Indexc{-i}\index{transpiler option!-i@{©-i©}}, \Indexc{--invariant}\index{transpiler option!--invariant@{©--invariant©}} \, invariant checking during AST passes 473 \item 474 \Indexc{-l}\index{transpiler option!-l@{©-l©}}, \Indexc{--libcfa}\index{transpiler option!--libcfa@{©--libcfa©}} \, generate ©libcfa.c© 475 \item 476 \Indexc{-L}\index{transpiler option!-L@{©-L©}}, \Indexc{--linemarks}\index{transpiler option!--linemarks@{©--linemarks©}} \, generate line marks 477 \item 478 \Indexc{-m}\index{transpiler option!-m@{©-m©}}, \Indexc{--no-main}\index{transpiler option!--no-main@{©--no-main©}} \, do not replace main 479 \item 480 \Indexc{-N}\index{transpiler option!-N@{©-N©}}, \Indexc{--no-linemarks}\index{transpiler option!--no-linemarks@{©--no-linemarks©}} \, do not generate line marks 481 \item 482 \Indexc{-n}\index{transpiler option!-n@{©-n©}}, \Indexc{--no-prelude}\index{transpiler option!--no-prelude@{©--no-prelude©}} \, do not read prelude 483 \item 484 \Indexc{-p}\index{transpiler option!-p@{©-p©}}, \Indexc{--prototypes}\index{transpiler option!--prototypes@{©--prototypes©}} \, do not generate prelude prototypes $\Rightarrow$ prelude not printed 485 \item 486 \Indexc{-d}\index{transpiler option!-d@{©-d©}}, \Indexc{--deterministic-out}\index{transpiler option!--deterministic-out@{©--deterministic-out©}} \, only print deterministic output 487 \item 488 \Indexc{-P}\index{transpiler option!-P@{©-P©}}, \Indexc{--print}\index{transpiler option!--print@{©--print©}} \, one of: 483 489 \begin{description}[topsep=0pt,itemsep=0pt,parsep=0pt] 484 490 \item 485 \Indexc{ascodegen}\index{trans lator option!-P@{©-P©}!©ascodegen©}\index{translator option!--print@{©-print©}!©ascodegen©} \, print AST as codegen rather than AST486 \item 487 \Indexc{asterr}\index{trans lator option!-P@{©-P©}!©asterr©}\index{translator option!--print@{©-print©}!©asterr©} \, print AST on error488 \item 489 \Indexc{declstats}\index{trans lator option!-P@{©-P©}!©declstats©}\index{translator option!--print@{©-print©}!©declstats©} \, print code property statistics490 \item 491 \Indexc{parse}\index{trans lator option!-P@{©-P©}!©parse©}\index{translator option!--print@{©-print©}!©parse©} \, print yacc (parsing) debug information492 \item 493 \Indexc{pretty}\index{trans lator option!-P@{©-P©}!©pretty©}\index{translator option!--print@{©-print©}!©pretty©} \, prettyprint for ©ascodegen© flag494 \item 495 \Indexc{rproto}\index{trans lator option!-P@{©-P©}!©rproto©}\index{translator option!--print@{©-print©}!©rproto©} \, resolver-proto instance496 \item 497 \Indexc{rsteps}\index{trans lator option!-P@{©-P©}!©rsteps©}\index{translator option!--print@{©-print©}!©rsteps©} \, print resolver steps498 \item 499 \Indexc{ast}\index{trans lator option!-P@{©-P©}!©ast©}\index{translator option!--print@{©-print©}!©ast©} \, print AST after parsing500 \item 501 \Indexc{excpdecl}\index{trans lator option!-P@{©-P©}!©excpdecl©}\index{translator option!--print@{©-print©}!©excpdecl©} \, print AST after translating exception decls502 \item 503 \Indexc{symevt}\index{trans lator option!-P@{©-P©}!©symevt©}\index{translator option!--print@{©-print©}!©symevt©} \, print AST after symbol table events504 \item 505 \Indexc{expralt}\index{trans lator option!-P@{©-P©}!©expralt©}\index{translator option!--print@{©-print©}!©expralt©} \, print AST after expressions alternatives506 \item 507 \Indexc{valdecl}\index{trans lator option!-P@{©-P©}!©valdecl©}\index{translator option!--print@{©-print©}!©valdecl©} \, print AST after declaration validation pass508 \item 509 \Indexc{bresolver}\index{trans lator option!-P@{©-P©}!©bresolver©}\index{translator option!--print@{©-print©}!©bresolver©} \, print AST before resolver step510 \item 511 \Indexc{expranly}\index{trans lator option!-P@{©-P©}!©expranly©}\index{translator option!--print@{©-print©}!©expranly©} \, print AST after expression analysis512 \item 513 \Indexc{ctordtor}\index{trans lator option!-P@{©-P©}!©ctordtor©}\index{translator option!--print@{©-print©}!©ctordtor©} \, print AST after ctor/dtor are replaced514 \item 515 \Indexc{tuple}\index{trans lator option!-P@{©-P©}!©tuple©}\index{translator option!--print@{©-print©}!©tuple©} \, print AST after tuple expansion516 \item 517 \Indexc{instgen}\index{trans lator option!-P@{©-P©}!©instgen©}\index{translator option!--print@{©-print©}!©instgen©} \, print AST after instantiate generics518 \item 519 \Indexc{bbox}\index{trans lator option!-P@{©-P©}!©bbox©}\index{translator option!--print@{©-print©}!©bbox©} \, print AST before box pass520 \item 521 \Indexc{bcodegen}\index{trans lator option!-P@{©-P©}!©bcodegen©}\index{translator option!--print@{©-print©}!©bcodegen©} \, print AST before code generation491 \Indexc{ascodegen}\index{transpiler option!-P@{©-P©}!©ascodegen©}\index{transpiler option!--print@{©-print©}!©ascodegen©} \, print AST as codegen rather than AST 492 \item 493 \Indexc{asterr}\index{transpiler option!-P@{©-P©}!©asterr©}\index{transpiler option!--print@{©-print©}!©asterr©} \, print AST on error 494 \item 495 \Indexc{declstats}\index{transpiler option!-P@{©-P©}!©declstats©}\index{transpiler option!--print@{©-print©}!©declstats©} \, print code property statistics 496 \item 497 \Indexc{parse}\index{transpiler option!-P@{©-P©}!©parse©}\index{transpiler option!--print@{©-print©}!©parse©} \, print yacc (parsing) debug information 498 \item 499 \Indexc{pretty}\index{transpiler option!-P@{©-P©}!©pretty©}\index{transpiler option!--print@{©-print©}!©pretty©} \, prettyprint for ©ascodegen© flag 500 \item 501 \Indexc{rproto}\index{transpiler option!-P@{©-P©}!©rproto©}\index{transpiler option!--print@{©-print©}!©rproto©} \, resolver-proto instance 502 \item 503 \Indexc{rsteps}\index{transpiler option!-P@{©-P©}!©rsteps©}\index{transpiler option!--print@{©-print©}!©rsteps©} \, print resolver steps 504 \item 505 \Indexc{ast}\index{transpiler option!-P@{©-P©}!©ast©}\index{transpiler option!--print@{©-print©}!©ast©} \, print AST after parsing 506 \item 507 \Indexc{excpdecl}\index{transpiler option!-P@{©-P©}!©excpdecl©}\index{transpiler option!--print@{©-print©}!©excpdecl©} \, print AST after translating exception decls 508 \item 509 \Indexc{symevt}\index{transpiler option!-P@{©-P©}!©symevt©}\index{transpiler option!--print@{©-print©}!©symevt©} \, print AST after symbol table events 510 \item 511 \Indexc{expralt}\index{transpiler option!-P@{©-P©}!©expralt©}\index{transpiler option!--print@{©-print©}!©expralt©} \, print AST after expressions alternatives 512 \item 513 \Indexc{valdecl}\index{transpiler option!-P@{©-P©}!©valdecl©}\index{transpiler option!--print@{©-print©}!©valdecl©} \, print AST after declaration validation pass 514 \item 515 \Indexc{bresolver}\index{transpiler option!-P@{©-P©}!©bresolver©}\index{transpiler option!--print@{©-print©}!©bresolver©} \, print AST before resolver step 516 \item 517 \Indexc{expranly}\index{transpiler option!-P@{©-P©}!©expranly©}\index{transpiler option!--print@{©-print©}!©expranly©} \, print AST after expression analysis 518 \item 519 \Indexc{ctordtor}\index{transpiler option!-P@{©-P©}!©ctordtor©}\index{transpiler option!--print@{©-print©}!©ctordtor©} \, print AST after ctor/dtor are replaced 520 \item 521 \Indexc{tuple}\index{transpiler option!-P@{©-P©}!©tuple©}\index{transpiler option!--print@{©-print©}!©tuple©} \, print AST after tuple expansion 522 \item 523 \Indexc{instgen}\index{transpiler option!-P@{©-P©}!©instgen©}\index{transpiler option!--print@{©-print©}!©instgen©} \, print AST after instantiate generics 524 \item 525 \Indexc{bbox}\index{transpiler option!-P@{©-P©}!©bbox©}\index{transpiler option!--print@{©-print©}!©bbox©} \, print AST before box pass 526 \item 527 \Indexc{bcodegen}\index{transpiler option!-P@{©-P©}!©bcodegen©}\index{transpiler option!--print@{©-print©}!©bcodegen©} \, print AST before code generation 522 528 \end{description} 523 529 \item 524 530 \Indexc{--prelude-dir} <directory> \, prelude directory for debug/nodebug 525 531 \item 526 \Indexc{-S}\index{trans lator option!-S@{©-S©}!©counters,heap,time,all,none©}, \Indexc{--statistics}\index{translator option!--statistics@{©--statistics©}!©counters,heap,time,all,none©} <option-list> \, enable profiling information: ©counters©, ©heap©, ©time©, ©all©, ©none©527 \item 528 \Indexc{-t}\index{trans lator option!-t@{©-t©}}, \Indexc{--tree}\index{translator option!--tree@{©--tree©}} build in tree532 \Indexc{-S}\index{transpiler option!-S@{©-S©}!©counters,heap,time,all,none©}, \Indexc{--statistics}\index{transpiler option!--statistics@{©--statistics©}!©counters,heap,time,all,none©} <option-list> \, enable profiling information: ©counters©, ©heap©, ©time©, ©all©, ©none© 533 \item 534 \Indexc{-t}\index{transpiler option!-t@{©-t©}}, \Indexc{--tree}\index{transpiler option!--tree@{©--tree©}} build in tree 529 535 \end{description} 530 536 … … 540 546 \end{cfa} 541 547 Existing C programs with keyword clashes can be converted by prefixing the keyword identifiers with double backquotes, and eventually the identifier name can be changed to a non-keyword name. 542 \VRef[Figure]{f:HeaderFileInterposition} shows how clashes in existing C header-files \see{\VRef{s:StandardHeaders}} can be handled using preprocessor \newterm{interposition}: ©#include_next© and ©-I filename©.543 Several common C header-files with keyword clashes are fixed in the standard \CFA header-library, so there is a seamless programming-experience.548 \VRef[Figure]{f:HeaderFileInterposition} shows how clashes in existing C header-files \see{\VRef{s:StandardHeaders}} can be handled using preprocessor \newterm{interposition}: ©#include_next© and command-line ©-I filename©. 549 Several common C header-files with keyword clashes are fixed in the standard \CFA header-library, so there is largely a seamless programming-experience. 544 550 545 551 \begin{figure} … … 591 597 the type suffixes ©U©, ©L©, \etc may start with an underscore ©1_U©, ©1_ll© or ©1.0E10_f©. 592 598 \end{enumerate} 593 It is significantly easier to read and enter long constants when they are broken up into smaller groupings (m anycultures use comma and/or period among digits for the same purpose).599 It is significantly easier to read and enter long constants when they are broken up into smaller groupings (most cultures use comma and/or period among digits for the same purpose). 594 600 This extension is backwards compatible, matches with the use of underscore in variable names, and appears in \Index*{Ada} and \Index*{Java} 8. 595 601 \CC uses the single quote (©'©) as a separator, restricted within a sequence of digits, \eg ©0xaa©©'©©ff©, ©3.141©©'©©592E1©©'©©1©. 602 However, the drawback of the \CC approach is difficults parsing for IDEs between character and numeric constants, as quotes are no longer balanced (©'x'© and ©3.14©©'©©159©). 596 603 597 604 598 605 \section{Exponentiation Operator} 599 606 600 C, \CC, and Java (and other programming languages) have noexponentiation operator\index{exponentiation!operator}\index{operator!exponentiation}, \ie $x^y$, and instead use a routine, like \Indexc{pow(x,y)}, to perform the exponentiation operation.607 C, \CC, and Java (and other programming languages) have \emph{no} exponentiation operator\index{exponentiation!operator}\index{operator!exponentiation}, \ie $x^y$, and instead use a routine, like \Indexc{pow(x,y)}, to perform the exponentiation operation. 601 608 \CFA extends the basic operators with the exponentiation operator ©?©\R{©\\©}©?©\index{?\\?@©?@\@?©} and ©?©\R{©\\©}©=?©\index{?\\=?@©@\@=?©}, as in, ©x ©\R{©\\©}© y© and ©x ©\R{©\\©}©= y©, which means $x^y$ and $x \leftarrow x^y$. 602 609 The priority of the exponentiation operator is between the cast and multiplicative operators, so that ©w * (int)x \ (int)y * z© is parenthesized as ©(w * (((int)x) \ ((int)y))) * z©. 603 610 604 611 There are exponentiation operators for integral and floating types, including the builtin \Index{complex} types. 605 Integral exponentiation\index{exponentiation!unsigned integral} is performed with repeated multiplication ($O(\log y)$ multipliesor shifting if the exponent is 2).612 Integral exponentiation\index{exponentiation!unsigned integral} is performed with repeated multiplication ($O(\log y)$ or shifting if the exponent is 2). 606 613 Overflow for a large exponent or negative exponent returns zero. 607 614 Floating exponentiation\index{exponentiation!floating} is performed using \Index{logarithm}s\index{exponentiation!logarithm}, so the exponent cannot be negative. … … 620 627 T ?®\®?( T ep, unsigned long int y ); 621 628 \end{cfa} 622 A user type ©T© must define multiplication, one (©1©), and ©*©.629 A user type ©T© must define one (©1©), and multiplication (©*©) \see{\VRef{s:Operator}}. 623 630 624 631 … … 631 638 \subsection{\texorpdfstring{\LstKeywordStyle{if} / \LstKeywordStyle{while} Statement}{if / while Statement}} 632 639 633 The \Indexc{if} /\Indexc{while} expression allows declarations, similar to\Indexc{for} declaration expression.\footnote{640 The \Indexc{if} and \Indexc{while} expressions are extended with declarations, similar to the \Indexc{for} declaration expression.\footnote{ 634 641 Declarations in the \Indexc{do}-©while© condition are not useful because they appear after the loop body.} 635 642 \begin{cfa} … … 653 660 \label{s:caseClause} 654 661 655 C restricts a \Indexc{case} clause in\Indexc{switch} statement to a single value.662 C restricts the \Indexc{case} clause in a \Indexc{switch} statement to a single value. 656 663 For multiple ©case© clauses prefixing a statement within the ©switch© statement, it is necessary to have multiple ©case© clauses rather than multiple values. 657 664 Requiring a ©case© clause for each value is not in the spirit of brevity normally associated with C. … … 688 695 \end{tabular} 689 696 \end{cquote} 690 In addition, subranges are allowed to specify case values.697 In addition, subranges are allowed to specify a contiguous set of case values. 691 698 \begin{cquote} 692 699 \begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{\hspace{2em}}l@{}} … … 801 808 \end{cfa} 802 809 This situation is better handled by a list of case values \see{\VRef{s:caseClause}}. 803 While fall-through itself is not a problem, the problem occurs when fall-through is the default, as this semantics is unintuitive to many programmers and is different from most programming languages with a ©switch© statement. 804 Hence, default fall-through semantics results in a many programming errors as programmers often \emph{forget} the ©break© statement at the end of a ©case© clause, resulting in inadvertent fall-through. 810 811 While fall-through itself is not a problem, the problem occurs when fall-through is the default, as this semantics is unintuitive for many programmers and is different from most programming languages with a ©switch© statement. 812 Hence, default fall-through semantics results in programming errors as programmers often \emph{forget} the ©break© statement at the end of a ©case© clause, resulting in inadvertent fall-through. 805 813 806 814 \item … … 819 827 The technical problem results from the inability to ensure declaration and initialization of variables when blocks are not entered at the beginning. 820 828 There are few arguments for this kind of control flow, and therefore, there is a strong impetus to eliminate it. 821 822 829 This C idiom is known as ``\Index*{Duff's device}''~\cite{Duff83}, from this example: 823 830 \begin{cfa} … … 996 1003 with the current \Indexc{switch}/\Indexc{choose} statement. 997 1004 1005 1006 \subsection{Loop Control} 1007 1008 Looping a fixed number of times, possibly with a loop index, occurs frequently. 1009 \CFA condenses simply looping to facilitate coding speed and safety~\see{examples in \VRef[Figure]{f:LoopControlExamples}}. 1010 1011 The \Indexc{for}, \Indexc{while}, and \Indexc{do} loop-control are extended to allow an empty conditional, which implies a comparison value of ©1© (true). 1012 \begin{cfa} 1013 while ( ®/* empty */® ) §\C{// while ( true )}§ 1014 for ( ®/* empty */® ) §\C{// for ( ; true; )}§ 1015 do ... while ( ®/* empty */® ) §\C{// do ... while ( true )}§ 1016 \end{cfa} 1017 998 1018 \begin{figure} 999 1019 \begin{tabular}{@{}l@{\hspace{50pt}}|l@{}} … … 1003 1023 while () { sout | "empty"; break; } 1004 1024 do { sout | "empty"; break; } while (); 1005 for () { sout | "empty"; break; } §\C [3in]{sout | nl | nlOff;}§1025 for () { sout | "empty"; break; } §\C{sout | nl | nlOff;}§ 1006 1026 1007 1027 for ( 0 ) { sout | "A"; } sout | "zero"; §\C{sout | nl;}§ … … 1045 1065 for ( i; 5 : j; @ -~ -5 ~ 2 : k; 1.5 ~ @ ) { sout | i | j | k; } §\C{sout | nl;}§ 1046 1066 for ( i; 5 : j; @ -~ -5 ~ 2 : k; 1.5 ~ @ ) { sout | i | j | k; } §\C{sout | nl;}§ 1047 for ( i; 5 : k; 1.5 ~ @ : j; @ -~ -5 ~ 2 ) { sout | i | j | k; } §\C{sout | nl;} \CRT§1067 for ( i; 5 : k; 1.5 ~ @ : j; @ -~ -5 ~ 2 ) { sout | i | j | k; } §\C{sout | nl;}§ 1048 1068 \end{cfa} 1049 1069 & … … 1108 1128 % for ( T i ; 3~9 ) => for ( T i = 3 ; i < 9; i += 1 ) // using 1 1109 1129 1110 1111 \subsection{Loop Control} 1112 1113 Looping a fixed number of times, possibly with a loop index, occurs frequently. 1114 \CFA condenses simply looping to facilitate coding speed and safety~\see{examples in \VRef[Figure]{f:LoopControlExamples}}. 1115 1116 The \Indexc{for}, \Indexc{while}, and \Indexc{do} loop-control allow an empty conditional, which implies a comparison value of ©1© (true). 1117 \begin{cfa} 1118 while ( ®/* empty */® ) §\C{// while ( true )}§ 1119 for ( ®/* empty */® ) §\C{// for ( ; true; )}§ 1120 do ... while ( ®/* empty */® ) §\C{// do ... while ( true )}§ 1121 \end{cfa} 1122 1123 The ©for© control has 4 new loop-control operators that are not overloadable: 1130 The ©for© control is extended with 4 new loop-control operators, which are not overloadable: 1124 1131 \begin{description}[itemsep=0pt,parsep=0pt] 1125 1132 \item … … 1195 1202 Similarly, the high value cannot be elided is an anonymous loop index (©1 ~ @©), as there is no index to stop the loop. 1196 1203 \item 1197 ©:© means lowanother index.1204 ©:© means add another index. 1198 1205 \begin{cfa} 1199 1206 for ( i; 5 ®:® j; 2 ~ 12 ~ 3 ) §\C{// for ( typeof(i) i = 1, j = 2; i < 5 \&\& j < 12; i += 1, j += 3 )}§ … … 1212 1219 \subsection{\texorpdfstring{Labelled \LstKeywordStyle{continue} / \LstKeywordStyle{break} Statement}{Labelled continue / break Statement}} 1213 1220 1214 C \Indexc{continue} and \Indexc{break} statements , for altering control flow,are restricted to one level of nesting for a particular control structure.1221 C \Indexc{continue} and \Indexc{break} statements are restricted to one level of nesting for a particular control structure. 1215 1222 This restriction forces programmers to use \Indexc{goto} to achieve the equivalent control-flow for more than one level of nesting. 1216 1223 To prevent having to switch to the ©goto©, \CFA extends the \Indexc{continue}\index{continue@©continue©!labelled}\index{labelled!continue@©continue©} and \Indexc{break}\index{break@©break©!labelled}\index{labelled!break@©break©} with a target label to support static multi-level exit\index{multi-level exit}\index{static multi-level exit}~\cite{Buhr85}, as in Java. … … 1302 1309 Furthermore, the location of the label at the \emph{beginning} of the target control structure informs the reader (\Index{eye candy}) that complex control-flow is occurring in the body of the control structure. 1303 1310 With ©goto©, the label is at the end of the control structure, which fails to convey this important clue early enough to the reader. 1304 Finally, using an explicit target for the transfer instead of an implicit targetallows new constructs to be added or removed without affecting existing constructs.1311 Finally, using an explicit target for the transfer, instead of an implicit target, allows new constructs to be added or removed without affecting existing constructs. 1305 1312 Otherwise, the implicit targets of the current ©continue© and ©break©, \ie the closest enclosing loop or ©switch©, change as certain constructs are added or removed. 1306 1313 … … 1373 1380 \begin{cfa} 1374 1381 Person p 1375 ®p.®name ; ®p.®address; ®p.®sex;§\C{// access containing fields}§1382 ®p.®name ...; ®p.®address ...; ®p.®sex ...; §\C{// access containing fields}§ 1376 1383 \end{cfa} 1377 1384 which extends to multiple levels of qualification for nested aggregates and multiple aggregates. 1378 1385 \begin{cfa} 1379 1386 struct Ticket { ... } t; 1380 ®p.name®.first ; ®p.address®.street;§\C{// access nested fields}§1381 ®t.®departure ; ®t.®cost;§\C{// access multiple aggregate}§1387 ®p.name®.first ...; ®p.address®.street ...; §\C{// access nested fields}§ 1388 ®t.®departure ...; ®t.®cost ...; §\C{// access multiple aggregate}§ 1382 1389 \end{cfa} 1383 1390 Repeated aggregate qualification is tedious and makes code difficult to read. … … 1558 1565 1559 1566 1560 \s ection{Non-local Exception}1567 \subsection{Non-local Exception} 1561 1568 1562 1569 \begin{cfa} … … 2347 2354 \subsection{Implicit String Conversions} 2348 2355 2349 The types ©char©, ©char *©, ©int©, ©double©, ©_Complex©, including signness and different sizes, implicitly convert to type ©string©. 2350 \VRef[Figure]{f:ImplicitStringConversions} shows examples of implicit conversion between C strings, integral, floating-point and complex types to ©string© 2351 The implicit conversions can be specified explicitly, as in: 2352 \begin{cfa} 2353 s = string( "abc" ); // converts char * to string 2354 s = string( 5 ); // converts int to string 2355 s = string( 5.5 ); // converts double to string 2356 \end{cfa} 2357 Conversions from ©string© to ©char *© are supported but with restrictions. 2358 Explicit As well, when a string is converted to a ©char *©, the storage for the ©char *© is created by the conversion operation, which must be subsequently deleted: 2359 \begin{cfa} 2360 string x = "abc"; 2361 char *p = x; // convert from string to char * 2362 ... 2363 delete p; // free storage created for p 2364 \end{cfa} 2356 The types ©char©, ©char *©, ©int©, ©double©, ©_Complex©, including different signness and sizes, implicitly convert to type ©string©. 2357 \VRef[Figure]{f:ImplicitConversionsString} shows examples of implicit conversions between C strings, integral, floating-point and complex types to ©string©. 2358 A conversions can be explicitly specified: 2359 \begin{cfa} 2360 s = string( "abc" ); §\C{// converts char * to string}§ 2361 s = string( 5 ); §\C{// converts int to string}§ 2362 s = string( 5.5 ); §\C{// converts double to string}§ 2363 \end{cfa} 2364 All conversions from ©string© to ©char *©, attempt to be safe: 2365 either by requiring the maximum length of the ©char *© storage (©strncpy©) or allocating the ©char *© storage for the string characters (ownership), meaning the programmer must free the storage. 2366 As well, a string is always null terminates, implying a minimum size of 1 character. 2367 \begin{cquote} 2368 \begin{tabular}{@{}l@{\hspace{1.75in}}|@{\hspace{15pt}}l@{}} 2369 \begin{cfa} 2370 string s = "abcde"; 2371 char cs[3]; 2372 strncpy( cs, s, sizeof(cs) ); §\C{sout | cs;}§ 2373 char * cp = s; §\C{sout | cp;}§ 2374 delete( cp ); 2375 cp = s + ' ' + s; §\C{sout | cp;}§ 2376 delete( cp ); 2377 \end{cfa} 2378 & 2379 \begin{cfa} 2380 2381 2382 ab 2383 abcde 2384 2385 abcde abcde 2386 2387 \end{cfa} 2388 \end{tabular} 2389 \end{cquote} 2365 2390 2366 2391 \begin{figure} … … 2370 2395 string s; 2371 2396 // conversion of char and char * to string 2372 s = 'x'; §\C D{sout | s;}§2373 s = "abc"; §\C D{sout | s;}§2397 s = 'x'; §\C{sout | s;}§ 2398 s = "abc"; §\C{sout | s;}§ 2374 2399 char cs[5] = "abc"; 2375 s = cs; §\C D{sout | s;}§2400 s = cs; §\C{sout | s;}§ 2376 2401 // conversion of integral, floating-point, and complex to string 2377 s = 45hh; §\CD{sout | s;}§ 2378 s = 45h; §\CD{sout | s;}§ 2379 s = -(ssize_t)MAX - 1; §\CD{sout | s;}§ 2380 s = (size_t)MAX; §\CD{sout | s;}§ 2381 s = 5.5; §\CD{sout | s;}§ 2382 s = 5.5L; §\CD{sout | s;}§ 2383 s = 5.5+3.4i; §\CD{sout | s;}§ 2384 s = 5.5L+3.4Li; §\CD{sout | s;}§ 2385 // safe conversion from string to char * 2386 strncpy( cs, s, sizeof(cs) ); §\CD{sout | cs;}§ 2387 char * cp = s; §\CD{sout | cp; // ownership}§ 2388 delete( cp ); 2389 cp = s + ' ' + s; §\CD{sout | cp; // ownership}§ 2390 delete( cp ); 2402 s = 45hh; §\C{sout | s;}§ 2403 s = 45h; §\C{sout | s;}§ 2404 s = -(ssize_t)MAX - 1; §\C{sout | s;}§ 2405 s = (size_t)MAX; §\C{sout | s;}§ 2406 s = 5.5; §\C{sout | s;}§ 2407 s = 5.5L; §\C{sout | s;}§ 2408 s = 5.5+3.4i; §\C{sout | s;}§ 2409 s = 5.5L+3.4Li; §\C{sout | s;}§ 2391 2410 \end{cfa} 2392 2411 & … … 2408 2427 5.5+3.4i 2409 2428 5.5+3.4i 2410 2411 5.5+2412 5.5+3.4i2413 2414 5.5+3.4i 5.5+3.4i2415 2416 2429 \end{cfa} 2417 2430 \end{tabular} 2418 \caption{Implicit String Conversions}2419 \label{f:Implicit StringConversions}2431 \caption{Implicit Conversions to String} 2432 \label{f:ImplicitConversionsString} 2420 2433 \end{figure} 2421 2434 2422 2435 2436 \subsection{Size (length)} 2437 2438 The ©size© operation returns the length of a string. 2439 \begin{cfa} 2440 i = size( "" ); §\C{// i is assigned 0}§ 2441 i = size( "abc" ); §\C{// i is assigned 3}§ 2442 i = size( peter ); §\C{// i is assigned 5}§ 2443 \end{cfa} 2444 2445 2423 2446 \subsection{Comparison Operators} 2424 2447 2425 The binary relational and equality operators ©<©, ©<=©, ©>©, ©>=©, ©==©, ©!=© compare ©string©using lexicographical ordering, where longer strings are greater than shorter strings.2448 The binary \Index{relational operator}s, ©<©, ©<=©, ©>©, ©>=©, and \Index{equality operator}s, ©==©, ©!=©, compare strings using lexicographical ordering, where longer strings are greater than shorter strings. 2426 2449 2427 2450 2428 2451 \subsection{Concatenation} 2429 2452 2430 The binary operator ©+© concatenates two strings. 2431 \begin{cfa} 2432 s = peter + digit; §\C{// s is assigned "PETER0123456789"}§ 2433 s += peter; §\C{// s is assigned "PETER0123456789PETER"}§ 2434 \end{cfa} 2435 There is also an assignment form ©+=©. 2453 The binary operators \Indexc{+} and \Indexc{+=} concatenate two strings, creating the sum of the strings. 2454 \begin{cfa} 2455 s = peter + ' ' + digit; §\C{// s is assigned "PETER 0123456789"}§ 2456 s += peter; §\C{// s is assigned "PETER 0123456789PETER"}§ 2457 \end{cfa} 2436 2458 2437 2459 2438 2460 \subsection{Repetition} 2439 2461 2440 The binary operator \Indexc{*} returns a string that is the string repeated ©n© times. 2441 If ©n = 0©, a zero length string, ©""© is returned. 2442 \begin{cfa} 2443 s = (peter + ' ') * 3; §\C{// s is assigned "PETER PETER PETER"}§ 2444 \end{cfa} 2445 There is also an assignment form ©*=©. 2446 2447 2448 \subsection{Length} 2449 2450 The ©length© operation 2451 \begin{cfa} 2452 int length() 2453 \end{cfa} 2454 returns the length of a string variable. 2455 \begin{cfa} 2456 i = peter.length(); §\C{// i is assigned the value 5}§ 2462 The binary operators \Indexc{*} and \Indexc{*=} repeat a string $N$ times. 2463 If $N = 0$, a zero length string, ©""© is returned. 2464 \begin{cfa} 2465 s = 'x' * 3; §\C{// s is assigned "PETER PETER PETER "}§ 2466 s = (peter + ' ') * 3; §\C{// s is assigned "PETER PETER PETER "}§ 2457 2467 \end{cfa} 2458 2468 2459 2469 2460 2470 \subsection{Substring} 2461 The substring operation: 2462 \begin{cfa} 2463 string operator () (int start, int lnth); 2464 \end{cfa} 2465 performs a substring operation that returns the string starting at a specified position (©start©) in the current string, and having the specified length (©lnth©). 2471 The substring operation returns a subset of the string starting at a position in the string and traversing a length. 2472 \begin{cfa} 2473 s = peter( 2, 3 ); §\C{// s is assigned "ETE"}§ 2474 s = peter( 4, -3 ); §\C{// s is assigned "ETE", length is opposite direction}§ 2475 s = peter( 2, 8 ); §\C{// s is assigned "ETER", length is clipped to 4}§ 2476 s = peter( 0, -1 ); §\C{// s is assigned "", beyond string so clipped to null}§ 2477 s = peter(-1, -1 ); §\C{// s is assigned "R", start and length are negative}§ 2478 \end{cfa} 2466 2479 A negative starting position is a specification from the right end of the string. 2467 2480 A negative length means that characters are selected in the opposite (right to left) direction from the starting position. 2468 2481 If the substring request extends beyond the beginning or end of the string, it is clipped (shortened) to the bounds of the string. 2469 2482 If the substring request is completely outside of the original string, a null string located at the end of the original string is returned. 2470 \begin{cfa}2471 s = peter( 2, 3 ); §\C{// s is assigned "ETE"}§2472 s = peter( 4, -3 ); §\C{// s is assigned "ETE", length is opposite direction}§2473 s = peter( 2, 8 ); §\C{// s is assigned "ETER", length is clipped to 4}§2474 s = peter( 0, -1 ); §\C{// s is assigned "", beyond string so clipped to null}§2475 s = peter(-1, -1 ); §\C{// s is assigned "R", start and length are negative}§2476 \end{cfa}2477 2483 The substring operation can also appear on the left hand side of the assignment operator. 2478 2484 The substring is replaced by the value on the right hand side of the assignment. 2479 2485 The length of the right-hand-side value may be shorter, the same length, or longer than the length of the substring that is selected on the left hand side of the assignment. 2480 \begin{cfa} [mathescape=false]2481 digit( 3, 3 ) = ""; §\C{// digit is assigned "0156789"}§2482 digit( 4, 3 ) = "xyz"; §\C{// digit is assigned "015xyz9"}§2483 digit( 7, 0 ) = "***"; §\C{// digit is assigned "015xyz***9"}§2484 digit(-4, 3 ) = "$$$"; §\C{// digit is assigned "015xyz\$\$\$9"}§2486 \begin{cfa} 2487 digit( 3, 3 ) = ""; §\C{// digit is assigned "0156789"}§ 2488 digit( 4, 3 ) = "xyz"; §\C{// digit is assigned "015xyz9"}§ 2489 digit( 7, 0 ) = "***"; §\C{// digit is assigned "015xyz***9"}§ 2490 digit(-4, 3 ) = "$$$"; §\C{// digit is assigned "015xyz\$\$\$9"}§ 2485 2491 \end{cfa} 2486 2492 A substring is treated as a pointer into the base (substringed) string rather than creating a copy of the subtext. … … 2501 2507 // e is a substring result passed by value 2502 2508 void test(string &x, string &a, string &b, string &c, string &d, string e) { 2503 §\C{// x a b c d e}§2504 a( 1, 2 ) = "aaa"; §\C{// aaaxxxxxxxxxxx aaax axx xxxxx xxxxx xxxxx}§2505 b( 2, 12 ) = "bbb"; §\C{// aaabbbxxxxxxxxx aaab abbb bbxxx xxxxx xxxxx}§2506 c( 4, 5 ) = "ccc"; §\C{// aaabbbxcccxxxxxx aaab abbb bbxccc ccxxx xxxxx}§2507 c = "yyy"; §\C{// aaabyyyxxxxxx aaab abyy yyy xxxxx xxxxx}§2508 d( 1, 3 ) = "ddd"; §\C{// aaabyyyxdddxx aaab abyy yyy dddxx xxxxx}§2509 e( 1, 3 ) = "eee"; §\C{// aaabyyyxdddxx aaab abyy yyy dddxx eeexx}§2510 x = e; §\C{// eeexx eeex exx x eeexx}§2509 §\C{// x a b c d e}§ 2510 a( 1, 2 ) = "aaa"; §\C{// aaaxxxxxxxxxxx aaax axx xxxxx xxxxx xxxxx}§ 2511 b( 2, 12 ) = "bbb"; §\C{// aaabbbxxxxxxxxx aaab abbb bbxxx xxxxx xxxxx}§ 2512 c( 4, 5 ) = "ccc"; §\C{// aaabbbxcccxxxxxx aaab abbb bbxccc ccxxx xxxxx}§ 2513 c = "yyy"; §\C{// aaabyyyxxxxxx aaab abyy yyy xxxxx xxxxx}§ 2514 d( 1, 3 ) = "ddd"; §\C{// aaabyyyxdddxx aaab abyy yyy dddxx xxxxx}§ 2515 e( 1, 3 ) = "eee"; §\C{// aaabyyyxdddxx aaab abyy yyy dddxx eeexx}§ 2516 x = e; §\C{// eeexx eeex exx x eeexx}§ 2511 2517 } 2512 2518 \end{cfa} … … 2518 2524 For example: 2519 2525 \begin{cfa} 2520 s = peter( 2 ); §\C{// s is assigned "ETER"}§2521 peter( 2 ) = "IPER"; §\C{// peter is assigned "PIPER"}§2526 s = peter( 2 ); §\C{// s is assigned "ETER"}§ 2527 peter( 2 ) = "IPER"; §\C{// peter is assigned "PIPER"}§ 2522 2528 \end{cfa} 2523 2529 It is also possible to substring using a string as the index for selecting the substring portion of the string. … … 2527 2533 For example: 2528 2534 \begin{cfa}[mathescape=false] 2529 digit( "xyz$$$" ) = "678"; §\C{// digit is assigned "0156789"}§ 2530 digit( "234") = "***"; §\C{// digit is assigned "0156789***"}§ 2531 \end{cfa} 2532 %$ 2535 digit( "xyz$$$" ) = "678"; §\C{// digit is assigned "0156789"}§ 2536 digit( "234") = "***"; §\C{// digit is assigned "0156789***"}§ 2537 \end{cfa} 2533 2538 2534 2539 … … 2570 2575 A negative starting position is a specification from the right end of the string. 2571 2576 \begin{cfa} 2572 i = peter.include( digitmask ); §\C{// i is assigned 1}§2573 i = peter.include( alphamask ); §\C{// i is assigned 6}§2577 i = peter.include( digitmask ); §\C{// i is assigned 1}§ 2578 i = peter.include( alphamask ); §\C{// i is assigned 6}§ 2574 2579 \end{cfa} 2575 2580 … … 2622 2627 \begin{cfa} 2623 2628 // remove leading blanks 2624 s = string( " ABC" ).trim( " " ); 2629 s = string( " ABC" ).trim( " " ); §\C{// s is assigned "ABC",}§ 2625 2630 // remove trailing blanks 2626 s = string( "ABC " ).trim( " ", last ); 2631 s = string( "ABC " ).trim( " ", last ); §\C{// s is assigned "ABC",}§ 2627 2632 \end{cfa} 2628 2633 … … 2651 2656 returns a string in which all occurrences of the ©from© string in the current string have been replaced by the ©to© string. 2652 2657 \begin{cfa} 2653 s = peter.replace( "E", "XX" ); §\C{// s is assigned "PXXTXXR"}§2658 s = peter.replace( "E", "XX" ); §\C{// s is assigned "PXXTXXR"}§ 2654 2659 \end{cfa} 2655 2660 The replacement is done left-to-right. 2656 2661 When an instance of the ©from© string is found and changed to the ©to© string, it is NOT examined again for further replacement. 2657 2662 2658 2659 \section{Returning N+1 on Failure} 2663 \subsection{Returning N+1 on Failure} 2660 2664 2661 2665 Any of the string search routines can fail at some point during the search. … … 2687 2691 2688 2692 2693 \subsection{C Compatibility} 2694 2695 To ease conversion from C to \CFA, there are companion ©string© routines for C strings. 2696 \VRef[Table]{t:CompanionStringRoutines} shows the C routines on the left that also work with ©string© and the rough equivalent ©string© opeation of the right. 2697 Hence, it is possible to directly convert a block of C string operations into @string@ just by changing the 2698 2699 \begin{table} 2700 \begin{cquote} 2701 \begin{tabular}{@{}l|l@{}} 2702 \multicolumn{1}{c|}{©char []©} & \multicolumn{1}{c}{©string©} \\ 2703 \hline 2704 ©strcpy©, ©strncpy© & ©=© \\ 2705 ©strcat©, ©strncat© & ©+© \\ 2706 ©strcmp©, ©strncmp© & ©==©, ©!=©, ©<©, ©<=©, ©>©, ©>=© \\ 2707 ©strlen© & ©size© \\ 2708 ©[]© & ©[]© \\ 2709 ©strstr© & ©find© \\ 2710 ©strcspn© & ©find_first_of©, ©find_last_of© \\ 2711 ©strspc© & ©find_fist_not_of©, ©find_last_not_of© 2712 \end{tabular} 2713 \end{cquote} 2714 \caption{Companion Routines for \CFA \lstinline{string} to C Strings} 2715 \label{t:CompanionStringRoutines} 2716 \end{table} 2717 2718 For example, this block of C code can be converted to \CFA by simply changing the type of variable ©s© from ©char []© to ©string©. 2719 \begin{cfa} 2720 char s[32]; 2721 //string s; 2722 strcpy( s, "abc" ); PRINT( %s, s ); 2723 strncpy( s, "abcdef", 3 ); PRINT( %s, s ); 2724 strcat( s, "xyz" ); PRINT( %s, s ); 2725 strncat( s, "uvwxyz", 3 ); PRINT( %s, s ); 2726 PRINT( %zd, strlen( s ) ); 2727 PRINT( %c, s[3] ); 2728 PRINT( %s, strstr( s, "yzu" ) ) ; 2729 PRINT( %s, strstr( s, 'y' ) ) ; 2730 \end{cfa} 2731 However, the conversion fails with I/O because ©printf© cannot print a ©string© using format code ©%s© because \CFA strings are not null terminated. 2732 2733 2689 2734 \subsection{Input/Output Operators} 2690 2735 … … 2707 2752 Hence, enums may be overloaded with variable, enum, and function names. 2708 2753 \begin{cfa} 2709 int Foo; §\C{// type/variable separate namespaces}§2754 int Foo; §\C{// type/variable separate namespaces}§ 2710 2755 enum Foo { Bar }; 2711 enum Goo { Bar }; §\C[1.75in]{// overload Foo.Bar}§2712 double Bar; §\C{// overload Foo.Bar, Goo.Bar}\CRT§2756 enum Goo { Bar }; §\C{// overload Foo.Bar}§ 2757 double Bar; §\C{// overload Foo.Bar, Goo.Bar}§ 2713 2758 \end{cfa} 2714 2759 An anonymous enumeration injects enums with specific values into a scope. … … 2783 2828 The following examples illustrate the difference between the enumeration type and the type of its enums. 2784 2829 \begin{cfa} 2785 Math m = PI; §\C[1.5in]{// allowed}§2786 double d = PI; §\C{// allowed, conversion to base type}§2787 m = E; §\C{// allowed}§2788 m = Alph; §\C{// {\color{red}disallowed}}§2789 m = 3.141597; §\C{// {\color{red}disallowed}}§2790 d = m; §\C{// allowed}§2791 d = Alph; §\C{// {\color{red}disallowed}}§2792 Letter l = A; §\C{// allowed}§2793 Greek g = Alph; §\C{// allowed}§2794 l = Alph; §\C{// allowed, conversion to base type}§2795 g = A; §\C{// {\color{red}disallowed}}\CRT§2830 Math m = PI; §\C{// allowed}§ 2831 double d = PI; §\C{// allowed, conversion to base type}§ 2832 m = E; §\C{// allowed}§ 2833 m = Alph; §\C{// {\color{red}disallowed}}§ 2834 m = 3.141597; §\C{// {\color{red}disallowed}}§ 2835 d = m; §\C{// allowed}§ 2836 d = Alph; §\C{// {\color{red}disallowed}}§ 2837 Letter l = A; §\C{// allowed}§ 2838 Greek g = Alph; §\C{// allowed}§ 2839 l = Alph; §\C{// allowed, conversion to base type}§ 2840 g = A; §\C{// {\color{red}disallowed}}§ 2796 2841 \end{cfa} 2797 2842 … … 3362 3407 For example, consider C's \Indexc{div} function, which returns the quotient and remainder for a division of an integer value. 3363 3408 \begin{cfa} 3364 typedef struct { int quot, rem; } div_t; §\C[7cm]{// from include stdlib.h}§3409 typedef struct { int quot, rem; } div_t; §\C{// from include stdlib.h}§ 3365 3410 div_t div( int num, int den ); 3366 div_t qr = div( 13, 5 ); 3411 div_t qr = div( 13, 5 ); §\C{// return quotient/remainder aggregate}§ 3367 3412 printf( "%d %d\n", qr.quot, qr.rem ); §\C{// print quotient/remainder}§ 3368 3413 \end{cfa} … … 3375 3420 For example, consider C's \Indexc{modf} function, which returns the integral and fractional part of a floating value. 3376 3421 \begin{cfa} 3377 double modf( double x, double * i ); 3422 double modf( double x, double * i ); §\C{// from include math.h}§ 3378 3423 double intp, frac = modf( 13.5, &intp ); §\C{// return integral and fractional components}§ 3379 3424 printf( "%g %g\n", intp, frac ); §\C{// print integral/fractional components}§ … … 3404 3449 When a function call is passed as an argument to another call, the best match of actual arguments to formal parameters is evaluated given all possible expression interpretations in the current scope. 3405 3450 \begin{cfa} 3406 void g( int, int ); 3407 void g( double, double ); 3408 g( div( 13, 5 ) ); 3409 g( modf( 13.5 ) ); 3451 void g( int, int ); §\C{// 1}§ 3452 void g( double, double ); §\C{// 2}§ 3453 g( div( 13, 5 ) ); §\C{// select 1}§ 3454 g( modf( 13.5 ) ); §\C{// select 2}§ 3410 3455 \end{cfa} 3411 3456 In this case, there are two overloaded ©g© routines. … … 3416 3461 The previous examples can be rewritten passing the multiple returned-values directly to the ©printf© function call. 3417 3462 \begin{cfa} 3418 [ int, int ] div( int x, int y ); 3463 [ int, int ] div( int x, int y ); §\C{// from include stdlib}§ 3419 3464 printf( "%d %d\n", div( 13, 5 ) ); §\C{// print quotient/remainder}§ 3420 3465 3421 [ double, double ] modf( double x ); 3466 [ double, double ] modf( double x ); §\C{// from include math}§ 3422 3467 printf( "%g %g\n", modf( 13.5 ) ); §\C{// print integral/fractional components}§ 3423 3468 \end{cfa} … … 3430 3475 \begin{cfa} 3431 3476 int quot, rem; 3432 [ quot, rem ] = div( 13, 5 ); 3433 printf( "%d %d\n", quot, rem ); §\C{// print quotient/remainder}\CRT§3477 [ quot, rem ] = div( 13, 5 ); §\C{// assign multiple variables}§ 3478 printf( "%d %d\n", quot, rem ); §\C{// print quotient/remainder}§ 3434 3479 \end{cfa} 3435 3480 Here, the multiple return-values are matched in much the same way as passing multiple return-values to multiple parameters in a call. … … 3848 3893 The general syntax of a lexical list is: 3849 3894 \begin{cfa} 3850 [ $\emph{exprlist}$]3851 \end{cfa} 3852 where ©$\emph{exprlist}$©is a list of one or more expressions separated by commas.3895 [ §\emph{exprlist}§ ] 3896 \end{cfa} 3897 where \LstBasicStyle{\emph{exprlist}} is a list of one or more expressions separated by commas. 3853 3898 The brackets, ©[]©, allow differentiating between lexical lists and expressions containing the C comma operator. 3854 3899 The following are examples of lexical lists: … … 3856 3901 [ x, y, z ] 3857 3902 [ 2 ] 3858 [ v +w, x*y, 3.14159, f() ]3903 [ v + w, x * y, 3.14159, f() ] 3859 3904 \end{cfa} 3860 3905 Tuples are permitted to contain sub-tuples (\ie nesting), such as ©[ [ 14, 21 ], 9 ]©, which is a 2-element tuple whose first element is itself a tuple. … … 3868 3913 The general syntax of a tuple type is: 3869 3914 \begin{cfa} 3870 [ $\emph{typelist}$]3871 \end{cfa} 3872 where ©$\emph{typelist}$©is a list of one or more legal \CFA or C type specifications separated by commas, which may include other tuple type specifications.3915 [ §\emph{typelist}§ ] 3916 \end{cfa} 3917 where \LstBasicStyle{\emph{typelist}} is a list of one or more legal \CFA or C type specifications separated by commas, which may include other tuple type specifications. 3873 3918 Examples of tuple types include: 3874 3919 \begin{cfa} 3875 3920 [ unsigned int, char ] 3876 3921 [ double, double, double ] 3877 [ * int, int * ] 3922 [ * int, int * ] §\C{// mix of CFA and ANSI}§ 3878 3923 [ * [ 5 ] int, * * char, * [ [ int, int ] ] (int, int) ] 3879 3924 \end{cfa} … … 3882 3927 Examples of declarations using tuple types are: 3883 3928 \begin{cfa} 3884 [ int, int ] x; 3885 * [ char, char ] y; 3929 [ int, int ] x; §\C{// 2 element tuple, each element of type int}§ 3930 * [ char, char ] y; §\C{// pointer to a 2 element tuple}§ 3886 3931 [ [ int, int ] ] z ([ int, int ]); 3887 3932 \end{cfa} … … 3900 3945 [ int, int ] w1; 3901 3946 [ int, int, int ] w2; 3902 [ void ] f (int, int, int); 3903 [ void ] g ([ int, int, int ]); §\C{3 element tuple as input}§3947 [ void ] f (int, int, int); §\C{// three input parameters of type int}§ 3948 [ void ] g ([ int, int, int ]); §\C{// 3 element tuple as input}§ 3904 3949 f( [ 1, 2, 3 ] ); 3905 3950 f( w1, 3 ); … … 3982 4027 [ int, int, int, int ] w = [ 1, 2, 3, 4 ]; 3983 4028 int x = 5; 3984 [ x, w ] = [ w, x ]; 4029 [ x, w ] = [ w, x ]; §\C{// all four tuple coercions}§ 3985 4030 \end{cfa} 3986 4031 Starting on the right-hand tuple in the last assignment statement, w is opened, producing a tuple of four values; … … 4000 4045 Mass assignment has the following form: 4001 4046 \begin{cfa} 4002 [ $\emph{lvalue}$, ... , $\emph{lvalue}$ ] = $\emph{expr}$;4047 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = §\emph{expr}§; 4003 4048 \end{cfa} 4004 4049 \index{lvalue} 4005 The left-hand side is a tuple of \ emph{lvalues}, which is a list of expressions each yielding an address, \ie any data object that can appear on the left-hand side of a conventional assignment statement.4006 ©$\emph{expr}$©is any standard arithmetic expression.4050 The left-hand side is a tuple of \LstBasicStyle{\emph{lvalues}}, which is a list of expressions each yielding an address, \ie any data object that can appear on the left-hand side of a conventional assignment statement. 4051 \LstBasicStyle{\emph{expr}} is any standard arithmetic expression. 4007 4052 Clearly, the types of the entities being assigned must be type compatible with the value of the expression. 4008 4053 … … 4041 4086 Multiple assignment has the following form: 4042 4087 \begin{cfa} 4043 [ $\emph{lvalue}$, ... , $\emph{lvalue}$ ] = [ $\emph{expr}$, ... , $\emph{expr}$];4088 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = [ §\emph{expr}§, ... , §\emph{expr}§ ]; 4044 4089 \end{cfa} 4045 4090 \index{lvalue} … … 4072 4117 both these examples produce indeterminate results: 4073 4118 \begin{cfa} 4074 f( x++, x++ ); 4075 [ v1, v2 ] = [ x++, x++ ]; 4119 f( x++, x++ ); §\C{// C routine call with side effects in arguments}§ 4120 [ v1, v2 ] = [ x++, x++ ]; §\C{// side effects in right-hand side of multiple assignment}§ 4076 4121 \end{cfa} 4077 4122 … … 4083 4128 Cascade assignment has the following form: 4084 4129 \begin{cfa} 4085 $\emph{tuple}$ = $\emph{tuple}$ = ... = $\emph{tuple}$;4130 §\emph{tuple}§ = §\emph{tuple}§ = ... = §\emph{tuple}§; 4086 4131 \end{cfa} 4087 4132 and it has the same parallel semantics as for mass and multiple assignment. … … 4261 4306 try { 4262 4307 choose ( argc ) { 4263 case 2, 3:4308 case 3, 2: 4264 4309 ®open®( in, argv[1] ); §\C{// open input file first as output creates file}§ 4265 4310 if ( argc == 3 ) ®open®( out, argv[2] ); §\C{// do not create output unless input opens}§ … … 4390 4435 \begin{cfa} 4391 4436 sout | 1 | ", x" | 2 | ". x" | 3 | "; x" | 4 | "! x" | 5 | "? x" | 6 | "% x" 4392 | 7 | " $\LstStringStyle{\textcent}$ x" | 8 | "$\LstStringStyle{\guillemotright}$x" | 9 | ") x" | 10 | "] x" | 11 | "} x";4437 | 7 | "§\LstStringStyle{\textcent}§ x" | 8 | "§\LstStringStyle{\guillemotright}§ x" | 9 | ") x" | 10 | "] x" | 11 | "} x"; 4393 4438 \end{cfa} 4394 4439 \begin{cfa}[showspaces=true] 4395 1®,® x 2®.® x 3®;® x 4®!® x 5®?® x 6®%® x 7 $\R{\LstStringStyle{\textcent}}$ x 8$\R{\LstStringStyle{\guillemotright}}$x 9®)® x 10®]® x 11®}® x4440 1®,® x 2®.® x 3®;® x 4®!® x 5®?® x 6®%® x 7§\R{\LstStringStyle{\textcent}}§ x 8§\R{\LstStringStyle{\guillemotright}}§ x 9®)® x 10®]® x 11®}® x 4396 4441 \end{cfa} 4397 4442 4398 4443 \item 4399 4444 A separator does not appear after a C string ending with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \LstStringStyle{([\{=\$\textsterling\textyen\textexclamdown\textquestiondown\guillemotleft}, where \LstStringStyle{\textexclamdown\textquestiondown} are inverted opening exclamation and question marks, and \LstStringStyle{\guillemotleft} is an opening citation mark. 4400 %$ 4401 \begin{cfa} 4402 sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $\LstStringStyle{\textdollar}$" | 5 | "x $\LstStringStyle{\textsterling}$" | 6 | "x $\LstStringStyle{\textyen}$" 4403 | 7 | "x $\LstStringStyle{\textexclamdown}$" | 8 | "x $\LstStringStyle{\textquestiondown}$" | 9 | "x $\LstStringStyle{\guillemotleft}$" | 10; 4404 \end{cfa} 4405 %$ 4445 \begin{cfa} 4446 sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x §\LstStringStyle{\textdollar}§" | 5 | "x §\LstStringStyle{\textsterling}§" | 6 | "x §\LstStringStyle{\textyen}§" 4447 | 7 | "x §\LstStringStyle{\textexclamdown}§" | 8 | "x §\LstStringStyle{\textquestiondown}§" | 9 | "x §\LstStringStyle{\guillemotleft}§" | 10; 4448 \end{cfa} 4406 4449 \begin{cfa}[showspaces=true] 4407 x ®(®1 x ®[®2 x ®{®3 x ®=®4 x $\LstStringStyle{\textdollar}$5 x $\R{\LstStringStyle{\textsterling}}$6 x $\R{\LstStringStyle{\textyen}}$7 x $\R{\LstStringStyle{\textexclamdown}}$8 x $\R{\LstStringStyle{\textquestiondown}}$9 x $\R{\LstStringStyle{\guillemotleft}}$10 4408 \end{cfa} 4409 %$ 4450 x ®(®1 x ®[®2 x ®{®3 x ®=®4 x §\LstStringStyle{\textdollar}§5 x §\R{\LstStringStyle{\textsterling}}§6 x §\R{\LstStringStyle{\textyen}}§7 x §\R{\LstStringStyle{\textexclamdown}}§8 x §\R{\LstStringStyle{\textquestiondown}}§9 x §\R{\LstStringStyle{\guillemotleft}}§10 4451 \end{cfa} 4410 4452 4411 4453 \item … … 4415 4457 \end{cfa} 4416 4458 \begin{cfa}[showspaces=true,showtabs=true] 4417 x®`®1®`®x $\R{\texttt{'}}$2$\R{\texttt{'}}$x$\R{\texttt{"}}$3$\R{\texttt{"}}$x®:®4®:®x® ®5® ®x® ®6® ®x4459 x®`®1®`®x§\R{\texttt{'}}§2§\R{\texttt{'}}§x§\R{\texttt{"}}§3§\R{\texttt{"}}§x®:®4®:®x® ®5® ®x® ®6® ®x 4418 4460 \end{cfa} 4419 4461 … … 4421 4463 If a space is desired before or after one of the special string start/end characters, explicitly insert a space. 4422 4464 \begin{cfa} 4423 sout | "x ( $\R{\texttt{\textvisiblespace}}$" | 1 | "$\R{\texttt{\textvisiblespace}}$) x" | 2 | "$\R{\texttt{\textvisiblespace}}$, x" | 3 | "$\R{\texttt{\textvisiblespace}}$:x:$\R{\texttt{\textvisiblespace}}$" | 4;4465 sout | "x (§\R{\texttt{\textvisiblespace}}§" | 1 | "§\R{\texttt{\textvisiblespace}}§) x" | 2 | "§\R{\texttt{\textvisiblespace}}§, x" | 3 | "§\R{\texttt{\textvisiblespace}}§:x:§\R{\texttt{\textvisiblespace}}§" | 4; 4424 4466 \end{cfa} 4425 4467 \begin{cfa}[showspaces=true,showtabs=true] … … 4438 4480 The separator string can be at most 16 characters including the ©'\0'© string terminator (15 printable characters). 4439 4481 \begin{cfa}[belowskip=0pt] 4440 sepSet( sout, ", $\LstStringStyle{\textdollar}$" ); §\C{// set separator from " " to ", \$"}§4482 sepSet( sout, ", §\LstStringStyle{\textdollar}§" ); §\C{// set separator from " " to ", \$"}§ 4441 4483 sout | 1 | 2 | 3 | " \"" | ®sepVal® | "\""; 4442 4484 \end{cfa} 4443 4485 \begin{cfa}[showspaces=true,aboveskip=0pt] 4444 1®, $\color{red}\LstStringStyle{\textdollar}$®2®, $\color{red}\LstStringStyle{\textdollar}$®3 ®", $\color{red}\LstStringStyle{\textdollar}$"®4486 1®, §\color{red}\LstStringStyle{\textdollar}§®2®, §\color{red}\LstStringStyle{\textdollar}§®3 ®", §\color{red}\LstStringStyle{\textdollar}§"® 4445 4487 \end{cfa} 4446 4488 \begin{cfa}[belowskip=0pt] … … 4643 4685 \item 4644 4686 \Indexc{unit}( engineering-notation )\index{manipulator!unit@©unit©} print engineering exponent as a letter between the range $10^{-24}$ and $10^{24}$: 4687 \begin{cquote} 4645 4688 ©y© $\Rightarrow 10^{-24}$, ©z© $\Rightarrow 10^{-21}$, ©a© $\Rightarrow 10^{-18}$, ©f© $\Rightarrow 10^{-15}$, ©p© $\Rightarrow 10^{-12}$, ©n© $\Rightarrow 10^{-9}$, ©u© $\Rightarrow 10^{-6}$, ©m© $\Rightarrow 10^{-3}$, ©K© $\Rightarrow 10^{3}$, ©M© $\Rightarrow 10^{6}$, ©G© $\Rightarrow 10^{9}$, ©T© $\Rightarrow 10^{12}$, ©P© $\Rightarrow 10^{15}$, ©E© $\Rightarrow 10^{18}$, ©Z© $\Rightarrow 10^{21}$, ©Y© $\Rightarrow 10^{24}$. 4689 \end{cquote} 4646 4690 For exponent $10^{0}$, no decimal point or letter is printed. 4647 4691 \begin{cfa}[belowskip=0pt] … … 4843 4887 // End: // 4844 4888 \end{comment} 4845 %$4846 4889 4847 4890 … … 4875 4918 The following string is matched by this scanset: 4876 4919 \begin{cfa} 4877 !&%$ abAA () ZZZ ?? xx§ §§§4920 !&%$ abAA () ZZZ ?? xx§\S\S\S§ 4878 4921 \end{cfa} 4879 4922 To match a minus, put it as the first character, ©"-0-9"©. … … 4889 4932 If the match fails, the input characters are left unread. 4890 4933 \begin{cfa}[belowskip=0pt] 4891 char sk[ $\,$] = "abc";4934 char sk[§\,§] = "abc"; 4892 4935 sin | "abc " | skip( sk ) | skip( 5 ); // match input sequence 4893 4936 \end{cfa} … … 4958 5001 For example, if two threads execute the following: 4959 5002 \begin{cfa} 4960 $\emph{thread\(_1\)}$: sout | "abc " | "def ";4961 $\emph{thread\(_2\)}$: sout | "uvw " | "xyz ";5003 §\emph{thread\(_1\)}§ : sout | "abc " | "def "; 5004 §\emph{thread\(_2\)}§ : sout | "uvw " | "xyz "; 4962 5005 \end{cfa} 4963 5006 possible outputs are: … … 4999 5042 The common usage is the short form of the mutex statement\index{ostream@©ostream©!mutex@©mutex©} to lock a stream during a single cascaded I/O expression, \eg: 5000 5043 \begin{cfa} 5001 $\emph{thread\(_1\)}$: ®mutex( sout )® sout | "abc " | "def ";5002 $\emph{thread\(_2\)}$: ®mutex( sout )® sout | "uvw " | "xyz ";5044 §\emph{thread\(_1\)}§ : ®mutex( sout )® sout | "abc " | "def "; 5045 §\emph{thread\(_2\)}§ : ®mutex( sout )® sout | "uvw " | "xyz "; 5003 5046 \end{cfa} 5004 5047 Now, the order of the thread execution is still non-deterministic, but the output is constrained to two possible lines in either order. … … 5061 5104 Cultures use different syntax, called a \newterm{locale}, for printing numbers so they are easier to read, \eg: 5062 5105 \begin{cfa} 5063 12®,®345®.®123 $\C[1.25in]{// comma separator, period decimal-point}$5106 12®,®345®.®123 §\C[1.25in]{// comma separator, period decimal-point}§ 5064 5107 12®.®345®,®123 §\C{// period separator, comma decimal-point}§ 5065 12 $\Sp$345®,®123®.® §\C{// space separator, comma decimal-point, period terminator}\CRT§5108 12§\Sp§345®,®123®.® §\C{// space separator, comma decimal-point, period terminator}\CRT§ 5066 5109 \end{cfa} 5067 5110 A locale is selected with function ©setlocale©, and the corresponding locale package \emph{must} be installed on the underlying system; … … 5109 5152 Ukraine uk_UA.utf8 5110 5153 12 123 1 234 12 345 123 456 1 234 567 5111 12®.® 123®,®1®.® 1 $\Sp$234®,®12®.® 12$\Sp$ 345®,®123®.® 123$\Sp$ 456®,®1234®.® 1$\Sp$ 234$\Sp$567®,®12345®.®5154 12®.® 123®,®1®.® 1§\Sp§234®,®12®.® 12§\Sp§ 345®,®123®.® 123§\Sp§ 456®,®1234®.® 1§\Sp§ 234§\Sp§567®,®12345®.® 5112 5155 5113 5156 Default locale off C … … 5461 5504 5462 5505 \subsection{Operator} 5506 \label{s:Operator} 5463 5507 5464 5508 \CFA also allows operators to be overloaded, to simplify the use of user-defined types. … … 7332 7376 7333 7377 \Index*[C++]{\CC{}} is a general-purpose programming language. 7334 It has imperative, object-oriented and generic programming features, while also providing facilities for low-level memory manipulation. (Wikipedia) 7335 7336 The primary focus of \CC seems to be adding object-oriented programming to C, and this is the primary difference between \CC and Do. 7378 It is an imperative, object-oriented and generic programming language, while also providing facilities for low-level memory manipulation. 7379 The primary focus of \CC was adding object-oriented programming to C, and this is the primary difference between \CC and \CFA. 7337 7380 \CC uses classes to encapsulate data and the functions that operate on that data, and to hide the internal representation of the data. 7338 7381 \CFA uses modules instead to perform these same tasks. … … 7352 7395 \subsection{Go} 7353 7396 7354 \Index*{Go}, also commonly referred to as golang, is a programming language developed at Google in 2007 [.].7397 \Index*{Go}, also commonly referred to as golang, is a programming language developed at Google in 2007~\cite{Go}. 7355 7398 It is a statically typed language with syntax loosely derived from that of C, adding garbage collection, type 7356 7399 safety, some structural typing capabilities, additional built-in types such as variable-length arrays and key-value maps, and a large standard library. (Wikipedia) … … 7406 7449 In \CFA, there are ambiguous cases with dereference and operator identifiers, \eg ©int *?*?()©, where the string ©*?*?© can be interpreted as: 7407 7450 \begin{cfa} 7408 *? $\Sp$*?§\C{// dereference operator, dereference operator}§7409 * $\Sp$?*?§\C{// dereference, multiplication operator}§7451 *?§\Sp§*? §\C{// dereference operator, dereference operator}§ 7452 *§\Sp§?*? §\C{// dereference, multiplication operator}§ 7410 7453 \end{cfa} 7411 7454 By default, the first interpretation is selected, which does not yield a meaningful parse. … … 7416 7459 The ambiguity occurs when the deference operator has no parameters: 7417 7460 \begin{cfa} 7418 *?() $\R{\textvisiblespace...}$;7419 *?() $\R{\textvisiblespace...}$(...) ;7461 *?()§\R{\textvisiblespace...}§ ; 7462 *?()§\R{\textvisiblespace...}§(...) ; 7420 7463 \end{cfa} 7421 7464 requiring arbitrary whitespace look-ahead for the routine-call parameter-list to disambiguate. … … 7425 7468 The remaining cases are with the increment/decrement operators and conditional expression, \eg: 7426 7469 \begin{cfa} 7427 i++? $\R{\textvisiblespace...}$(...);7428 i?++ $\R{\textvisiblespace...}$(...);7470 i++?§\R{\textvisiblespace...}§(...); 7471 i?++§\R{\textvisiblespace...}§(...); 7429 7472 \end{cfa} 7430 7473 requiring arbitrary whitespace look-ahead for the operator parameter-list, even though that interpretation is an incorrect expression (juxtaposed identifiers). 7431 7474 Therefore, it is necessary to disambiguate these cases with a space: 7432 7475 \begin{cfa} 7433 i++ $\Sp$? i : 0;7434 i? $\Sp$++i : 0;7476 i++§\Sp§? i : 0; 7477 i?§\Sp§++i : 0; 7435 7478 \end{cfa} 7436 7479 … … 7459 7502 \eg: 7460 7503 \begin{cfa} 7461 x; 7462 *y; §\C{// int *y}§7463 f( p1, p2 ); 7464 g( p1, p2 ) int p1, p2; 7504 x; §\C{// int x}§ 7505 *y; §\C{// int * y}§ 7506 f( p1, p2 ); §\C{// int f( int p1, int p2 );}§ 7507 g( p1, p2 ) int p1, p2; §\C{// int g( int p1, int p2 );}§ 7465 7508 \end{cfa} 7466 7509 \CFA continues to support K\&R routine definitions: 7467 7510 \begin{cfa} 7468 f( a, b, c ) 7469 int a, b; char c 7511 f( a, b, c ) §\C{// default int return}§ 7512 int a, b; char c; §\C{// K\&R parameter declarations}§ 7470 7513 { 7471 7514 ... … … 7486 7529 int rtn( int i ); 7487 7530 int rtn( char c ); 7488 rtn( 'x' ); 7531 rtn( 'x' ); §\C{// programmer expects 2nd rtn to be called}§ 7489 7532 \end{cfa} 7490 7533 \item[Rationale:] it is more intuitive for the call to ©rtn© to match the second version of definition of ©rtn© rather than the first. … … 7508 7551 \item[Change:] make string literals ©const©: 7509 7552 \begin{cfa} 7510 char * p = "abc"; 7511 char * q = expr ? "abc" : "de"; 7553 char * p = "abc"; §\C{// valid in C, deprecated in \CFA}§ 7554 char * q = expr ? "abc" : "de"; §\C{// valid in C, invalid in \CFA}§ 7512 7555 \end{cfa} 7513 7556 The type of a string literal is changed from ©[] char© to ©const [] char©. … … 7516 7559 \begin{cfa} 7517 7560 char * p = "abc"; 7518 p[0] = 'w'; 7561 p[0] = 'w'; §\C{// segment fault or change constant literal}§ 7519 7562 \end{cfa} 7520 7563 The same problem occurs when passing a string literal to a routine that changes its argument. … … 7528 7571 \item[Change:] remove \newterm{tentative definitions}, which only occurs at file scope: 7529 7572 \begin{cfa} 7530 int i; 7531 int *j = ®&i®; 7532 int i = 0; 7573 int i; §\C{// forward definition}§ 7574 int *j = ®&i®; §\C{// forward reference, valid in C, invalid in \CFA}§ 7575 int i = 0; §\C{// definition}§ 7533 7576 \end{cfa} 7534 7577 is valid in C, and invalid in \CFA because duplicate overloaded object definitions at the same scope level are disallowed. … … 7536 7579 \begin{cfa} 7537 7580 struct X { int i; struct X *next; }; 7538 static struct X a; 7539 static struct X b = { 0, ®&a® }; §\C{// forward reference, valid in C, invalid in \CFA}§7540 static struct X a = { 1, &b }; 7581 static struct X a; §\C{// forward definition}§ 7582 static struct X b = { 0, ®&a® }; §\C{// forward reference, valid in C, invalid in \CFA}§ 7583 static struct X a = { 1, &b }; §\C{// definition}§ 7541 7584 \end{cfa} 7542 7585 \item[Rationale:] avoids having different initialization rules for builtin types and user-defined types. … … 7552 7595 enum ®Colour® { R, G, B, Y, C, M }; 7553 7596 struct Person { 7554 enum ®Colour® { R, G, B }; $\C[7cm]{// nested type}$7555 struct Face { 7556 ®Colour® Eyes, Hair; 7597 enum ®Colour® { R, G, B }; §\C{// nested type}§ 7598 struct Face { §\C{// nested type}§ 7599 ®Colour® Eyes, Hair; §\C{// type defined outside (1 level)}§ 7557 7600 }; 7558 ®.Colour® shirt; 7559 ®Colour® pants; 7560 Face looks[10]; 7601 ®.Colour® shirt; §\C{// type defined outside (top level)}§ 7602 ®Colour® pants; §\C{// type defined same level}§ 7603 Face looks[10]; §\C{// type defined same level}§ 7561 7604 }; 7562 ®Colour® c = R; 7563 Person®.Colour® pc = Person®.®R; §\C{// type/enum defined inside}§7564 Person®.®Face pretty; §\C{// type defined inside}\CRT§7605 ®Colour® c = R; §\C{// type/enum defined same level}§ 7606 Person®.Colour® pc = Person®.®R; §\C{// type/enum defined inside}§ 7607 Person®.®Face pretty; §\C{// type defined inside}§ 7565 7608 \end{cfa} 7566 7609 In C, the name of the nested types belongs to the same scope as the name of the outermost enclosing structure, \ie the nested types are hoisted to the scope of the outer-most type, which is not useful and confusing. … … 7639 7682 \CFA introduces the following new \Index{keyword}s, which cannot be used as identifiers. 7640 7683 7684 \begin{cquote} 7685 \Indexc{basetypeof}, \Indexc{choose}, \Indexc{coroutine}, \Indexc{disable}, 7686 \Indexc{enable}, \Indexc{exception}, \Indexc{fallthrough}, \Indexc{fallthru}, 7687 \Indexc{finally}, \Indexc{fixup}, \Indexc{forall},\Indexc{generator}, 7688 \Indexc{int128}, \Indexc{monitor}, \Indexc{mutex}, \Indexc{one_t}, 7689 \Indexc{report}, \Indexc{suspend}, \Indexc{throw}, \Indexc{throwResume}, 7690 \Indexc{trait}, \Indexc{try}, \Indexc{virtual}, \Indexc{waitfor}, 7691 \Indexc{when}, \Indexc{with}, \Indexc{zero_t} 7692 \end{cquote} 7693 \CFA introduces the following new \Index{quasi-keyword}s, which can be used as identifiers. 7694 \begin{cquote} 7695 \Indexc{catch}, \Indexc{catchResume}, \Indexc{finally}, \Indexc{fixup}, \Indexc{or}, \Indexc{timeout} 7696 \end{cquote} 7697 7698 \begin{comment} 7641 7699 \begin{cquote} 7642 7700 \begin{tabular}{@{}lllllll@{}} … … 7707 7765 \end{tabular} 7708 7766 \end{cquote} 7767 \end{comment} 7768 7709 7769 7710 7770 \section{Standard Headers} 7711 7771 \label{s:StandardHeaders} 7712 7772 7713 \Celeven prescribes the following standard header-files~\cite[\S~7.1.2]{C11} and \CFA adds to this list: 7773 \Celeven prescribes the following standard header-files~\cite[\S~7.1.2]{C11}: 7774 \begin{cquote} 7775 \Indexc{assert.h}, \Indexc{complex.h}, \Indexc{ctype.h}, \Indexc{errno.h}, \Indexc{fenv.h}, 7776 \Indexc[deletekeywords=float]{float.h}, \Indexc{inttypes.h}, \Indexc{iso646.h}, \Indexc{limits.h}, 7777 \Indexc{locale.h}, \Indexc{math.h}, \Indexc{setjmp.h}, \Indexc{signal.h}, \Indexc{stdalign.h}, 7778 \Indexc{stdarg.h}, \Indexc{stdatomic.h}, \Indexc{stdbool.h}, \Indexc{stddef.h}, \Indexc{stdint.h}, 7779 \Indexc{stdio.h}, \Indexc{stdlib.h}, \Indexc{stdnoreturn.h}, \Indexc{string.h}, \Indexc{tgmath.h}, 7780 \Indexc{threads.h}, \Indexc{time.h}, \Indexc{uchar.h}, \Indexc{wchar.h}, \Indexc{wctype.h} 7781 \end{cquote} 7782 and \CFA adds to this list: 7783 \begin{cquote} 7784 \Indexc{gmp.h}, \Indexc{malloc.h}, \Indexc{unistd.h} 7785 \end{cquote} 7786 \begin{comment} 7714 7787 \begin{cquote} 7715 7788 \begin{tabular}{@{}llllll|l@{}} … … 7773 7846 \end{tabular} 7774 7847 \end{cquote} 7848 \end{comment} 7775 7849 For the prescribed head-files, \CFA uses header interposition to wraps these includes in an ©extern "C"©; 7776 7850 hence, names in these include files are not mangled\index{mangling!name} \see{\VRef{s:Interoperability}}. … … 7837 7911 Type-safe allocation is provided for all C allocation routines and new \CFA allocation routines, \eg in 7838 7912 \begin{cfa} 7839 int * ip = (int *)malloc( sizeof(int) ); 7840 int * ip = malloc(); 7841 int * ip = alloc(); 7913 int * ip = (int *)malloc( sizeof(int) ); §\C{// C}§ 7914 int * ip = malloc(); §\C{// \CFA type-safe version of C malloc}§ 7915 int * ip = alloc(); §\C{// \CFA type-safe uniform alloc}§ 7842 7916 \end{cfa} 7843 7917 the latter two allocations determine the allocation size from the type of ©p© (©int©) and cast the pointer to the allocated storage to ©int *©. … … 7846 7920 \begin{cfa} 7847 7921 struct S { int i; } __attribute__(( aligned( 128 ) )); // cache-line alignment 7848 S * sp = malloc(); 7922 S * sp = malloc(); §\C{// honour type alignment}§ 7849 7923 \end{cfa} 7850 7924 the storage allocation is implicitly aligned to 128 rather than the default 16. … … 7861 7935 \CFA memory management extends allocation to support constructors for initialization of allocated storage, \eg in 7862 7936 \begin{cfa} 7863 struct S { int i; }; 7937 struct S { int i; }; §\C{// cache-line alignment}§ 7864 7938 void ?{}( S & s, int i ) { s.i = i; } 7865 7939 // assume ?|? operator for printing an S 7866 7940 7867 S & sp = *®new®( 3 ); 7941 S & sp = *®new®( 3 ); §\C{// call constructor after allocation}§ 7868 7942 sout | sp.i; 7869 7943 ®delete®( &sp ); 7870 7944 7871 S * spa = ®anew®( 10, 5 ); 7945 S * spa = ®anew®( 10, 5 ); §\C{// allocate array and initialize each array element}§ 7872 7946 for ( i; 10 ) sout | spa[i] | nonl; 7873 7947 sout | nl; … … 7881 7955 extern "C" { 7882 7956 // C unsafe allocation 7883 void * malloc( size_t size ); $\indexc{malloc}$7884 void * calloc( size_t dim, size_t size ); $\indexc{calloc}$7885 void * realloc( void * ptr, size_t size ); $\indexc{realloc}$7886 void * memalign( size_t align, size_t size ); $\indexc{memalign}$7887 void * aligned_alloc( size_t align, size_t size ); $\indexc{aligned_alloc}$7888 int posix_memalign( void ** ptr, size_t align, size_t size ); $\indexc{posix_memalign}$7889 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); $\indexc{cmemalign}$// CFA7957 void * malloc( size_t size );§\indexc{malloc}§ 7958 void * calloc( size_t dim, size_t size );§\indexc{calloc}§ 7959 void * realloc( void * ptr, size_t size );§\indexc{realloc}§ 7960 void * memalign( size_t align, size_t size );§\indexc{memalign}§ 7961 void * aligned_alloc( size_t align, size_t size );§\indexc{aligned_alloc}§ 7962 int posix_memalign( void ** ptr, size_t align, size_t size );§\indexc{posix_memalign}§ 7963 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize );§\indexc{cmemalign}§ // CFA 7890 7964 7891 7965 // C unsafe initialization/copy 7892 void * memset( void * dest, int c, size_t size ); $\indexc{memset}$7893 void * memcpy( void * dest, const void * src, size_t size ); $\indexc{memcpy}$7966 void * memset( void * dest, int c, size_t size );§\indexc{memset}§ 7967 void * memcpy( void * dest, const void * src, size_t size );§\indexc{memcpy}§ 7894 7968 } 7895 7969 … … 7897 7971 7898 7972 forall( dtype T | sized(T) ) { 7899 // $\CFA$safe equivalents, i.e., implicit size specification7973 // §\CFA§ safe equivalents, i.e., implicit size specification 7900 7974 T * malloc( void ); 7901 7975 T * calloc( size_t dim ); … … 7906 7980 int posix_memalign( T ** ptr, size_t align ); 7907 7981 7908 // $\CFA$safe general allocation, fill, resize, alignment, array7909 T * alloc( void ); $\indexc{alloc}$ $\C[3.5in]{// variable, T size}$7982 // §\CFA§ safe general allocation, fill, resize, alignment, array 7983 T * alloc( void );§\indexc{alloc}§ §\C[3.5in]{// variable, T size}§ 7910 7984 T * alloc( size_t dim ); §\C{// array[dim], T size elements}§ 7911 7985 T * alloc( T ptr[], size_t dim ); §\C{// realloc array[dim], T size elements}§ 7912 7986 7913 T * alloc_set( char fill ); $\indexc{alloc_set}$§\C{// variable, T size, fill bytes with value}§7987 T * alloc_set( char fill );§\indexc{alloc_set}§ §\C{// variable, T size, fill bytes with value}§ 7914 7988 T * alloc_set( T fill ); §\C{// variable, T size, fill with value}§ 7915 7989 T * alloc_set( size_t dim, char fill ); §\C{// array[dim], T size elements, fill bytes with value}§ … … 7930 8004 T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); §\C{// realloc new aligned array[dim], fill new bytes with value}§ 7931 8005 7932 // $\CFA$safe initialization/copy, i.e., implicit size specification7933 T * memset( T * dest, char fill ); $\indexc{memset}$7934 T * memcpy( T * dest, const T * src ); $\indexc{memcpy}$7935 7936 // $\CFA$safe initialization/copy, i.e., implicit size specification, array types8006 // §\CFA§ safe initialization/copy, i.e., implicit size specification 8007 T * memset( T * dest, char fill );§\indexc{memset}§ 8008 T * memcpy( T * dest, const T * src );§\indexc{memcpy}§ 8009 8010 // §\CFA§ safe initialization/copy, i.e., implicit size specification, array types 7937 8011 T * amemset( T dest[], char fill, size_t dim ); 7938 8012 T * amemcpy( T dest[], const T src[], size_t dim ); 7939 8013 } 7940 8014 7941 // $\CFA$allocation/deallocation and constructor/destructor, non-array types7942 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p ); $\indexc{new}$7943 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr ); $\indexc{delete}$8015 // §\CFA§ allocation/deallocation and constructor/destructor, non-array types 8016 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );§\indexc{new}§ 8017 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );§\indexc{delete}§ 7944 8018 forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) 7945 8019 void delete( T * ptr, Params rest ); 7946 8020 7947 // $\CFA$allocation/deallocation and constructor/destructor, array types7948 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p ); $\indexc{anew}$7949 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] ); $\indexc{adelete}$8021 // §\CFA§ allocation/deallocation and constructor/destructor, array types 8022 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );§\indexc{anew}§ 8023 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );§\indexc{adelete}§ 7950 8024 forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) 7951 8025 void adelete( size_t dim, T arr[], Params rest ); … … 7957 8031 \leavevmode 7958 8032 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7959 int ato( const char * ptr ); $\indexc{ato}$8033 int ato( const char * ptr );§\indexc{ato}§ 7960 8034 unsigned int ato( const char * ptr ); 7961 8035 long int ato( const char * ptr ); … … 7990 8064 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7991 8065 forall( T | { int ?<?( T, T ); } ) §\C{// location}§ 7992 T * bsearch( T key, const T * arr, size_t dim ); $\indexc{bsearch}$8066 T * bsearch( T key, const T * arr, size_t dim );§\indexc{bsearch}§ 7993 8067 7994 8068 forall( T | { int ?<?( T, T ); } ) §\C{// position}§ … … 7996 8070 7997 8071 forall( T | { int ?<?( T, T ); } ) 7998 void qsort( const T * arr, size_t dim ); $\indexc{qsort}$8072 void qsort( const T * arr, size_t dim );§\indexc{qsort}§ 7999 8073 8000 8074 forall( E | { int ?<?( E, E ); } ) { 8001 E * bsearch( E key, const E * vals, size_t dim ); $\indexc{bsearch}$§\C{// location}§8075 E * bsearch( E key, const E * vals, size_t dim );§\indexc{bsearch}§ §\C{// location}§ 8002 8076 size_t bsearch( E key, const E * vals, size_t dim );§\C{// position}§ 8003 E * bsearchl( E key, const E * vals, size_t dim ); $\indexc{bsearchl}$8077 E * bsearchl( E key, const E * vals, size_t dim );§\indexc{bsearchl}§ 8004 8078 size_t bsearchl( E key, const E * vals, size_t dim ); 8005 E * bsearchu( E key, const E * vals, size_t dim ); $\indexc{bsearchu}$8079 E * bsearchu( E key, const E * vals, size_t dim );§\indexc{bsearchu}§ 8006 8080 size_t bsearchu( E key, const E * vals, size_t dim ); 8007 8081 } … … 8017 8091 8018 8092 forall( E | { int ?<?( E, E ); } ) { 8019 void qsort( E * vals, size_t dim ); $\indexc{qsort}$8093 void qsort( E * vals, size_t dim );§\indexc{qsort}§ 8020 8094 } 8021 8095 \end{cfa} … … 8026 8100 \leavevmode 8027 8101 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 8028 unsigned char abs( signed char ); $\indexc{abs}$8102 unsigned char abs( signed char );§\indexc{abs}§ 8029 8103 int abs( int ); 8030 8104 unsigned long int abs( long int ); … … 8041 8115 8042 8116 8043 \subsection{ Random Numbers}8117 \subsection{C Random Numbers} 8044 8118 8045 8119 \leavevmode 8046 8120 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 8047 void srandom( unsigned int seed ); $\indexc{srandom}$8048 char random( void ); $\indexc{random}$8121 void srandom( unsigned int seed );§\indexc{srandom}§ 8122 char random( void );§\indexc{random}§ 8049 8123 char random( char u ); §\C{// [0,u)}§ 8050 8124 char random( char l, char u ); §\C{// [l,u]}§ … … 8073 8147 \leavevmode 8074 8148 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 8075 forall( T | { int ?<?( T, T ); } ) T min( T t1, T t2 ); $\indexc{min}$8076 forall( T | { int ?>?( T, T ); } ) T max( T t1, T t2 ); $\indexc{max}$8077 forall( T | { T min( T, T ); T max( T, T ); } ) T clamp( T value, T min_val, T max_val ); $\indexc{clamp}$8078 forall( T ) void swap( T * t1, T * t2 ); $\indexc{swap}$8149 forall( T | { int ?<?( T, T ); } ) T min( T t1, T t2 );§\indexc{min}§ 8150 forall( T | { int ?>?( T, T ); } ) T max( T t1, T t2 );§\indexc{max}§ 8151 forall( T | { T min( T, T ); T max( T, T ); } ) T clamp( T value, T min_val, T max_val );§\indexc{clamp}§ 8152 forall( T ) void swap( T * t1, T * t2 );§\indexc{swap}§ 8079 8153 \end{cfa} 8080 8154 … … 8090 8164 \leavevmode 8091 8165 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 8092 float ?%?( float, float ); $\indexc{fmod}$8166 float ?%?( float, float );§\indexc{fmod}§ 8093 8167 float fmod( float, float ); 8094 8168 double ?%?( double, double ); … … 8097 8171 long double fmod( long double, long double ); 8098 8172 8099 float remainder( float, float ); $\indexc{remainder}$8173 float remainder( float, float );§\indexc{remainder}§ 8100 8174 double remainder( double, double ); 8101 8175 long double remainder( long double, long double ); 8102 8176 8103 float remquo( float, float, int * ); $\indexc{remquo}$8177 float remquo( float, float, int * );§\indexc{remquo}§ 8104 8178 double remquo( double, double, int * ); 8105 8179 long double remquo( long double, long double, int * ); … … 8112 8186 [ int, long double ] div( long double, long double ); 8113 8187 8114 float fma( float, float, float ); $\indexc{fma}$8188 float fma( float, float, float );§\indexc{fma}§ 8115 8189 double fma( double, double, double ); 8116 8190 long double fma( long double, long double, long double ); 8117 8191 8118 float fdim( float, float ); $\indexc{fdim}$8192 float fdim( float, float );§\indexc{fdim}§ 8119 8193 double fdim( double, double ); 8120 8194 long double fdim( long double, long double ); 8121 8195 8122 float nan( const char * ); $\indexc{nan}$8196 float nan( const char * );§\indexc{nan}§ 8123 8197 double nan( const char * ); 8124 8198 long double nan( const char * ); … … 8130 8204 \leavevmode 8131 8205 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 8132 float exp( float ); $\indexc{exp}$8206 float exp( float );§\indexc{exp}§ 8133 8207 double exp( double ); 8134 8208 long double exp( long double ); … … 8137 8211 long double _Complex exp( long double _Complex ); 8138 8212 8139 float exp2( float ); $\indexc{exp2}$8213 float exp2( float );§\indexc{exp2}§ 8140 8214 double exp2( double ); 8141 8215 long double exp2( long double ); … … 8144 8218 // long double _Complex exp2( long double _Complex ); 8145 8219 8146 float expm1( float ); $\indexc{expm1}$8220 float expm1( float );§\indexc{expm1}§ 8147 8221 double expm1( double ); 8148 8222 long double expm1( long double ); 8149 8223 8150 float pow( float, float ); $\indexc{pow}$8224 float pow( float, float );§\indexc{pow}§ 8151 8225 double pow( double, double ); 8152 8226 long double pow( long double, long double ); … … 8161 8235 \leavevmode 8162 8236 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 8163 float log( float ); $\indexc{log}$8237 float log( float );§\indexc{log}§ 8164 8238 double log( double ); 8165 8239 long double log( long double ); … … 8168 8242 long double _Complex log( long double _Complex ); 8169 8243 8170 int log2( unsigned int ); $\indexc{log2}$8244 int log2( unsigned int );§\indexc{log2}§ 8171 8245 long int log2( unsigned long int ); 8172 8246 long long int log2( unsigned long long int ) … … 8178 8252 // long double _Complex log2( long double _Complex ); 8179 8253 8180 float log10( float ); $\indexc{log10}$8254 float log10( float );§\indexc{log10}§ 8181 8255 double log10( double ); 8182 8256 long double log10( long double ); … … 8185 8259 // long double _Complex log10( long double _Complex ); 8186 8260 8187 float log1p( float ); $\indexc{log1p}$8261 float log1p( float );§\indexc{log1p}§ 8188 8262 double log1p( double ); 8189 8263 long double log1p( long double ); 8190 8264 8191 int ilogb( float ); $\indexc{ilogb}$8265 int ilogb( float );§\indexc{ilogb}§ 8192 8266 int ilogb( double ); 8193 8267 int ilogb( long double ); 8194 8268 8195 float logb( float ); $\indexc{logb}$8269 float logb( float );§\indexc{logb}§ 8196 8270 double logb( double ); 8197 8271 long double logb( long double ); 8198 8272 8199 float sqrt( float ); $\indexc{sqrt}$8273 float sqrt( float );§\indexc{sqrt}§ 8200 8274 double sqrt( double ); 8201 8275 long double sqrt( long double ); … … 8204 8278 long double _Complex sqrt( long double _Complex ); 8205 8279 8206 float cbrt( float ); $\indexc{cbrt}$8280 float cbrt( float );§\indexc{cbrt}§ 8207 8281 double cbrt( double ); 8208 8282 long double cbrt( long double ); 8209 8283 8210 float hypot( float, float ); $\indexc{hypot}$8284 float hypot( float, float );§\indexc{hypot}§ 8211 8285 double hypot( double, double ); 8212 8286 long double hypot( long double, long double ); … … 8218 8292 \leavevmode 8219 8293 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 8220 float sin( float ); $\indexc{sin}$8294 float sin( float );§\indexc{sin}§ 8221 8295 double sin( double ); 8222 8296 long double sin( long double ); … … 8225 8299 long double _Complex sin( long double _Complex ); 8226 8300 8227 float cos( float ); $\indexc{cos}$8301 float cos( float );§\indexc{cos}§ 8228 8302 double cos( double ); 8229 8303 long double cos( long double ); … … 8232 8306 long double _Complex cos( long double _Complex ); 8233 8307 8234 float tan( float ); $\indexc{tan}$8308 float tan( float );§\indexc{tan}§ 8235 8309 double tan( double ); 8236 8310 long double tan( long double ); … … 8239 8313 long double _Complex tan( long double _Complex ); 8240 8314 8241 float asin( float ); $\indexc{asin}$8315 float asin( float );§\indexc{asin}§ 8242 8316 double asin( double ); 8243 8317 long double asin( long double ); … … 8246 8320 long double _Complex asin( long double _Complex ); 8247 8321 8248 float acos( float ); $\indexc{acos}$8322 float acos( float );§\indexc{acos}§ 8249 8323 double acos( double ); 8250 8324 long double acos( long double ); … … 8253 8327 long double _Complex acos( long double _Complex ); 8254 8328 8255 float atan( float ); $\indexc{atan}$8329 float atan( float );§\indexc{atan}§ 8256 8330 double atan( double ); 8257 8331 long double atan( long double ); … … 8260 8334 long double _Complex atan( long double _Complex ); 8261 8335 8262 float atan2( float, float ); $\indexc{atan2}$8336 float atan2( float, float );§\indexc{atan2}§ 8263 8337 double atan2( double, double ); 8264 8338 long double atan2( long double, long double ); 8265 8339 8266 8340 float atan( float, float ); §\C{// alternative name for atan2}§ 8267 double atan( double, double ); $\indexc{atan}$8341 double atan( double, double );§\indexc{atan}§ 8268 8342 long double atan( long double, long double ); 8269 8343 \end{cfa} … … 8274 8348 \leavevmode 8275 8349 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 8276 float sinh( float ); $\indexc{sinh}$8350 float sinh( float );§\indexc{sinh}§ 8277 8351 double sinh( double ); 8278 8352 long double sinh( long double ); … … 8281 8355 long double _Complex sinh( long double _Complex ); 8282 8356 8283 float cosh( float ); $\indexc{cosh}$8357 float cosh( float );§\indexc{cosh}§ 8284 8358 double cosh( double ); 8285 8359 long double cosh( long double ); … … 8288 8362 long double _Complex cosh( long double _Complex ); 8289 8363 8290 float tanh( float ); $\indexc{tanh}$8364 float tanh( float );§\indexc{tanh}§ 8291 8365 double tanh( double ); 8292 8366 long double tanh( long double ); … … 8295 8369 long double _Complex tanh( long double _Complex ); 8296 8370 8297 float asinh( float ); $\indexc{asinh}$8371 float asinh( float );§\indexc{asinh}§ 8298 8372 double asinh( double ); 8299 8373 long double asinh( long double ); … … 8302 8376 long double _Complex asinh( long double _Complex ); 8303 8377 8304 float acosh( float ); $\indexc{acosh}$8378 float acosh( float );§\indexc{acosh}§ 8305 8379 double acosh( double ); 8306 8380 long double acosh( long double ); … … 8309 8383 long double _Complex acosh( long double _Complex ); 8310 8384 8311 float atanh( float ); $\indexc{atanh}$8385 float atanh( float );§\indexc{atanh}§ 8312 8386 double atanh( double ); 8313 8387 long double atanh( long double ); … … 8322 8396 \leavevmode 8323 8397 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 8324 float erf( float ); $\indexc{erf}$8398 float erf( float );§\indexc{erf}§ 8325 8399 double erf( double ); 8326 8400 long double erf( long double ); … … 8329 8403 long double _Complex erf( long double _Complex ); 8330 8404 8331 float erfc( float ); $\indexc{erfc}$8405 float erfc( float );§\indexc{erfc}§ 8332 8406 double erfc( double ); 8333 8407 long double erfc( long double ); … … 8336 8410 long double _Complex erfc( long double _Complex ); 8337 8411 8338 float lgamma( float ); $\indexc{lgamma}$8412 float lgamma( float );§\indexc{lgamma}§ 8339 8413 double lgamma( double ); 8340 8414 long double lgamma( long double ); … … 8343 8417 long double lgamma( long double, int * ); 8344 8418 8345 float tgamma( float ); $\indexc{tgamma}$8419 float tgamma( float );§\indexc{tgamma}§ 8346 8420 double tgamma( double ); 8347 8421 long double tgamma( long double ); … … 8389 8463 unsigned long long int ceiling( unsigned long long int n, unsigned long long int align ); 8390 8464 8391 float floor( float ); $\indexc{floor}$8465 float floor( float );§\indexc{floor}§ 8392 8466 double floor( double ); 8393 8467 long double floor( long double ); 8394 8468 8395 float ceil( float ); $\indexc{ceil}$8469 float ceil( float );§\indexc{ceil}§ 8396 8470 double ceil( double ); 8397 8471 long double ceil( long double ); 8398 8472 8399 float trunc( float ); $\indexc{trunc}$8473 float trunc( float );§\indexc{trunc}§ 8400 8474 double trunc( double ); 8401 8475 long double trunc( long double ); 8402 8476 8403 float rint( float ); $\indexc{rint}$8477 float rint( float );§\indexc{rint}§ 8404 8478 long double rint( long double ); 8405 8479 long int rint( float ); … … 8410 8484 long long int rint( long double ); 8411 8485 8412 long int lrint( float ); $\indexc{lrint}$8486 long int lrint( float );§\indexc{lrint}§ 8413 8487 long int lrint( double ); 8414 8488 long int lrint( long double ); … … 8417 8491 long long int llrint( long double ); 8418 8492 8419 float nearbyint( float ); $\indexc{nearbyint}$8493 float nearbyint( float );§\indexc{nearbyint}§ 8420 8494 double nearbyint( double ); 8421 8495 long double nearbyint( long double ); 8422 8496 8423 float round( float ); $\indexc{round}$8497 float round( float );§\indexc{round}§ 8424 8498 long double round( long double ); 8425 8499 long int round( float ); … … 8430 8504 long long int round( long double ); 8431 8505 8432 long int lround( float ); $\indexc{lround}$8506 long int lround( float );§\indexc{lround}§ 8433 8507 long int lround( double ); 8434 8508 long int lround( long double ); … … 8443 8517 \leavevmode 8444 8518 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 8445 float copysign( float, float ); $\indexc{copysign}$8519 float copysign( float, float );§\indexc{copysign}§ 8446 8520 double copysign( double, double ); 8447 8521 long double copysign( long double, long double ); 8448 8522 8449 float frexp( float, int * ); $\indexc{frexp}$8523 float frexp( float, int * );§\indexc{frexp}§ 8450 8524 double frexp( double, int * ); 8451 8525 long double frexp( long double, int * ); 8452 8526 8453 float ldexp( float, int ); $\indexc{ldexp}$8527 float ldexp( float, int );§\indexc{ldexp}§ 8454 8528 double ldexp( double, int ); 8455 8529 long double ldexp( long double, int ); 8456 8530 8457 [ float, float ] modf( float ); $\indexc{modf}$8531 [ float, float ] modf( float );§\indexc{modf}§ 8458 8532 float modf( float, float * ); 8459 8533 [ double, double ] modf( double ); … … 8462 8536 long double modf( long double, long double * ); 8463 8537 8464 float nextafter( float, float ); $\indexc{nextafter}$8538 float nextafter( float, float );§\indexc{nextafter}§ 8465 8539 double nextafter( double, double ); 8466 8540 long double nextafter( long double, long double ); 8467 8541 8468 float nexttoward( float, long double ); $\indexc{nexttoward}$8542 float nexttoward( float, long double );§\indexc{nexttoward}§ 8469 8543 double nexttoward( double, long double ); 8470 8544 long double nexttoward( long double, long double ); 8471 8545 8472 float scalbn( float, int ); $\indexc{scalbn}$8546 float scalbn( float, int );§\indexc{scalbn}§ 8473 8547 double scalbn( double, int ); 8474 8548 long double scalbn( long double, int ); 8475 8549 8476 float scalbln( float, long int ); $\indexc{scalbln}$8550 float scalbln( float, long int );§\indexc{scalbln}§ 8477 8551 double scalbln( double, long int ); 8478 8552 long double scalbln( long double, long int ); … … 8734 8808 All \newterm{pseudo random-number generators} (\newterm{PRNG}) involve some technique to scramble bits of a value, \eg multiplicative recurrence: 8735 8809 \begin{cfa} 8736 rand = 3 6973 * (rand & 65535) + (rand >> 16); // scramble bits8810 rand = 33967 * (rand + 1063); // scramble bits 8737 8811 \end{cfa} 8738 8812 Multiplication of large values adds new least-significant bits and drops most-significant bits. … … 8741 8815 bits 63--32 (most) & bits 31--0 (least) \\ 8742 8816 \hline 8743 0x0 & 0x3e8e36\\8744 0x5f & 0x718c25e1\\8745 0xad3e & 0x7b5f1dbe\\8746 0xbc3b & 0xac69ff19\\8747 0x1070f & 0x2d258dc6 \\ 8817 ©0x0© & ©0x3e8e36© \\ 8818 ©0x5f© & ©0x718c25e1© \\ 8819 ©0xad3e© & ©0x7b5f1dbe© \\ 8820 ©0xbc3b© & ©0xac69ff19© \\ 8821 ©0x1070f© & ©0x2d258dc6© 8748 8822 \end{tabular} 8749 8823 \end{quote} … … 8751 8825 The least-significant bits \emph{appear} random but the same bits are always generated given a fixed starting value, called the \newterm{seed} (value 0x3e8e36 above). 8752 8826 Hence, if a program uses the same seed, the same sequence of pseudo-random values is generated from the PRNG. 8753 Often the seed is set to another random value like a program's process identifier ( ©getpid©\index{getpid@©getpid©}) or time when the program is run;8827 Often the seed is set to another random value like a program's process identifier (\Indexc{getpid}) or time when the program is run; 8754 8828 hence, one random value bootstraps another. 8755 8829 Finally, a PRNG usually generates a range of large values, \eg ©[0, UINT_MAX]©, which are scaled using the modulus operator, \eg ©prng() % 5© produces random values in the range 0--4. 8756 8830 8757 \CFA provides a sequential PRNG type only accessible by a single thread (not thread-safe) and a set of global and companion thread PRNG functions accessible by multiple threads without contention. 8831 \CFA provides 32/64-bit sequential PRNG type only accessible by a single thread (not thread-safe) and a set of global routines and companion thread PRNG functions accessible by multiple threads without contention. 8832 To use the PRNG interface \Textbf{requires including \Indexc{stdlib.hfa}}. 8758 8833 \begin{itemize} 8759 8834 \item 8760 The ©PRNG© type is for sequential programs, like coroutining: 8761 \begin{cfa} 8762 struct PRNG { ... }; $\C[3.75in]{// opaque type}$ 8763 void ?{}( PRNG & prng ); §\C{// random seed}§ 8764 void ?{}( PRNG & prng, uint32_t seed ); §\C{// fixed seed}§ 8765 void set_seed( PRNG & prng, uint32_t seed ); §\C{// set seed}§ 8766 uint32_t get_seed( PRNG & prng ); §\C{// get seed}§ 8767 uint32_t prng( PRNG & prng ); §\C{// [0,UINT\_MAX]}§ 8768 uint32_t prng( PRNG & prng, uint32_t u ); §\C{// [0,u)}§ 8769 uint32_t prng( PRNG & prng, uint32_t l, uint32_t u ); §\C{// [l,u]}§ 8770 uint32_t calls( PRNG & prng ); §\C{// number of calls}\CRT§ 8771 \end{cfa} 8835 The ©PRNG© types for sequential programs, including coroutining, are: 8836 \begin{cfa} 8837 struct PRNG32 {}; §\C{// opaque type, no copy or assignment}§ 8838 void ?{}( PRNG32 & prng, uint32_t seed ); §\C{// fixed seed}§ 8839 void ?{}( PRNG32 & prng ); §\C{// random seed}§ 8840 void set_seed( PRNG32 & prng, uint32_t seed ); §\C{// set seed}§ 8841 uint32_t get_seed( PRNG32 & prng ); §\C{// get seed}§ 8842 uint32_t prng( PRNG32 & prng ); §\C{// [0,UINT\_MAX]}§ 8843 uint32_t prng( PRNG32 & prng, uint32_t u ); §\C{// [0,u)}§ 8844 uint32_t prng( PRNG32 & prng, uint32_t l, uint32_t u ); §\C{// [l,u]}§ 8845 uint32_t calls( PRNG32 & prng ); §\C{// number of calls}§ 8846 void copy( PRNG32 & dst, PRNG32 & src ); §\C{// checkpoint PRNG state}§ 8847 \end{cfa} 8848 \begin{cfa} 8849 struct PRNG64 {}; §\C{// opaque type, no copy or assignment}§ 8850 void ?{}( PRNG64 & prng, uint64_t seed ); §\C{// fixed seed}§ 8851 void ?{}( PRNG64 & prng ); §\C{// random seed}§ 8852 void set_seed( PRNG64 & prng, uint64_t seed ); §\C{// set seed}§ 8853 uint64_t get_seed( PRNG64 & prng ); §\C{// get seed}§ 8854 uint64_t prng( PRNG64 & prng ); §\C{// [0,UINT\_MAX]}§ 8855 uint64_t prng( PRNG64 & prng, uint64_t u ); §\C{// [0,u)}§ 8856 uint64_t prng( PRNG64 & prng, uint64_t l, uint64_t u ); §\C{// [l,u]}§ 8857 uint64_t calls( PRNG64 & prng ); §\C{// number of calls}§ 8858 void copy( PRNG64 & dst, PRNG64 & src ); §\C{// checkpoint PRNG state}§ 8859 \end{cfa} 8860 The type ©PRNG© is aliased to ©PRNG64© on 64-bit architectures and ©PRNG32© on 32-bit architectures. 8772 8861 A ©PRNG© object is used to randomize behaviour or values during execution, \eg in games, a character makes a random move or an object takes on a random value. 8773 8862 In this scenario, it is useful to have multiple ©PRNG© objects, \eg one per player or object. 8774 However, sequential execution is still repeatable given the same starting seeds for all ©PRNG©s. 8863 However, sequential execution is still repeatable given the same starting seeds for all ©PRNG©s. 8775 8864 \VRef[Figure]{f:SequentialPRNG} shows an example that creates two sequential ©PRNG©s, sets both to the same seed (1009), and illustrates the three forms for generating random values, where both ©PRNG©s generate the same sequence of values. 8865 Note, to prevent accidental PRNG copying, the copy constructor and assignment are hidden. 8866 To copy a PRNG for checkpointing, use the explicit ©copy© member. 8776 8867 8777 8868 \begin{figure} 8778 8869 \begin{cfa} 8779 PRNG prng1, prng2;8780 ®set_seed( prng1, 1009 )®; ®set_seed(prng2, 1009 )®;8870 PRNG sprng1, sprng2; §\C{// select appropriate 32/64-bit PRNG}§ 8871 ®set_seed( sprng1, 1009 )®; ®set_seed( sprng2, 1009 )®; 8781 8872 for ( 10 ) { 8782 8873 // Do not cascade prng calls because side-effect functions called in arbitrary order. 8783 sout | nlOff | ®prng( prng1 )®; sout | ®prng( prng1, 5 )®; sout | ®prng(prng1, 0, 5 )® | '\t';8784 sout | ®prng( prng2 )®; sout | ®prng( prng2, 5 )®; sout | ®prng(prng2, 0, 5 )® | nlOn;8874 sout | nlOff | ®prng( sprng1 )®; sout | ®prng( sprng1, 5 )®; sout | ®prng( sprng1, 0, 5 )® | '\t'; 8875 sout | ®prng( sprng2 )®; sout | ®prng( sprng2, 5 )®; sout | ®prng( sprng2, 0, 5 )® | nlOn; 8785 8876 } 8786 8877 \end{cfa} … … 8821 8912 The PRNG global and companion thread functions are for concurrent programming, such as randomizing execution in short-running programs, \eg ©yield( prng() % 5 )©. 8822 8913 \begin{cfa} 8823 void set_seed( uint32_t seed ); $\C[3.75in]{// set global seed}$8824 uint32_t get_seed();§\C{// get global seed}§8825 // SLOWER 8826 uint32_t prng();§\C{// [0,UINT\_MAX]}§8827 uint32_t prng( uint32_t u );§\C{// [0,u)}§8828 uint32_t prng( uint32_t l, uint32_t u );§\C{// [l,u]}§8829 // FASTER 8830 uint32_t prng( $thread\LstStringStyle{\textdollar}$& th ); §\C{// [0,UINT\_MAX]}§8831 uint32_t prng( $thread\LstStringStyle{\textdollar}$ & th, uint32_t u ); §\C{// [0,u)}§8832 uint32_t prng( $thread\LstStringStyle{\textdollar}$ & th, uint32_t l, uint32_t u ); §\C{// [l,u]}\CRT§8914 void set_seed( size_t seed ); §\C{// set global seed}§ 8915 size_t get_seed(); §\C{// get global seed}§ 8916 // SLOWER, global routines 8917 size_t prng( void ); §\C{// [0,UINT\_MAX]}§ 8918 size_t prng( size_t u ); §\C{// [0,u)}§ 8919 size_t prng( size_t l, size_t u ); §\C{// [l,u]}§ 8920 // FASTER, thread members 8921 size_t prng( §thread\LstStringStyle{\textdollar}§ & th ); §\C{// [0,UINT\_MAX]}§ 8922 size_t prng( §thread\LstStringStyle{\textdollar}§ & th, size_t u ); §\C{// [0,u)}§ 8923 size_t prng( §thread\LstStringStyle{\textdollar}§ & th, size_t l, size_t u ); §\C{// [l,u]}§ 8833 8924 \end{cfa} 8834 8925 The only difference between the two sets of ©prng© routines is performance. … … 8841 8932 Hence, these threads generate different sequences of random numbers. 8842 8933 If each thread needs its own seed, use a sequential ©PRNG© in each thread. 8843 The slower ©prng© functions \emph{without} a thread argument call ©active_thread© internally to indirectly access the current thread's PRNG state, while the faster ©prng© functions \emph{with} a thread argumentdirectly access the thread through the thread parameter.8934 The slower ©prng© global functions, \ie \emph{without} a thread argument, call ©active_thread© internally to indirectly access the current thread's PRNG state, while the faster ©prng© functions, \ie \emph{with} a thread argument, directly access the thread through the thread parameter. 8844 8935 If a thread pointer is available, \eg in thread main, eliminating the call to ©active_thread© significantly reduces the cost of accessing the thread's PRNG state. 8845 8936 \VRef[Figure]{f:ConcurrentPRNG} shows an example using the slower/faster concurrent PRNG in the program main and a thread. … … 8856 8947 int main() { 8857 8948 set_seed( 1009 ); 8858 $\R{thread\LstStringStyle{\textdollar}}$®& th = *active_thread()®; // program-main thread-address8949 §\R{thread\LstStringStyle{\textdollar}}§ ®& th = *active_thread()®; // program-main thread-address 8859 8950 for ( i; 10 ) { 8860 8951 sout | nlOff | ®prng()®; sout | ®prng( 5 )®; sout | ®prng( 0, 5 )® | '\t'; // SLOWER … … 9079 9170 \hline 9080 9171 \begin{cfa} 9081 #include <gmp.h> $\indexc{gmp.h}$9172 #include <gmp.h>§\indexc{gmp.h}§ 9082 9173 int main( void ) { 9083 9174 ®gmp_printf®( "Factorial Numbers\n" ); … … 9093 9184 & 9094 9185 \begin{cfa} 9095 #include <gmp.hfa> $\indexc{gmp}$9186 #include <gmp.hfa>§\indexc{gmp}§ 9096 9187 int main( void ) { 9097 9188 sout | "Factorial Numbers"; … … 9165 9256 \begin{cfa}[belowskip=0pt] 9166 9257 // implementation 9167 struct Rational { $\indexc{Rational}$9258 struct Rational {§\indexc{Rational}§ 9168 9259 long int numerator, denominator; §\C{// invariant: denominator > 0}§ 9169 9260 }; // Rational
Note:
See TracChangeset
for help on using the changeset viewer.