Changeset 84876aa4 in mainline for uspace/lib/c/generic/capa.c


Ignore:
Timestamp:
2019-11-15T13:46:34Z (6 years ago)
Author:
Jiri Svoboda <jiri@…>
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.
Message:

Merge master into gfx

Mainly to get XCW fixes

File:
1 moved

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/capa.c

    rb093a62 r84876aa4  
    3434 */
    3535
    36 #include <cap.h>
     36#include <capa.h>
    3737#include <errno.h>
    3838#include <imath.h>
     
    4343enum {
    4444        /** Simplified capacity maximum integer digits */
    45         scap_max_idig = 3,
     45        scapa_max_idig = 3,
    4646        /** Simplified capacity maximum significant digits */
    47         scap_max_sdig = 4
     47        scapa_max_sdig = 4
    4848};
    4949
     
    6060};
    6161
    62 void cap_from_blocks(uint64_t nblocks, size_t block_size, cap_spec_t *cap)
     62void capa_from_blocks(uint64_t nblocks, size_t block_size, capa_spec_t *capa)
    6363{
    6464        uint64_t tsize;
    6565
    6666        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;
    7070}
    7171
     
    8181 * and @c cv_max gives the maximum value.
    8282 */
    83 errno_t cap_to_blocks(cap_spec_t *cap, cap_vsel_t cvsel, size_t block_size,
     83errno_t capa_to_blocks(capa_spec_t *capa, capa_vsel_t cvsel, size_t block_size,
    8484    uint64_t *rblocks)
    8585{
     
    9292        errno_t rc;
    9393
    94         exp = cap->cunit * 3 - cap->dp;
     94        exp = capa->cunit * 3 - capa->dp;
    9595        if (exp < 0) {
    9696                rc = ipow10_u64(-exp, &f);
    9797                if (rc != EOK)
    9898                        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)
    101101                        return ERANGE;
    102102        } else {
     
    118118                }
    119119
    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)
    122122                        return ERANGE;
    123123        }
     
    138138 * digits and at most two fractional digits, e.g abc.xy <unit>.
    139139 */
    140 void cap_simplify(cap_spec_t *cap)
     140void capa_simplify(capa_spec_t *capa)
    141141{
    142142        uint64_t div;
     
    146146        errno_t rc;
    147147
    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);
    150150        assert(rc == EOK);
    151151
    152         rc = ipow10_u64(cap->dp, &div);
     152        rc = ipow10_u64(capa->dp, &div);
    153153        assert(rc == EOK);
    154154
    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;
    158159                div = div * 1000;
    159160        }
    160161
    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) {
    164165                /* 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;
    168169
    169170                rc = ipow10_u64(rdig, &div);
    170171                assert(rc == EOK);
    171172
    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
     199errno_t capa_format(capa_spec_t *capa, char **rstr)
    178200{
    179201        errno_t rc;
     
    186208        sunit = NULL;
    187209
    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);
    191213        if (rc != EOK)
    192214                return rc;
    193215
    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) {
    199221                ret = asprintf(rstr, "%" PRIu64 ".%0*" PRIu64 " %s", ipart,
    200                     (int)cap->dp, fpart, sunit);
     222                    (int)capa->dp, fpart, sunit);
    201223        } else {
    202224                ret = asprintf(rstr, "%" PRIu64 " %s", ipart, sunit);
    203225        }
     226
    204227        if (ret < 0)
    205228                return ENOMEM;
     
    208231}
    209232
    210 static errno_t cap_digit_val(char c, int *val)
     233static errno_t capa_digit_val(char c, int *val)
    211234{
    212235        switch (c) {
     
    248271}
    249272
    250 errno_t cap_parse(const char *str, cap_spec_t *cap)
     273errno_t capa_parse(const char *str, capa_spec_t *capa)
    251274{
    252275        const char *eptr;
     
    260283
    261284        eptr = str;
    262         while (cap_digit_val(*eptr, &d) == EOK) {
     285        while (capa_digit_val(*eptr, &d) == EOK) {
    263286                m = m * 10 + d;
    264287                ++eptr;
     
    268291                ++eptr;
    269292                dp = 0;
    270                 while (cap_digit_val(*eptr, &d) == EOK) {
     293                while (capa_digit_val(*eptr, &d) == EOK) {
    271294                        m = m * 10 + d;
    272295                        ++dp;
     
    281304
    282305        if (*eptr == '\0') {
    283                 cap->cunit = cu_byte;
     306                capa->cunit = cu_byte;
    284307        } else {
    285308                for (i = 0; i < CU_LIMIT; i++) {
     
    296319                return EINVAL;
    297320        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;
    303326        return EOK;
    304327}
Note: See TracChangeset for help on using the changeset viewer.