Changeset 84876aa4 in mainline for uspace/lib/c/generic/capa.c
- Timestamp:
- 2019-11-15T13:46:34Z (4 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- ecb7828
- Parents:
- b093a62 (diff), d548fc0 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/capa.c
rb093a62 r84876aa4 34 34 */ 35 35 36 #include <cap .h>36 #include <capa.h> 37 37 #include <errno.h> 38 38 #include <imath.h> … … 43 43 enum { 44 44 /** Simplified capacity maximum integer digits */ 45 scap _max_idig = 3,45 scapa_max_idig = 3, 46 46 /** Simplified capacity maximum significant digits */ 47 scap _max_sdig = 447 scapa_max_sdig = 4 48 48 }; 49 49 … … 60 60 }; 61 61 62 void cap _from_blocks(uint64_t nblocks, size_t block_size, cap_spec_t *cap)62 void capa_from_blocks(uint64_t nblocks, size_t block_size, capa_spec_t *capa) 63 63 { 64 64 uint64_t tsize; 65 65 66 66 tsize = nblocks * block_size; 67 cap ->m = tsize;68 cap ->dp = 0;69 cap ->cunit = cu_byte;67 capa->m = tsize; 68 capa->dp = 0; 69 capa->cunit = cu_byte; 70 70 } 71 71 … … 81 81 * and @c cv_max gives the maximum value. 82 82 */ 83 errno_t cap _to_blocks(cap_spec_t *cap, cap_vsel_t cvsel, size_t block_size,83 errno_t capa_to_blocks(capa_spec_t *capa, capa_vsel_t cvsel, size_t block_size, 84 84 uint64_t *rblocks) 85 85 { … … 92 92 errno_t rc; 93 93 94 exp = cap ->cunit * 3 - cap->dp;94 exp = capa->cunit * 3 - capa->dp; 95 95 if (exp < 0) { 96 96 rc = ipow10_u64(-exp, &f); 97 97 if (rc != EOK) 98 98 return ERANGE; 99 bytes = (cap ->m + (f / 2)) / f;100 if (bytes * f - (f / 2) != cap ->m)99 bytes = (capa->m + (f / 2)) / f; 100 if (bytes * f - (f / 2) != capa->m) 101 101 return ERANGE; 102 102 } else { … … 118 118 } 119 119 120 bytes = cap ->m * f + adj;121 if ((bytes - adj) / f != cap ->m)120 bytes = capa->m * f + adj; 121 if ((bytes - adj) / f != capa->m) 122 122 return ERANGE; 123 123 } … … 138 138 * digits and at most two fractional digits, e.g abc.xy <unit>. 139 139 */ 140 void cap _simplify(cap_spec_t *cap)140 void capa_simplify(capa_spec_t *capa) 141 141 { 142 142 uint64_t div; … … 146 146 errno_t rc; 147 147 148 /* Change units so that we have at most @c scap _max_idig integer digits */149 rc = ipow10_u64(scap _max_idig, &maxv);148 /* Change units so that we have at most @c scapa_max_idig integer digits */ 149 rc = ipow10_u64(scapa_max_idig, &maxv); 150 150 assert(rc == EOK); 151 151 152 rc = ipow10_u64(cap ->dp, &div);152 rc = ipow10_u64(capa->dp, &div); 153 153 assert(rc == EOK); 154 154 155 while (cap->m / div >= maxv) { 156 ++cap->cunit; 157 cap->dp += 3; 155 /* Change units until we have no more than scapa_max_idig integer digits */ 156 while (capa->m / div >= maxv) { 157 ++capa->cunit; 158 capa->dp += 3; 158 159 div = div * 1000; 159 160 } 160 161 161 /* Round the number so that we have at most @c scap _max_sdig significant digits */162 sdig = 1 + ilog10_u64(cap ->m); /* number of significant digits */163 if (sdig > scap _max_sdig) {162 /* Round the number so that we have at most @c scapa_max_sdig significant digits */ 163 sdig = 1 + ilog10_u64(capa->m); /* number of significant digits */ 164 if (sdig > scapa_max_sdig) { 164 165 /* Number of digits to remove */ 165 rdig = sdig - scap _max_sdig;166 if (rdig > cap ->dp)167 rdig = cap ->dp;166 rdig = sdig - scapa_max_sdig; 167 if (rdig > capa->dp) 168 rdig = capa->dp; 168 169 169 170 rc = ipow10_u64(rdig, &div); 170 171 assert(rc == EOK); 171 172 172 cap->m = (cap->m + (div / 2)) / div; 173 cap->dp -= rdig; 174 } 175 } 176 177 errno_t cap_format(cap_spec_t *cap, char **rstr) 173 /* Division with rounding */ 174 capa->m = (capa->m + (div / 2)) / div; 175 capa->dp -= rdig; 176 } 177 178 /* 179 * If we rounded up from something like 999.95 to 1000.0,, we still 180 * have more than scapa_max_idig integer digits and need to change 181 * units once more. 182 */ 183 rc = ipow10_u64(capa->dp, &div); 184 assert(rc == EOK); 185 186 if (capa->m / div >= 1000) { 187 ++capa->cunit; 188 capa->dp += 3; 189 190 /* 191 * We now have one more significant digit than we want 192 * so round to one less digits 193 */ 194 capa->m = (capa->m + 5) / 10; 195 --capa->dp; 196 } 197 } 198 199 errno_t capa_format(capa_spec_t *capa, char **rstr) 178 200 { 179 201 errno_t rc; … … 186 208 sunit = NULL; 187 209 188 assert(cap ->cunit < CU_LIMIT);189 190 rc = ipow10_u64(cap ->dp, &div);210 assert(capa->cunit < CU_LIMIT); 211 212 rc = ipow10_u64(capa->dp, &div); 191 213 if (rc != EOK) 192 214 return rc; 193 215 194 ipart = cap ->m / div;195 fpart = cap ->m % div;196 197 sunit = cu_str[cap ->cunit];198 if (cap ->dp > 0) {216 ipart = capa->m / div; 217 fpart = capa->m % div; 218 219 sunit = cu_str[capa->cunit]; 220 if (capa->dp > 0) { 199 221 ret = asprintf(rstr, "%" PRIu64 ".%0*" PRIu64 " %s", ipart, 200 (int)cap ->dp, fpart, sunit);222 (int)capa->dp, fpart, sunit); 201 223 } else { 202 224 ret = asprintf(rstr, "%" PRIu64 " %s", ipart, sunit); 203 225 } 226 204 227 if (ret < 0) 205 228 return ENOMEM; … … 208 231 } 209 232 210 static errno_t cap _digit_val(char c, int *val)233 static errno_t capa_digit_val(char c, int *val) 211 234 { 212 235 switch (c) { … … 248 271 } 249 272 250 errno_t cap _parse(const char *str, cap_spec_t *cap)273 errno_t capa_parse(const char *str, capa_spec_t *capa) 251 274 { 252 275 const char *eptr; … … 260 283 261 284 eptr = str; 262 while (cap _digit_val(*eptr, &d) == EOK) {285 while (capa_digit_val(*eptr, &d) == EOK) { 263 286 m = m * 10 + d; 264 287 ++eptr; … … 268 291 ++eptr; 269 292 dp = 0; 270 while (cap _digit_val(*eptr, &d) == EOK) {293 while (capa_digit_val(*eptr, &d) == EOK) { 271 294 m = m * 10 + d; 272 295 ++dp; … … 281 304 282 305 if (*eptr == '\0') { 283 cap ->cunit = cu_byte;306 capa->cunit = cu_byte; 284 307 } else { 285 308 for (i = 0; i < CU_LIMIT; i++) { … … 296 319 return EINVAL; 297 320 found: 298 cap ->cunit = i;299 } 300 301 cap ->m = m;302 cap ->dp = dp;321 capa->cunit = i; 322 } 323 324 capa->m = m; 325 capa->dp = dp; 303 326 return EOK; 304 327 }
Note:
See TracChangeset
for help on using the changeset viewer.