source: src/libcfa/stdlib @ 9c47a47

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 9c47a47 was 9c47a47, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

extend stdlib bsearch

  • Property mode set to 100644
File size: 11.4 KB
Line 
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 Dec 28 18:43:12 2017
13// Update Count     : 277
14//
15
16#pragma once
17
18//#define _XOPEN_SOURCE 600                                                             // posix_memalign, *rand48
19#include <stdlib.h>                                                                             // strto*, *abs
20
21//---------------------------------------
22
23#ifndef EXIT_FAILURE
24#define EXIT_FAILURE    1                                                               // failing exit status
25#define EXIT_SUCCESS    0                                                               // successful exit status
26#endif // ! EXIT_FAILURE
27
28//---------------------------------------
29
30// allocation, non-array types
31static inline forall( dtype T | sized(T) ) T * malloc( void ) {
32        // printf( "* malloc\n" );
33        return (T *)(void *)malloc( (size_t)sizeof(T) );        // C malloc
34} // malloc
35
36// static inline forall( dtype T | sized(T) ) T & malloc( void ) {
37//      int & p = *(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
38//      printf( "& malloc %p\n", &p );
39//      return p;
40// //   return (T &)*(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
41// } // malloc
42
43extern "C" { void * calloc( size_t dim, size_t size ); } // default C routine
44static inline forall( dtype T | sized(T) ) T * calloc( size_t dim ) {
45        //printf( "X2\n" );
46        return (T *)(void *)calloc( dim, sizeof(T) );           // C cmalloc
47}
48
49extern "C" { void * realloc( void * ptr, size_t size ); } // default C routine for void *
50static inline forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) {
51        //printf( "X3\n" );
52        return (T *)(void *)realloc( (void *)ptr, size );
53}
54
55extern "C" { void * memalign( size_t align, size_t size ); } // use default C routine for void *
56static inline forall( dtype T | sized(T) ) T * memalign( size_t align ) {
57        //printf( "X4\n" );
58        return (T *)memalign( align, sizeof(T) );
59} // memalign
60
61static inline forall( dtype T | sized(T) ) T * aligned_alloc( size_t align ) {
62        //printf( "X5\n" );
63        return (T *)memalign( align, sizeof(T) );
64} // aligned_alloc
65
66extern "C" { int posix_memalign( void ** ptr, size_t align, size_t size ); } // use default C routine for void *
67static inline forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t align ) {
68        //printf( "X6\n" );
69        return posix_memalign( (void **)ptr, align, sizeof(T) );
70} // posix_memalign
71
72
73extern "C" { void * memset( void * dest, int c, size_t size ); } // use default C routine for void *
74
75static inline forall( dtype T | sized(T) ) T * alloc( void ) {
76        //printf( "X7\n" );
77        return (T *)(void *)malloc( (size_t)sizeof(T) );        // C malloc
78} // alloc
79static inline forall( dtype T | sized(T) ) T * alloc( char fill ) {
80        //printf( "X8\n" );
81        T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) );     // C malloc
82    return (T *)memset( ptr, (int)fill, sizeof(T) );    // initial with fill value
83} // alloc
84
85static inline forall( dtype T | sized(T) ) T * alloc( size_t dim ) {
86        //printf( "X9\n" );
87        return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
88} // alloc
89static inline forall( dtype T | sized(T) ) T * alloc( size_t dim, char fill ) {
90        //printf( "X10\n" );
91        T * ptr = (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
92    return (T *)memset( ptr, (int)fill, dim * sizeof(T) );
93} // alloc
94
95static inline forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim ) {
96        //printf( "X11\n" );
97        return (T *)(void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
98} // alloc
99forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill );
100
101static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align ) {
102        //printf( "X13\n" );
103        return (T *)memalign( align, sizeof(T) );
104} // align_alloc
105static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, char fill ) {
106        //printf( "X14\n" );
107    T * ptr = (T *)memalign( align, sizeof(T) );
108    return (T *)memset( ptr, (int)fill, sizeof(T) );
109} // align_alloc
110
111static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim ) {
112        //printf( "X15\n" );
113        return (T *)memalign( align, dim * sizeof(T) );
114} // align_alloc
115static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim, char fill ) {
116        //printf( "X16\n" );
117    T * ptr = (T *)memalign( align, dim * sizeof(T) );
118    return (T *)memset( ptr, (int)fill, dim * sizeof(T) );
119} // align_alloc
120
121
122// data, non-array types
123static inline forall( dtype T | sized(T) ) T * memset( T * dest, char c ) {
124        //printf( "X17\n" );
125        return (T *)memset( dest, c, sizeof(T) );
126} // memset
127extern "C" { void * memcpy( void * dest, const void * src, size_t size ); } // use default C routine for void *
128static inline forall( dtype T | sized(T) ) T * memcpy( T * dest, const T * src ) {
129        //printf( "X18\n" );
130        return (T *)memcpy( dest, src, sizeof(T) );
131} // memcpy
132
133// data, array types
134static inline forall( dtype T | sized(T) ) T * memset( T dest[], size_t dim, char c ) {
135        //printf( "X19\n" );
136        return (T *)(void *)memset( dest, c, dim * sizeof(T) ); // C memset
137} // memset
138static inline forall( dtype T | sized(T) ) T * memcpy( T dest[], const T src[], size_t dim ) {
139        //printf( "X20\n" );
140        return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
141} // memcpy
142
143// allocation/deallocation and constructor/destructor, non-array types
144forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
145forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );
146forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
147
148// allocation/deallocation and constructor/destructor, array types
149forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
150forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
151forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );
152
153//---------------------------------------
154
155static inline int strto( const char * sptr, char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
156static inline unsigned int strto( const char * sptr, char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
157static inline long int strto( const char * sptr, char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
158static inline unsigned long int strto( const char * sptr, char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
159static inline long long int strto( const char * sptr, char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
160static inline unsigned long long int strto( const char * sptr, char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
161
162static inline float strto( const char * sptr, char ** eptr ) { return strtof( sptr, eptr ); }
163static inline double strto( const char * sptr, char ** eptr ) { return strtod( sptr, eptr ); }
164static inline long double strto( const char * sptr, char ** eptr ) { return strtold( sptr, eptr ); }
165
166float _Complex strto( const char * sptr, char ** eptr );
167double _Complex strto( const char * sptr, char ** eptr );
168long double _Complex strto( const char * sptr, char ** eptr );
169
170static inline int ato( const char * sptr ) {return (int)strtol( sptr, 0, 10 ); }
171static inline unsigned int ato( const char * sptr ) { return (unsigned int)strtoul( sptr, 0, 10 ); }
172static inline long int ato( const char * sptr ) { return strtol( sptr, 0, 10 ); }
173static inline unsigned long int ato( const char * sptr ) { return strtoul( sptr, 0, 10 ); }
174static inline long long int ato( const char * sptr ) { return strtoll( sptr, 0, 10 ); }
175static inline unsigned long long int ato( const char * sptr ) { return strtoull( sptr, 0, 10 ); }
176
177static inline float ato( const char * sptr ) { return strtof( sptr, 0 ); }
178static inline double ato( const char * sptr ) { return strtod( sptr, 0 ); }
179static inline long double ato( const char * sptr ) { return strtold( sptr, 0 ); }
180
181static inline float _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
182static inline double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
183static inline long double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
184
185//---------------------------------------
186
187forall( otype E | { int ?<?( E, E ); } )
188E * bsearch( E key, const E * arr, size_t dim );
189
190forall( otype E | { int ?<?( E, E ); } )
191unsigned int bsearch( E key, const E * arr, size_t dim );
192
193forall( otype K, otype E | { int ?<?( K, K ); K getKey( E & ); } )
194E * bsearch( K key, const E * arr, size_t dim );
195
196forall( otype K, otype E | { int ?<?( K, K ); K getKey( E & ); } )
197unsigned int bsearch( K key, const E * arr, size_t dim );
198
199forall( otype E | { int ?<?( E, E ); } )
200void qsort( E * arr, size_t dim );
201
202//---------------------------------------
203
204[ int, int ] div( int num, int denom );
205[ long int, long int ] div( long int num, long int denom );
206[ long long int, long long int ] div( long long int num, long long int denom );
207forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
208[ T, T ] div( T num, T demon );
209
210//---------------------------------------
211
212static inline unsigned char abs( signed char v ) { return abs( (int)v ); }
213extern "C" { int abs( int ); }                                                  // use default C routine for int
214static inline unsigned long int abs( long int v ) { return labs( v ); }
215static inline unsigned long long int abs( long long int v ) { return llabs( v ); }
216
217extern "C" {
218double fabs( double );
219float fabsf( float );
220long double fabsl( long double );
221} // extern "C"
222static inline float abs( float x ) { return fabsf( x ); }
223static inline double abs( double x ) { return fabs( x ); }
224static inline long double abs( long double x ) { return fabsl( x ); }
225
226extern "C" {
227double cabs( double _Complex );
228float cabsf( float _Complex );
229long double cabsl( long double _Complex );
230} // extern "C"
231static inline float abs( float _Complex x ) { return cabsf( x ); }
232static inline double abs( double _Complex x ) { return cabs( x ); }
233static inline long double abs( long double _Complex x ) { return cabsl( x ); }
234
235forall( otype T | { void ?{}( T &, zero_t ); int ?<?( T, T ); T -?( T ); } )
236T abs( T );
237
238//---------------------------------------
239
240void random_seed( long int s );
241char random( void );
242char random( char u );
243char random( char l, char u );
244int random( void );
245int random( int u );
246int random( int l, int u );
247unsigned int random( void );
248unsigned int random( unsigned int u );
249unsigned int random( unsigned int l, unsigned int u );
250extern "C" { long int random( void ); }
251long int random( long int u );
252long int random( long int l, long int u );
253unsigned long int random( void );
254unsigned long int random( unsigned long int u );
255unsigned long int random( unsigned long int l, unsigned long int u );
256float random( void );
257double random( void );
258float _Complex random( void );
259double _Complex random( void );
260long double _Complex random( void );
261
262//---------------------------------------
263
264forall( otype T | { int ?<?( T, T ); } )
265static inline T min( T t1, T t2 ) { return t1 < t2 ? t1 : t2; }
266
267forall( otype T | { int ?>?( T, T ); } )
268static inline T max( T t1, T t2 ) { return t1 > t2 ? t1 : t2; }
269
270forall( otype T | { T min( T, T ); T max( T, T ); } )
271static inline T clamp( T value, T min_val, T max_val ) { return max( min_val, min( value, max_val ) ); }
272
273forall( otype T )
274static inline void swap( T & v1, T & v2 ) { T temp = v1; v1 = v2; v2 = temp; }
275
276// Local Variables: //
277// mode: c //
278// tab-width: 4 //
279// End: //
Note: See TracBrowser for help on using the repository browser.