diff options
| author | Mattias Andrée <maandree@kth.se> | 2016-03-02 09:11:48 +0100 |
|---|---|---|
| committer | Mattias Andrée <maandree@kth.se> | 2016-03-02 09:12:31 +0100 |
| commit | ea2d6c93d28a9c5af94035057c52c36f0618ba2c (patch) | |
| tree | 73cc4b6cbb66e623ae6f5ee933c489bae97a01d8 /src | |
| parent | Why zbits returns 1 for 0 (diff) | |
| download | libzahl-ea2d6c93d28a9c5af94035057c52c36f0618ba2c.tar.gz libzahl-ea2d6c93d28a9c5af94035057c52c36f0618ba2c.tar.bz2 libzahl-ea2d6c93d28a9c5af94035057c52c36f0618ba2c.tar.xz | |
zstr_length_positive is safe for non-positive, hence rename to zstr_length; and add zstr_length
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'src')
| -rw-r--r-- | src/internals.h | 5 | ||||
| -rw-r--r-- | src/zstr_length.c | 28 |
2 files changed, 32 insertions, 1 deletions
diff --git a/src/internals.h b/src/internals.h index df973fb..a544924 100644 --- a/src/internals.h +++ b/src/internals.h @@ -9,7 +9,10 @@ #define BITS_IN_LAST_CHAR(bits) ((bits) & (BITS_PER_CHAR - 1)) #define LIST_TEMPS\ - X(libzahl_tmp_cmp) + X(libzahl_tmp_cmp)\ + X(libzahl_tmp_str_num)\ + X(libzahl_tmp_str_mag)\ + X(libzahl_tmp_str_div) #define X(x) extern z_t x; LIST_TEMPS diff --git a/src/zstr_length.c b/src/zstr_length.c new file mode 100644 index 0000000..4edabb0 --- /dev/null +++ b/src/zstr_length.c @@ -0,0 +1,28 @@ +/* See LICENSE file for copyright and license details. */ +#include "internals" + +#define num libzahl_tmp_str_num +#define mag libzahl_tmp_str_mag +#define div libzahl_tmp_str_div + + +size_t +zstr_length(z_t a, unsigned long long int radix) +{ + size_t size_total = 1, size_temp; + zset(num, a); + while (!zzero(num)) { + zsetu(mag, radix); + zset(div, mag); + size_temp = 1; + while (zcmpmag(mag, num) <= 0) { + zset(div, mag); + zsqr(mag, mag); + size_temp <<= 1; + } + size_temp >>= 1; + size_total += size_temp; + zdiv(num, num, div); + } + return size_total; +} |
