source: src/libcfa/stdlib.c@ d9ff69a

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since d9ff69a was e672372, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

more inline code in stdlib and update tests

  • Property mode set to 100644
File size: 7.4 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//
[43385ca]7// algorithm.c --
[bd85400]8//
9// Author : Peter A. Buhr
10// Created On : Thu Jan 28 17:10:29 2016
11// Last Modified By : Peter A. Buhr
[e672372]12// Last Modified On : Sun Dec 24 13:00:15 2017
13// Update Count : 344
[bd85400]14//
15
16#include "stdlib"
17
18//---------------------------------------
19
20#define _XOPEN_SOURCE 600 // posix_memalign, *rand48
[f3fc631f]21#include <string.h> // memcpy, memset
[bd85400]22#include <malloc.h> // malloc_usable_size
23#include <math.h> // fabsf, fabs, fabsl
[6e991d6]24#include <complex.h> // _Complex_I
[e672372]25#include <assert.h>
[bd85400]26
[f3fc631f]27// resize, non-array types
[6065b3aa]28forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ) {
[f3fc631f]29 size_t olen = malloc_usable_size( ptr ); // current allocation
[6065b3aa]30 char * nptr = (void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
[f3fc631f]31 size_t nlen = malloc_usable_size( nptr ); // new allocation
32 if ( nlen > olen ) { // larger ?
33 memset( nptr + olen, (int)fill, nlen - olen ); // initialize added storage
[aca65621]34 } //
[f3fc631f]35 return (T *)nptr;
[6065b3aa]36} // alloc
[f3ddc21]37
[6065b3aa]38// allocation/deallocation and constructor/destructor, non-array types
[aca65621]39forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
[627f585]40T * new( Params p ) {
[a493682]41 return &(*malloc()){ p }; // run constructor
[f3ddc21]42} // new
[627f585]43
[83a071f9]44forall( dtype T | sized(T) | { void ^?{}( T & ); } )
[627f585]45void delete( T * ptr ) {
[6065b3aa]46 if ( ptr ) { // ignore null
[83a071f9]47 ^(*ptr){}; // run destructor
[f3ddc21]48 free( ptr );
[f3fc631f]49 } // if
[f3ddc21]50} // delete
[627f585]51
[83a071f9]52forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } )
[bf76eab]53void delete( T * ptr, Params rest ) {
[6065b3aa]54 if ( ptr ) { // ignore null
[83a071f9]55 ^(*ptr){}; // run destructor
[bf76eab]56 free( ptr );
[f3fc631f]57 } // if
[bf76eab]58 delete( rest );
[f3ddc21]59} // delete
[bf76eab]60
[6065b3aa]61
62// allocation/deallocation and constructor/destructor, array types
[aca65621]63forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
[6065b3aa]64T * anew( size_t dim, Params p ) {
65 T *arr = alloc( dim );
66 for ( unsigned int i = 0; i < dim; i += 1 ) {
[a493682]67 (arr[i]){ p }; // run constructor
[6065b3aa]68 } // for
69 return arr;
70} // anew
71
[aca65621]72forall( dtype T | sized(T) | { void ^?{}( T & ); } )
[6065b3aa]73void adelete( size_t dim, T arr[] ) {
74 if ( arr ) { // ignore null
75 for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned
[a493682]76 ^(arr[i]){}; // run destructor
[6065b3aa]77 } // for
78 free( arr );
79 } // if
80} // adelete
81
[aca65621]82forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } )
[6065b3aa]83void adelete( size_t dim, T arr[], Params rest ) {
84 if ( arr ) { // ignore null
85 for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned
[a493682]86 ^(arr[i]){}; // run destructor
[6065b3aa]87 } // for
88 free( arr );
89 } // if
90 adelete( rest );
91} // adelete
92
[bd85400]93//---------------------------------------
94
95float _Complex strto( const char * sptr, char ** eptr ) {
96 float re, im;
[e672372]97 char * eeptr;
98 re = strtof( sptr, &eeptr );
99 if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
100 im = strtof( eeptr, &eeptr );
101 if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
102 if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
[bd85400]103 return re + im * _Complex_I;
[f3ddc21]104} // strto
105
[bd85400]106double _Complex strto( const char * sptr, char ** eptr ) {
107 double re, im;
[e672372]108 char * eeptr;
109 re = strtod( sptr, &eeptr );
110 if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
111 im = strtod( eeptr, &eeptr );
112 if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
113 if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
[bd85400]114 return re + im * _Complex_I;
[f3ddc21]115} // strto
116
[bd85400]117long double _Complex strto( const char * sptr, char ** eptr ) {
118 long double re, im;
[e672372]119 char * eeptr;
120 re = strtold( sptr, &eeptr );
121 if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
122 im = strtold( eeptr, &eeptr );
123 if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
124 if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
[bd85400]125 return re + im * _Complex_I;
[f3ddc21]126} // strto
[bd85400]127
128//---------------------------------------
129
[4040425]130forall( otype T | { int ?<?( T, T ); } )
[f3fc631f]131T * bsearch( T key, const T * arr, size_t dim ) {
[bd85400]132 int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
[f3fc631f]133 return (T *)bsearch( &key, arr, dim, sizeof(T), comp );
[bd85400]134} // bsearch
135
[707446a]136forall( otype T | { int ?<?( T, T ); } )
[f3fc631f]137unsigned int bsearch( T key, const T * arr, size_t dim ) {
[e672372]138 T * result = bsearch( key, arr, dim );
[f3fc631f]139 return result ? result - arr : dim; // pointer subtraction includes sizeof(T)
[707446a]140} // bsearch
141
[4040425]142forall( otype T | { int ?<?( T, T ); } )
[f3fc631f]143void qsort( const T * arr, size_t dim ) {
[bd85400]144 int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
[f3fc631f]145 qsort( arr, dim, sizeof(T), comp );
[bd85400]146} // qsort
147
148//---------------------------------------
149
[5ea26ed]150[ int, int ] div( int num, int denom ) { div_t qr = div( num, denom ); return [ qr.quot, qr.rem ]; }
151[ long int, long int ] div( long int num, long int denom ) { ldiv_t qr = ldiv( num, denom ); return [ qr.quot, qr.rem ]; }
152[ long long int, long long int ] div( long long int num, long long int denom ) { lldiv_t qr = lldiv( num, denom ); return [ qr.quot, qr.rem ]; }
[43385ca]153forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
[5ea26ed]154[ T, T ] div( T num, T denom ) { return [ num / denom, num % denom ]; }
[bd85400]155
156//---------------------------------------
157
[e672372]158void random_seed( long int s ) { srand48( s ); srandom( s ); } // call srandom to harmonize with C-lib random
159char random( void ) { return (unsigned long int)random(); }
160char random( char u ) { return random( (unsigned long int)u ); }
161char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); }
162int random( void ) { return (long int)random(); }
163int random( int u ) { return random( (long int)u ); }
164int random( int l, int u ) { return random( (long int)l, (long int)u ); }
165unsigned int random( void ) { return (unsigned long int)random(); }
166unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); }
167unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); }
168//extern "C" { long int random() { return mrand48(); } }
169long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); }
170long int random( long int l, long int u ) { assert( l < u ); return lrand48() % (u - l) + l; }
[70e4895d]171unsigned long int random( void ) { return lrand48(); }
172unsigned long int random( unsigned long int u ) { return lrand48() % u; }
[e672372]173unsigned long int random( unsigned long int l, unsigned long int u ) { assert( l < u ); return lrand48() % (u - l) + l; }
174float random( void ) { return (float)drand48(); } // cast otherwise float uses lrand48
[70e4895d]175double random( void ) { return drand48(); }
176float _Complex random( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
177double _Complex random( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); }
[e672372]178long double _Complex random( void ) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
[a9f2c13]179
[bd85400]180
181// Local Variables: //
182// tab-width: 4 //
183// End: //
Note: See TracBrowser for help on using the repository browser.