[d3b7937] | 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 | // algorithm.c -- |
---|
| 8 | // |
---|
| 9 | // Author : Peter A. Buhr |
---|
| 10 | // Created On : Thu Jan 28 17:10:29 2016 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[5721a6d] | 12 | // Last Modified On : Mon Feb 1 13:42:05 2016 |
---|
| 13 | // Update Count : 52 |
---|
[d3b7937] | 14 | // |
---|
| 15 | |
---|
| 16 | #include "algorithm" |
---|
| 17 | |
---|
| 18 | forall( type T | { int ?<?( T, T ); } ) |
---|
| 19 | T min( const T t1, const T t2 ) { |
---|
| 20 | return t1 < t2 ? t1 : t2; |
---|
| 21 | } // min |
---|
| 22 | |
---|
| 23 | forall( type T | { int ?>?( T, T ); } ) |
---|
| 24 | T max( const T t1, const T t2 ) { |
---|
| 25 | return t1 > t2 ? t1 : t2; |
---|
| 26 | } // max |
---|
| 27 | |
---|
[5721a6d] | 28 | //--------------------------------------- |
---|
| 29 | |
---|
| 30 | forall( type T ) |
---|
| 31 | void swap( T * t1, T * t2 ) { |
---|
| 32 | T temp = *t1; |
---|
| 33 | *t1 = *t2; |
---|
| 34 | *t2 = temp; |
---|
| 35 | } // swap |
---|
| 36 | |
---|
| 37 | //--------------------------------------- |
---|
[d3b7937] | 38 | |
---|
| 39 | extern "C" { |
---|
[5721a6d] | 40 | #define _XOPEN_SOURCE // required to access "rand48" routines |
---|
[d3b7937] | 41 | #include <stdlib.h> // abs, labs, llabs |
---|
| 42 | #include <math.h> // fabsf, fabs, fabsl |
---|
| 43 | #include <complex.h> // cabsf, cabs, cabsl |
---|
[5721a6d] | 44 | #undef I // free name |
---|
[d3b7937] | 45 | } // extern |
---|
| 46 | |
---|
| 47 | char abs( char v ) { return abs( (int)v ); } |
---|
| 48 | long int abs( long int v ) { return labs( v ); } |
---|
| 49 | long long int abs( long long int v ) { return llabs( v ); } |
---|
| 50 | float abs( float v ) { return fabsf( v ); } |
---|
| 51 | double abs( double v ) { return fabs( v ); } |
---|
| 52 | long double abs( long double v ) { return fabsl( v ); } |
---|
| 53 | float _Complex abs( float _Complex v ) { return cabsf( v ); } |
---|
| 54 | double _Complex abs( double _Complex v ) { return cabs( v ); } |
---|
| 55 | long double _Complex abs( long double _Complex v ) { return cabsl( v ); } |
---|
| 56 | |
---|
[5721a6d] | 57 | //--------------------------------------- |
---|
| 58 | |
---|
| 59 | void randseed( long int s ) { srand48( s ); } |
---|
| 60 | char random() { return lrand48(); } |
---|
| 61 | int random() { return mrand48(); } |
---|
| 62 | unsigned int random() { return lrand48(); } |
---|
| 63 | long int random() { return mrand48(); } |
---|
| 64 | unsigned long int random() { return lrand48(); } |
---|
| 65 | float random() { return (float)drand48(); } // otherwise float uses lrand48 |
---|
| 66 | double random() { return drand48(); } |
---|
| 67 | float _Complex random() { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); } |
---|
| 68 | double _Complex random() { return drand48() + (double _Complex)(drand48() * _Complex_I); } |
---|
| 69 | long double _Complex random() { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); } |
---|
[d3b7937] | 70 | |
---|
| 71 | // Local Variables: // |
---|
| 72 | // tab-width: 4 // |
---|
| 73 | // End: // |
---|