[0fc52b6] | 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 | // common -- |
---|
| 8 | // |
---|
| 9 | // Author : Peter A. Buhr |
---|
| 10 | // Created On : Wed Jul 11 17:54:36 2018 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[4026d1be] | 12 | // Last Modified On : Wed May 5 14:02:04 2021 |
---|
| 13 | // Update Count : 18 |
---|
[0fc52b6] | 14 | // |
---|
| 15 | |
---|
| 16 | #pragma once |
---|
| 17 | |
---|
| 18 | //--------------------------------------- |
---|
| 19 | |
---|
| 20 | [ int, int ] div( int num, int denom ); |
---|
| 21 | [ long int, long int ] div( long int num, long int denom ); |
---|
| 22 | [ long long int, long long int ] div( long long int num, long long int denom ); |
---|
[fd54fef] | 23 | forall( T | { T ?/?( T, T ); T ?%?( T, T ); } ) |
---|
[0fc52b6] | 24 | [ T, T ] div( T num, T demon ); |
---|
| 25 | |
---|
| 26 | //--------------------------------------- |
---|
| 27 | |
---|
| 28 | extern "C" { |
---|
| 29 | int abs( int ); // stdlib.h |
---|
| 30 | long int labs( long int ); |
---|
| 31 | long long int llabs( long long int ); |
---|
| 32 | } // extern "C" |
---|
| 33 | |
---|
| 34 | static inline { |
---|
| 35 | unsigned char abs( signed char v ) { return abs( (int)v ); } |
---|
| 36 | // use default C routine for int |
---|
| 37 | unsigned long int abs( long int v ) { return labs( v ); } |
---|
| 38 | unsigned long long int abs( long long int v ) { return llabs( v ); } |
---|
| 39 | } // distribution |
---|
| 40 | |
---|
| 41 | extern "C" { |
---|
| 42 | double fabs( double ); // math.h |
---|
| 43 | float fabsf( float ); |
---|
| 44 | long double fabsl( long double ); |
---|
| 45 | } // extern "C" |
---|
| 46 | static inline { |
---|
| 47 | float abs( float x ) { return fabsf( x ); } |
---|
| 48 | double abs( double x ) { return fabs( x ); } |
---|
| 49 | long double abs( long double x ) { return fabsl( x ); } |
---|
| 50 | } // distribution |
---|
| 51 | |
---|
| 52 | extern "C" { |
---|
| 53 | double cabs( double _Complex ); // complex.h |
---|
| 54 | float cabsf( float _Complex ); |
---|
| 55 | long double cabsl( long double _Complex ); |
---|
| 56 | } // extern "C" |
---|
| 57 | static inline { |
---|
| 58 | float abs( float _Complex x ) { return cabsf( x ); } |
---|
| 59 | double abs( double _Complex x ) { return cabs( x ); } |
---|
| 60 | long double abs( long double _Complex x ) { return cabsl( x ); } |
---|
| 61 | } // distribution |
---|
| 62 | |
---|
[fd54fef] | 63 | forall( T | { void ?{}( T &, zero_t ); int ?<?( T, T ); T -?( T ); } ) |
---|
[0fc52b6] | 64 | T abs( T ); |
---|
| 65 | |
---|
| 66 | //--------------------------------------- |
---|
| 67 | |
---|
| 68 | static inline { |
---|
[4026d1be] | 69 | char min( char v1, char v2 ) { return v1 < v2 ? v1 : v2; } // optimization |
---|
| 70 | int min( int v1, int v2 ) { return v1 < v2 ? v1 : v2; } |
---|
| 71 | unsigned int min( unsigned int v1, unsigned int v2 ) { return v1 < v2 ? v1 : v2; } |
---|
| 72 | long int min( long int v1, long int v2 ) { return v1 < v2 ? v1 : v2; } |
---|
| 73 | unsigned long int min( unsigned long int v1, unsigned int v2 ) { return v1 < v2 ? v1 : v2; } |
---|
| 74 | long long int min( long long int v1, long long int v2 ) { return v1 < v2 ? v1 : v2; } |
---|
| 75 | unsigned long long int min( unsigned long long int v1, unsigned int v2 ) { return v1 < v2 ? v1 : v2; } |
---|
[fd54fef] | 76 | forall( T | { int ?<?( T, T ); } ) |
---|
[4026d1be] | 77 | T min( T v1, T v2 ) { return v1 < v2 ? v1 : v2; } |
---|
[0fc52b6] | 78 | |
---|
[4026d1be] | 79 | char max( char v1, char v2 ) { return v1 > v2 ? v1 : v2; } // optimization |
---|
| 80 | int max( int v1, int v2 ) { return v1 > v2 ? v1 : v2; } |
---|
| 81 | unsigned int max( unsigned int v1, unsigned int v2 ) { return v1 > v2 ? v1 : v2; } |
---|
| 82 | long int max( long int v1, long int v2 ) { return v1 > v2 ? v1 : v2; } |
---|
| 83 | unsigned long int max( unsigned long int v1, unsigned long int v2 ) { return v1 > v2 ? v1 : v2; } |
---|
| 84 | long long int max( long long int v1, long long int v2 ) { return v1 > v2 ? v1 : v2; } |
---|
| 85 | unsigned long long int max( unsigned long long int v1, unsigned long long int v2 ) { return v1 > v2 ? v1 : v2; } |
---|
[fd54fef] | 86 | forall( T | { int ?>?( T, T ); } ) |
---|
[4026d1be] | 87 | T max( T v1, T v2 ) { return v1 > v2 ? v1 : v2; } |
---|
[0fc52b6] | 88 | |
---|
[fd54fef] | 89 | forall( T | { T min( T, T ); T max( T, T ); } ) |
---|
[0fc52b6] | 90 | T clamp( T value, T min_val, T max_val ) { return max( min_val, min( value, max_val ) ); } |
---|
| 91 | |
---|
[fd54fef] | 92 | forall( T ) |
---|
[0fc52b6] | 93 | void swap( T & v1, T & v2 ) { T temp = v1; v1 = v2; v2 = temp; } |
---|
| 94 | } // distribution |
---|
| 95 | |
---|
| 96 | // Local Variables: // |
---|
| 97 | // mode: c // |
---|
| 98 | // tab-width: 4 // |
---|
| 99 | // End: // |
---|