-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathparam.h
132 lines (105 loc) · 5.78 KB
/
param.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* Inline routines for testing of input parameters
*
* Copyright (C) ADDA contributors
* This file is part of ADDA.
*
* ADDA is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* ADDA is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with ADDA. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef __param_h
#define __param_h
// project headers
#include "function.h" // needed for function attributes
// system headers
#include <limits.h> // for INT_MIN and INT_MAX
#include <math.h>
typedef struct {
int l1; // first level index
int l2; // second level index
} opt_index;
extern opt_index opt; // defined in param.c
void PrintErrorHelp(const char * restrict fmt, ... ) ATT_PRINTF(1,2) ATT_NORETURN;
//======================================================================================================================
static inline void TestPositive(const double val,const char * restrict name)
// check if val is positive, otherwise produces error message
{
if (val<=0) PrintErrorHelp("Illegal %s ("GFORMDEF"), must be positive",name,val);
}
//======================================================================================================================
static inline void TestNonNegative(const double val,const char * restrict name)
// check if val is nonnegative, otherwise produces error message
{
if (val<0) PrintErrorHelp("Illegal %s ("GFORMDEF"), must be nonnegative",name,val);
}
//======================================================================================================================
static inline void TestPositive_i(const int val,const char * restrict name)
// check if val (int) is positive, otherwise produces error message
{
if (val<=0) PrintErrorHelp("Illegal %s (%d), must be positive",name,val);
}
//======================================================================================================================
static inline void TestNonNegative_i(const int val,const char * restrict name)
// check if val (int) is nonnegative, otherwise produces error message
{
if (val<0) PrintErrorHelp("Illegal %s (%d), must be nonnegative",name,val);
}
//======================================================================================================================
/* In following 4 functions, one of two letters means either Including or Not-including (left and right point of the
* interval respectively)
*/
static inline void TestRangeII(const double val,const char * restrict name,const double min,const double max)
// check if val is in interval [min,max], otherwise produces error message
{
if (val<min || val>max)
PrintErrorHelp("Illegal %s ("GFORMDEF"), must belong to the interval ["GFORMDEF","GFORMDEF"]",name,val,min,max);
}
//======================================================================================================================
static inline void TestRangeNI(const double val,const char * restrict name,const double min,const double max)
// checks if val is in interval (min,max], otherwise produces error message
{
if (val<=min || val>max)
PrintErrorHelp("Illegal %s ("GFORMDEF"), must belong to the interval ("GFORMDEF","GFORMDEF"]",name,val,min,max);
}
//======================================================================================================================
static inline void TestRangeIN(const double val,const char * restrict name,const double min,const double max)
// checks if val is in interval [min,max), otherwise produces error message
{
if (val<min || val>=max)
PrintErrorHelp("Illegal %s ("GFORMDEF"), must belong to the interval ["GFORMDEF","GFORMDEF")",name,val,min,max);
}
//======================================================================================================================
static inline void TestRangeNN(const double val,const char * restrict name,const double min,const double max)
// checks if val is in interval (min,max), otherwise produces error message
{
if (val<=min || val>=max)
PrintErrorHelp("Illegal %s ("GFORMDEF"), must belong to the interval ("GFORMDEF","GFORMDEF")",name,val,min,max);
}
//======================================================================================================================
static inline void ConvertToInteger(double val,const char * restrict name,int *res)
/* converts val to res, but first checks if val is really an integer and in the bounds, otherwise produces an error
* message
*/
{
if (val != floor(val)) PrintErrorHelp("Illegal %s ("GFORMDEF"), must be an integer",name,val);
if (val<INT_MIN || val>INT_MAX) PrintErrorHelp("Illegal %s ("GFORMDEF"), must be inside integer bounds",name,val);
*res=(int)val;
}
//======================================================================================================================
static inline void TestRange_i(const int val,const char * restrict name,const int min,const int max)
// checks if val (int) is in interval [min,max], otherwise produces error message
{
if (val<min || val>max) PrintErrorHelp("Illegal %s (%d), must belong to the interval [%d,%d]",name,val,min,max);
}
//======================================================================================================================
static inline void TestGreaterThan_i(const int val,const char * restrict name,const int min)
// checks if val (int) is greater than min, otherwise produces error message
{
if (val<=min) PrintErrorHelp("Illegal %s (%d), must be greater than %d",name,val,min);
}
#endif // __param_h