source: src/libcfa/stdlib @ cb811ac

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since cb811ac was cb811ac, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

change stdlib swap to use references, and update swap tests

  • Property mode set to 100644
File size: 8.5 KB
RevLine 
[bd85400]1//
2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[bb82c03]7// stdlib --
[bd85400]8//
9// Author           : Peter A. Buhr
10// Created On       : Thu Jan 28 17:12:35 2016
11// Last Modified By : Peter A. Buhr
[cb811ac]12// Last Modified On : Wed Aug 23 20:29:47 2017
13// Update Count     : 224
[bd85400]14//
15
[53a6c2a]16#pragma once
[17e5e2b]17
[bd85400]18//---------------------------------------
19
[45161b4d]20#ifndef EXIT_FAILURE
21#define EXIT_FAILURE    1                                                               // failing exit status
22#define EXIT_SUCCESS    0                                                               // successful exit status
23#endif // ! EXIT_FAILURE
24
25//---------------------------------------
26
[f3fc631f]27// allocation, non-array types
28static inline forall( dtype T | sized(T) ) T * malloc( void ) {
29        //printf( "X1\n" );
30        return (T *)(void *)malloc( (size_t)sizeof(T) );        // C malloc
31} // malloc
32
[6065b3aa]33extern "C" { void * calloc( size_t dim, size_t size ); } // default C routine
[f3fc631f]34static inline forall( dtype T | sized(T) ) T * calloc( size_t dim ) {
[6065b3aa]35        //printf( "X2\n" );
[f3fc631f]36        return (T *)(void *)calloc( dim, sizeof(T) );           // C cmalloc
37}
38
[6065b3aa]39extern "C" { void * realloc( void * ptr, size_t size ); } // default C routine for void *
[f3fc631f]40static inline forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) {
[6065b3aa]41        //printf( "X3\n" );
[f3fc631f]42        return (T *)(void *)realloc( (void *)ptr, size );
43}
[6065b3aa]44
45extern "C" { void * memalign( size_t align, size_t size ); } // use default C routine for void *
46static inline forall( dtype T | sized(T) ) T * memalign( size_t align ) {
47        //printf( "X4\n" );
48        return (T *)memalign( align, sizeof(T) );
49} // memalign
50
51static inline forall( dtype T | sized(T) ) T * aligned_alloc( size_t align ) {
52        //printf( "X5\n" );
53        return (T *)memalign( align, sizeof(T) );
54} // aligned_alloc
55
56extern "C" { int posix_memalign( void ** ptr, size_t align, size_t size ); } // use default C routine for void *
57static inline forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t align ) {
58        //printf( "X6\n" );
59        return posix_memalign( (void **)ptr, align, sizeof(T) );
60} // posix_memalign
61
62
63extern "C" { void * memset( void * dest, int c, size_t size ); } // use default C routine for void *
64
65static inline forall( dtype T | sized(T) ) T * alloc( void ) {
[f3fc631f]66        //printf( "X7\n" );
[6065b3aa]67        return (T *)(void *)malloc( (size_t)sizeof(T) );        // C malloc
68} // alloc
69static inline forall( dtype T | sized(T) ) T * alloc( char fill ) {
[f3fc631f]70        //printf( "X8\n" );
[6065b3aa]71        T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) );     // C malloc
72    return memset( ptr, (int)fill, sizeof(T) );                 // initial with fill value
73} // alloc
[f3fc631f]74
[6065b3aa]75static inline forall( dtype T | sized(T) ) T * alloc( size_t dim ) {
[f3fc631f]76        //printf( "X9\n" );
[6065b3aa]77        return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
78} // alloc
79static inline forall( dtype T | sized(T) ) T * alloc( size_t dim, char fill ) {
[f3fc631f]80        //printf( "X10\n" );
[6065b3aa]81        T * ptr = (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
82    return memset( ptr, (int)fill, dim * sizeof(T) );
83} // alloc
[f3fc631f]84
[6065b3aa]85static inline forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim ) {
[f3fc631f]86        //printf( "X11\n" );
[6065b3aa]87        return (void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
88} // alloc
89forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill );
90
91static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align ) {
[f3fc631f]92        //printf( "X13\n" );
[6065b3aa]93        return (T *)memalign( align, sizeof(T) );
94} // align_alloc
95static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, char fill ) {
[f3fc631f]96        //printf( "X14\n" );
[6065b3aa]97    T * ptr = (T *)memalign( align, sizeof(T) );
98    return memset( ptr, (int)fill, sizeof(T) );
99} // align_alloc
[f3fc631f]100
[6065b3aa]101static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim ) {
[f3fc631f]102        //printf( "X15\n" );
[6065b3aa]103        return (T *)memalign( align, dim * sizeof(T) );
104} // align_alloc
105static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim, char fill ) {
[f3fc631f]106        //printf( "X16\n" );
[6065b3aa]107    T * ptr = (T *)memalign( align, dim * sizeof(T) );
[f3fc631f]108    return memset( ptr, (int)fill, dim * sizeof(T) );
[6065b3aa]109} // align_alloc
110
[f3fc631f]111
112// data, non-array types
113static inline forall( dtype T | sized(T) ) T * memset( T * dest, char c ) {
114        //printf( "X17\n" );
115        return memset( dest, c, sizeof(T) );
116} // memset
117extern "C" { void * memcpy( void * dest, const void * src, size_t size ); } // use default C routine for void *
118static inline forall( dtype T | sized(T) ) T * memcpy( T * dest, const T * src ) {
119        //printf( "X18\n" );
120        return memcpy( dest, src, sizeof(T) );
121} // memcpy
122
123// data, array types
[6065b3aa]124static inline forall( dtype T | sized(T) ) T * memset( T dest[], size_t dim, char c ) {
[f3fc631f]125        //printf( "X19\n" );
[a4683611]126        return (void *)memset( dest, c, dim * sizeof(T) );      // C memset
[6065b3aa]127} // memset
128static inline forall( dtype T | sized(T) ) T * memcpy( T dest[], const T src[], size_t dim ) {
[f3fc631f]129        //printf( "X20\n" );
[6065b3aa]130        return (void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
131} // memcpy
[f3fc631f]132
[6065b3aa]133// allocation/deallocation and constructor/destructor, non-array types
[aca65621]134forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
[83a071f9]135forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );
136forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
[627f585]137
[6065b3aa]138// allocation/deallocation and constructor/destructor, array types
[aca65621]139forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
140forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
141forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );
[6065b3aa]142
[bd85400]143//---------------------------------------
144
145int ato( const char * ptr );
146unsigned int ato( const char * ptr );
147long int ato( const char * ptr );
148unsigned long int ato( const char * ptr );
149long long int ato( const char * ptr );
150unsigned long long int ato( const char * ptr );
151float ato( const char * ptr );
152double ato( const char * ptr );
153long double ato( const char * ptr );
154float _Complex ato( const char * ptr );
155double _Complex ato( const char * ptr );
156long double _Complex ato( const char * ptr );
157
158int strto( const char * sptr, char ** eptr, int base );
159unsigned int strto( const char * sptr, char ** eptr, int base );
160long int strto( const char * sptr, char ** eptr, int base );
161unsigned long int strto( const char * sptr, char ** eptr, int base );
162long long int strto( const char * sptr, char ** eptr, int base );
163unsigned long long int strto( const char * sptr, char ** eptr, int base );
164float strto( const char * sptr, char ** eptr );
165double strto( const char * sptr, char ** eptr );
166long double strto( const char * sptr, char ** eptr );
167float _Complex strto( const char * sptr, char ** eptr );
168double _Complex strto( const char * sptr, char ** eptr );
169long double _Complex strto( const char * sptr, char ** eptr );
170
171//---------------------------------------
172
[4040425]173forall( otype T | { int ?<?( T, T ); } )
[f3fc631f]174T * bsearch( T key, const T * arr, size_t dim );
[f3ddc21]175
[707446a]176forall( otype T | { int ?<?( T, T ); } )
[f3fc631f]177unsigned int bsearch( T key, const T * arr, size_t dim );
[bd85400]178
[f3ddc21]179
[4040425]180forall( otype T | { int ?<?( T, T ); } )
[f3fc631f]181void qsort( const T * arr, size_t dim );
[bd85400]182
183//---------------------------------------
184
[5ea26ed]185[ int, int ] div( int num, int denom );
186[ long int, long int ] div( long int num, long int denom );
187[ long long int, long long int ] div( long long int num, long long int denom );
[4040425]188forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
[5ea26ed]189[ T, T ] div( T num, T demon );
[bd85400]190
191//---------------------------------------
192
[c3ebf37]193unsigned char abs( signed char );
[6e991d6]194extern "C" { int abs( int ); }                                                  // use default C routine for int
[c3ebf37]195unsigned long int abs( long int );
196unsigned long long int abs( long long int );
[bd85400]197float abs( float );
198double abs( double );
199long double abs( long double );
[6e991d6]200float abs( float _Complex );
201double abs( double _Complex );
202long double abs( long double _Complex );
[aca65621]203forall( otype T | { void ?{}( T &, zero_t ); int ?<?( T, T ); T -?( T ); } )
[f3ddc21]204T abs( T );
[53ba273]205
206//---------------------------------------
207
[0438091]208void rand48seed( long int s );
[8dc51c8]209char rand48( void );
210int rand48( void );
211unsigned int rand48( void );
212long int rand48( void );
213unsigned long int rand48( void );
214float rand48( void );
215double rand48( void );
216float _Complex rand48( void );
217double _Complex rand48( void );
218long double _Complex rand48( void );
[bd85400]219
220//---------------------------------------
221
[4040425]222forall( otype T | { int ?<?( T, T ); } )
[a797e2b1]223T min( T t1, T t2 );
[bd85400]224
[4040425]225forall( otype T | { int ?>?( T, T ); } )
[a797e2b1]226T max( T t1, T t2 );
[bd85400]227
[a9f2c13]228forall( otype T | { T min( T, T ); T max( T, T ); } )
[a797e2b1]229T clamp( T value, T min_val, T max_val );
[a9f2c13]230
[4040425]231forall( otype T )
[cb811ac]232void swap( T & t1, T & t2 );
[bd85400]233
234// Local Variables: //
235// mode: c //
236// tab-width: 4 //
237// End: //
Note: See TracBrowser for help on using the repository browser.