Changeset e3272101 in mainline


Ignore:
Timestamp:
2019-02-11T14:08:52Z (5 years ago)
Author:
Matthieu Riolo <matthieu.riolo@…>
Children:
d5a89a3
Parents:
aa57bf7
Message:

make ilog10_u64() return an errno_t

The function ilog10_u64() used to return 0
for the value 0. Which is not correct. Either
NaN or -Infinity are correct, but not 0, since
it would be ambiguous with log(1). To ensure this
case the function ilog10_u64() has been changed
to return a errno_t indicating a failure.

Location:
uspace/lib/c
Files:
4 edited

Legend:

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

    raa57bf7 re3272101  
    160160
    161161        /* 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 */
     162        rc = ilog10_u64(cap->m, &sdig); /* number of significant digits */
     163        sdig += 1;
     164        assert(rc == EOK);
    163165        if (sdig > scap_max_sdig) {
    164166                /* Number of digits to remove */
  • uspace/lib/c/generic/imath.c

    raa57bf7 re3272101  
    8181 *
    8282 * @param v Value to compute logarithm from
     83 * @param res Place to store result
     84 * @return EOK on success
    8385 * @return Logarithm value
    8486 */
    85 unsigned ilog10_u64(uint64_t v)
     87errno_t ilog10_u64(uint64_t v, unsigned *res)
    8688{
     89        if (v == 0)
     90                return ERANGE;
     91
    8792        unsigned b;
    8893        unsigned e;
     
    117122        }
    118123
    119         return r;
     124        *res = r;
     125        return EOK;
    120126}
    121127
  • uspace/lib/c/include/imath.h

    raa57bf7 re3272101  
    4040
    4141extern errno_t ipow10_u64(unsigned, uint64_t *);
    42 extern unsigned ilog10_u64(uint64_t);
     42extern errno_t ilog10_u64(uint64_t, unsigned *);
    4343
    4444#endif
  • uspace/lib/c/test/imath.c

    raa57bf7 re3272101  
    7878PCUT_TEST(ilog10_u64_zero)
    7979{
    80         unsigned ret = ilog10_u64(0);
    81         PCUT_ASSERT_INT_EQUALS(0, ret);
     80        unsigned res;
     81        errno_t ret = ilog10_u64(0, &res);
     82        PCUT_ASSERT_ERRNO_VAL(ERANGE, ret);
    8283}
    8384
    8485PCUT_TEST(ilog10_u64_one)
    8586{
    86         unsigned ret = ilog10_u64(1);
    87         PCUT_ASSERT_INT_EQUALS(0, ret);
     87        unsigned res;
     88        errno_t ret = ilog10_u64(1, &res);
     89        PCUT_ASSERT_ERRNO_VAL(EOK, ret);
     90        PCUT_ASSERT_INT_EQUALS(0, res);
    8891}
    8992
    9093PCUT_TEST(ilog10_u64_max)
    9194{
    92         unsigned ret = ilog10_u64(MAX_NUM);
    93         PCUT_ASSERT_INT_EQUALS(MAX_EXP, ret);
     95        unsigned res;
     96        errno_t ret = ilog10_u64(MAX_NUM, &res);
     97        PCUT_ASSERT_ERRNO_VAL(EOK, ret);
     98        PCUT_ASSERT_INT_EQUALS(MAX_EXP, res);
    9499}
    95100
Note: See TracChangeset for help on using the changeset viewer.