[91a72ef] | 1 | // |
---|
[0fc52b6] | 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. |
---|
[91a72ef] | 6 | // |
---|
| 7 | // common.hfa -- |
---|
| 8 | // |
---|
[0fc52b6] | 9 | // Author : Peter A. Buhr |
---|
| 10 | // Created On : Wed Jul 11 17:54:36 2018 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[4b75ae9] | 12 | // Last Modified On : Sat Aug 24 08:01:36 2024 |
---|
| 13 | // Update Count : 43 |
---|
[91a72ef] | 14 | // |
---|
[0fc52b6] | 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" |
---|
[95bda0a] | 33 | static inline __attribute__((always_inline)) { |
---|
[24d6572] | 34 | unsigned char abs( signed char v ) { return (int)abs( (int)v ); } |
---|
[0fc52b6] | 35 | // use default C routine for int |
---|
| 36 | unsigned long int abs( long int v ) { return labs( v ); } |
---|
| 37 | unsigned long long int abs( long long int v ) { return llabs( v ); } |
---|
| 38 | } // distribution |
---|
| 39 | |
---|
| 40 | extern "C" { |
---|
| 41 | double fabs( double ); // math.h |
---|
| 42 | float fabsf( float ); |
---|
| 43 | long double fabsl( long double ); |
---|
| 44 | } // extern "C" |
---|
[95bda0a] | 45 | static inline __attribute__((always_inline)) { |
---|
[0fc52b6] | 46 | float abs( float x ) { return fabsf( x ); } |
---|
| 47 | double abs( double x ) { return fabs( x ); } |
---|
| 48 | long double abs( long double x ) { return fabsl( x ); } |
---|
| 49 | } // distribution |
---|
| 50 | |
---|
| 51 | extern "C" { |
---|
| 52 | double cabs( double _Complex ); // complex.h |
---|
| 53 | float cabsf( float _Complex ); |
---|
| 54 | long double cabsl( long double _Complex ); |
---|
| 55 | } // extern "C" |
---|
[95bda0a] | 56 | static inline __attribute__((always_inline)) { |
---|
[0fc52b6] | 57 | float abs( float _Complex x ) { return cabsf( x ); } |
---|
| 58 | double abs( double _Complex x ) { return cabs( x ); } |
---|
| 59 | long double abs( long double _Complex x ) { return cabsl( x ); } |
---|
| 60 | } // distribution |
---|
| 61 | |
---|
[fd54fef] | 62 | forall( T | { void ?{}( T &, zero_t ); int ?<?( T, T ); T -?( T ); } ) |
---|
[0fc52b6] | 63 | T abs( T ); |
---|
| 64 | |
---|
| 65 | //--------------------------------------- |
---|
| 66 | |
---|
[95bda0a] | 67 | static inline __attribute__((always_inline)) { |
---|
[d3b8752] | 68 | // Specializations |
---|
| 69 | char min( char v1, char v2 ) { return v1 < v2 ? v1 : v2; } |
---|
[4026d1be] | 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; } |
---|
[24d6572] | 73 | unsigned long int min( unsigned long int v1, unsigned long int v2 ) { return v1 < v2 ? v1 : v2; } |
---|
[4026d1be] | 74 | long long int min( long long int v1, long long int v2 ) { return v1 < v2 ? v1 : v2; } |
---|
[24d6572] | 75 | unsigned long long int min( unsigned long long int v1, unsigned long long int v2 ) { return v1 < v2 ? v1 : v2; } |
---|
[0fc52b6] | 76 | |
---|
[d3b8752] | 77 | char max( char v1, char v2 ) { return v1 > v2 ? v1 : v2; } |
---|
[4026d1be] | 78 | int max( int v1, int v2 ) { return v1 > v2 ? v1 : v2; } |
---|
| 79 | unsigned int max( unsigned int v1, unsigned int v2 ) { return v1 > v2 ? v1 : v2; } |
---|
| 80 | long int max( long int v1, long int v2 ) { return v1 > v2 ? v1 : v2; } |
---|
| 81 | unsigned long int max( unsigned long int v1, unsigned long int v2 ) { return v1 > v2 ? v1 : v2; } |
---|
| 82 | long long int max( long long int v1, long long int v2 ) { return v1 > v2 ? v1 : v2; } |
---|
| 83 | unsigned long long int max( unsigned long long int v1, unsigned long long int v2 ) { return v1 > v2 ? v1 : v2; } |
---|
[4b75ae9] | 84 | |
---|
| 85 | // Polymorphic |
---|
| 86 | forall( T | { int ?<?( T, T ); } ) |
---|
| 87 | T min( T v1, T v2 ) { return v1 < v2 ? v1 : v2; } |
---|
| 88 | |
---|
| 89 | forall( T, Ts ... | { T min( T, T ); T min( Ts ); } ) |
---|
| 90 | T min( T arg, Ts args ) { return min( arg, min( args ) ); } |
---|
| 91 | |
---|
| 92 | forall( T | { int ?>?( T, T ); } ) |
---|
| 93 | T max( T v1, T v2 ) { return v1 > v2 ? v1 : v2; } |
---|
| 94 | |
---|
| 95 | forall( T, Ts ... | { T max( T, T ); T max( Ts ); } ) |
---|
| 96 | T max( T arg, Ts args ) { return max( arg, max( args ) ); } |
---|
| 97 | |
---|
| 98 | forall( T | { T min( T, T ); T max( T, T ); } ) |
---|
| 99 | T clamp( T value, T min_val, T max_val ) { return max( min_val, min( value, max_val ) ); } |
---|
| 100 | |
---|
| 101 | forall( T ) |
---|
| 102 | void swap( T & v1, T & v2 ) { T temp = v1; v1 = v2; v2 = temp; } |
---|
[0fc52b6] | 103 | } // distribution |
---|
| 104 | |
---|
| 105 | // Local Variables: // |
---|
| 106 | // mode: c // |
---|
| 107 | // tab-width: 4 // |
---|
| 108 | // End: // |
---|