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