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