add `s_strtol()`
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
diff --git a/etc/tune.c b/etc/tune.c
index 3f02502..b3019c3 100644
--- a/etc/tune.c
+++ b/etc/tune.c
@@ -193,6 +193,25 @@ static void s_run(const char *name, uint64_t (*op)(int), int *cutoff)
*cutoff = x - s_stabilization_extra * args.increment_print;
}
+static long s_strtol(const char *str, char **endptr, const char *err)
+{
+ const int base = 10;
+ char *_endptr;
+ long val;
+ errno = 0;
+ val = strtol(str, &_endptr, base);
+ if ((val > INT_MAX || val < 0) || (errno != 0)) {
+ fprintf(stderr, "Value %s not usable\n", str);
+ exit(EXIT_FAILURE);
+ }
+ if (_endptr == str) {
+ fprintf(stderr, "%s\n", err);
+ exit(EXIT_FAILURE);
+ }
+ if (endptr) *endptr = _endptr;
+ return val;
+}
+
static void s_usage(char *s)
{
fprintf(stderr,"Usage: %s [TvcpGbtrSLFfMmosh]\n",s);
@@ -235,9 +254,7 @@ int main(int argc, char **argv)
int printpreset = 0;
/*int preset[8];*/
- int base = 10;
char *endptr, *str;
- long val;
uint64_t seed = 0xdeadbeef;
@@ -326,71 +343,28 @@ int main(int argc, char **argv)
}
str = argv[opt];
errno = 0;
- val = strtol(str, &endptr, base);
- if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
- || (errno != 0 && val == 0)) {
- fprintf(stderr,"Seed %s not usable\n", argv[opt]);
- exit(EXIT_FAILURE);
- }
- if (endptr == str) {
- fprintf(stderr, "No seed given?\n");
- exit(EXIT_FAILURE);
- }
- seed = (uint64_t)val;
+ seed = (uint64_t)s_strtol(argv[opt], NULL, "No seed given?\n");
break;
case 'L':
opt++;
if (opt >= argc) {
s_usage(argv[0]);
}
- str = argv[opt];
- errno = 0;
- val = strtol(str, &endptr, base);
- if ((val > INT_MAX || val < 0) || (errno != 0)) {
- fprintf(stderr,"Value %s not usable\n", argv[opt]);
- exit(EXIT_FAILURE);
- }
- if (endptr == str) {
- fprintf(stderr, "No value for option \"-L\"given\n");
- exit(EXIT_FAILURE);
- }
- s_stabilization_extra = (int)val;
+ s_stabilization_extra = (int)s_strtol(argv[opt], NULL, "No value for option \"-L\"given");
break;
case 'o':
opt++;
if (opt >= argc) {
s_usage(argv[0]);
}
- str = argv[opt];
- errno = 0;
- val = strtol(str, &endptr, base);
- if ((val > INT_MAX || val < 0) || (errno != 0)) {
- fprintf(stderr,"Value %s not usable as an offset\n", argv[opt]);
- exit(EXIT_FAILURE);
- }
- if (endptr == str) {
- fprintf(stderr, "No value for the offset given\n");
- exit(EXIT_FAILURE);
- }
- s_offset = (int)val;
+ s_offset = (int)s_strtol(argv[opt], NULL, "No value for the offset given");
break;
case 'r':
opt++;
if (opt >= argc) {
s_usage(argv[0]);
}
- str = argv[opt];
- errno = 0;
- val = strtol(str, &endptr, base);
- if ((val > INT_MAX || val < 0) || (errno != 0)) {
- fprintf(stderr,"Value %s not usable as the number of rounds for \"-r\"\n", argv[opt]);
- exit(EXIT_FAILURE);
- }
- if (endptr == str) {
- fprintf(stderr, "No value for the number of rounds given\n");
- exit(EXIT_FAILURE);
- }
- s_number_of_test_loops = (int)val;
+ s_number_of_test_loops = (int)s_strtol(argv[opt], NULL, "No value for the number of rounds given");
break;
case 'M':
@@ -398,36 +372,14 @@ int main(int argc, char **argv)
if (opt >= argc) {
s_usage(argv[0]);
}
- str = argv[opt];
- errno = 0;
- val = strtol(str, &endptr, base);
- if ((val > INT_MAX || val < 0) || (errno != 0)) {
- fprintf(stderr,"Value %s not usable as the upper limit of T-C tests (\"-M\")\n", argv[opt]);
- exit(EXIT_FAILURE);
- }
- if (endptr == str) {
- fprintf(stderr, "No value for the upper limit of T-C tests given\n");
- exit(EXIT_FAILURE);
- }
- args.upper_limit_print = (int)val;
+ args.upper_limit_print = (int)s_strtol(argv[opt], NULL, "No value for the upper limit of T-C tests given");
break;
case 'm':
opt++;
if (opt >= argc) {
s_usage(argv[0]);
}
- str = argv[opt];
- errno = 0;
- val = strtol(str, &endptr, base);
- if ((val > INT_MAX || val < 0) || (errno != 0)) {
- fprintf(stderr,"Value %s not usable as the increment for the T-C tests (\"-m\")\n", argv[opt]);
- exit(EXIT_FAILURE);
- }
- if (endptr == str) {
- fprintf(stderr, "No value for the increment for the T-C tests given\n");
- exit(EXIT_FAILURE);
- }
- args.increment_print = (int)val;
+ args.increment_print = (int)s_strtol(argv[opt], NULL, "No value for the increment for the T-C tests given");
break;
case 's':
printpreset = 1;
@@ -437,60 +389,13 @@ int main(int argc, char **argv)
s_usage(argv[0]);
}
str = argv[opt];
- i = 0;
- /* Only the most basic checks */
- errno = 0;
- val = strtol(str, &endptr, base);
- if ((val > INT_MAX || val < 0) || (errno != 0)) {
- fprintf(stderr,"input #%d wrong\n", i+1);
- exit(EXIT_FAILURE);
- }
- if (endptr == str) {
- fprintf(stderr, "No input for #%d?\n", i+1);
- exit(EXIT_FAILURE);
- }
- i++;
+ KARATSUBA_MUL_CUTOFF = (int)s_strtol(str, &endptr, "[1/4] No value for KARATSUBA_MUL_CUTOFF given");
str = endptr + 1;
- KARATSUBA_MUL_CUTOFF = (int)val;
- errno = 0;
- val = strtol(str, &endptr, base);
- if ((val > INT_MAX || val < 0) || (errno != 0)) {
- fprintf(stderr,"input #%d wrong\n", i+1);
- exit(EXIT_FAILURE);
- }
- if (endptr == str) {
- fprintf(stderr, "No input for #%d?\n", i+1);
- exit(EXIT_FAILURE);
- }
- i++;
- str = endptr + 1;
- KARATSUBA_SQR_CUTOFF = (int)val;
- errno = 0;
- val = strtol(str, &endptr, base);
- if ((val > INT_MAX || val < 0) || (errno != 0)) {
- fprintf(stderr,"input #%d wrong\n", i+1);
- exit(EXIT_FAILURE);
- }
- if (endptr == str) {
- fprintf(stderr, "No input for #%d?\n", i+1);
- exit(EXIT_FAILURE);
- }
- i++;
+ KARATSUBA_SQR_CUTOFF = (int)s_strtol(str, &endptr, "[2/4] No value for KARATSUBA_SQR_CUTOFF given");
str = endptr + 1;
- TOOM_MUL_CUTOFF = (int)val;
- errno = 0;
- val = strtol(str, &endptr, base);
- if ((val > INT_MAX || val < 0) || (errno != 0)) {
- fprintf(stderr,"input #%d wrong\n", i+1);
- exit(EXIT_FAILURE);
- }
- if (endptr == str) {
- fprintf(stderr, "No input for #%d?\n", i+1);
- exit(EXIT_FAILURE);
- }
- i++;
+ TOOM_MUL_CUTOFF = (int)s_strtol(str, &endptr, "[3/4] No value for TOOM_MUL_CUTOFF given");
str = endptr + 1;
- TOOM_SQR_CUTOFF = (int)val;
+ TOOM_SQR_CUTOFF = (int)s_strtol(str, &endptr, "[4/4] No value for TOOM_SQR_CUTOFF given");
break;
case 'h':
default: