]> git.ipfire.org Git - thirdparty/e2fsprogs.git/commitdiff
misc: deduplicate log2/log10 functions
authorAndreas Dilger <adilger@whamcloud.com>
Sat, 25 Jan 2025 00:42:18 +0000 (17:42 -0700)
committerTheodore Ts'o <tytso@mit.edu>
Wed, 21 May 2025 13:48:47 +0000 (09:48 -0400)
Remove duplicate log2() and log10() functions and replace them with
functions ext2fs_log2_u{32,64}() and ext2fs_log10_u{32,64}().

The int_log10() functions in progress.c and mke2fs.c were not like
the others, since they did not divide by the base before increment,
effectively rounding up instead of down.  Compensate by adding one
to the returned ext2fs_log10_u32() value at the callers.

Signed-off-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: Li Dongyang <dongyangli@ddn.com>
Change-Id: Ifc86efe7e5f0243eb914c6d24319cc7dee3ebbe5
Reviewed-on: https://review.whamcloud.com/52385
Link: https://lore.kernel.org/r/20250125004220.44607-1-adilger@whamcloud.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
debugfs/debugfs.c
debugfs/filefrag.c
lib/ext2fs/ext2fs.h
lib/ext2fs/extent.c
lib/ext2fs/progress.c
misc/e2freefrag.c
misc/e4crypt.c
misc/filefrag.c
misc/mk_hugefiles.c
misc/mke2fs.c
misc/mke2fs.h

index 8acb56a4d4be63a8bf60efa11e09da73c55ade8b..46ff517e0515732d1afde400c0c0ebc33265a451 100644 (file)
@@ -653,18 +653,6 @@ static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
        fprintf(f,"\n");
 }
 
-static int int_log10(unsigned long long arg)
-{
-       int     l = 0;
-
-       arg = arg / 10;
-       while (arg) {
-               l++;
-               arg = arg / 10;
-       }
-       return l;
-}
-
 #define DUMP_LEAF_EXTENTS      0x01
 #define DUMP_NODE_EXTENTS      0x02
 #define DUMP_EXTENT_TABLE      0x04
@@ -1076,11 +1064,12 @@ void do_dump_extents(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused))
                return;
        }
 
-       logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
-                                 current_fs->blocksize) + 1;
+       logical_width = ext2fs_log10_u32((EXT2_I_SIZE(&inode) +
+                                         current_fs->blocksize - 1) /
+                                        current_fs->blocksize) + 1;
        if (logical_width < 5)
                logical_width = 5;
-       physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
+       physical_width = ext2fs_log10_u64(ext2fs_blocks_count(current_fs->super)) + 1;
        if (physical_width < 5)
                physical_width = 5;
 
index 9bda65defe6af49329b04da7cf6371ac211e2e2c..2a5b6a91afd39c18cefd9086fb3c54db5d830712 100644 (file)
@@ -54,18 +54,6 @@ struct filefrag_struct {
        struct dir_list *dir_list, *dir_last;
 };
 
-static int int_log10(unsigned long long arg)
-{
-       int     l = 0;
-
-       arg = arg / 10;
-       while (arg) {
-               l++;
-               arg = arg / 10;
-       }
-       return l;
-}
-
 static void print_header(struct filefrag_struct *fs)
 {
        if (fs->options & VERBOSE_OPT) {
@@ -135,8 +123,8 @@ static void filefrag(ext2_ino_t ino, struct ext2_inode *inode,
        errcode_t       retval;
        int             blocksize = current_fs->blocksize;
 
-       fs->logical_width = int_log10((EXT2_I_SIZE(inode) + blocksize - 1) /
-                                     blocksize) + 1;
+       fs->logical_width = ext2fs_log10_u32((EXT2_I_SIZE(inode) +
+                                             blocksize - 1) / blocksize) + 1;
        if (fs->logical_width < 7)
                fs->logical_width = 7;
        fs->ext = 0;
@@ -313,7 +301,7 @@ void do_filefrag(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
                return;
 
        fs.f = open_pager();
-       fs.physical_width = int_log10(ext2fs_blocks_count(current_fs->super));
+       fs.physical_width = ext2fs_log10_u64(ext2fs_blocks_count(current_fs->super));
        fs.physical_width++;
        if (fs.physical_width < 8)
                fs.physical_width = 8;
index ff22f66baba691bf174a94bb297f427f0dcd7543..80310b2998d876830774dbcb0f4c7287519d760d 100644 (file)
@@ -2226,6 +2226,66 @@ _INLINE_ int ext2fs_htree_intnode_maxrecs(ext2_filsys fs, int blocks)
                                                sizeof(struct ext2_dx_entry));
 }
 
+/*
+ * log base 2, rounded down
+ */
+_INLINE_ int ext2fs_log2_u32(__u32 arg)
+{
+       int l = 0;
+
+       arg >>= 1;
+       while (arg) {
+               l++;
+               arg >>= 1;
+       }
+       return l;
+}
+
+/*
+ * log base 2, rounded down
+ */
+_INLINE_ int ext2fs_log2_u64(__u64 arg)
+{
+       int l = 0;
+
+       arg >>= 1;
+       while (arg) {
+               l++;
+               arg >>= 1;
+       }
+       return l;
+}
+
+/*
+ * log base 10, rounded down
+ */
+_INLINE_ int ext2fs_log10_u32(__u32 arg)
+{
+       int l = 0;
+
+       arg /= 10;
+       while (arg) {
+               l++;
+               arg /= 10;
+       }
+       return l;
+}
+
+/*
+ * log base 10, rounded down
+ */
+_INLINE_ int ext2fs_log10_u64(__u64 arg)
+{
+       int l = 0;
+
+       arg /= 10;
+       while (arg) {
+               l++;
+               arg /= 10;
+       }
+       return l;
+}
+
 /*
  * This is an efficient, overflow safe way of calculating ceil((1.0 * a) / b)
  */
index c4b95741ef09918a49d75ffda42115e1af3f7ea4..013ea2f633ccac697091feec843cc7db84a22bdb 100644 (file)
@@ -1743,18 +1743,6 @@ errcode_t ext2fs_extent_get_info(ext2_extent_handle_t handle,
        return 0;
 }
 
-static int ul_log2(unsigned long arg)
-{
-       int     l = 0;
-
-       arg >>= 1;
-       while (arg) {
-               l++;
-               arg >>= 1;
-       }
-       return l;
-}
-
 size_t ext2fs_max_extent_depth(ext2_extent_handle_t handle)
 {
        size_t iblock_sz = sizeof(((struct ext2_inode *)NULL)->i_block);
@@ -1769,8 +1757,9 @@ size_t ext2fs_max_extent_depth(ext2_extent_handle_t handle)
        if (last_blocksize && last_blocksize == handle->fs->blocksize)
                return last_result;
 
-       last_result = 1 + ((ul_log2(EXT_MAX_EXTENT_LBLK) - ul_log2(iblock_extents)) /
-                   ul_log2(extents_per_block));
+       last_result = 1 + ((ext2fs_log2_u64(EXT_MAX_EXTENT_LBLK) -
+                           ext2fs_log2_u64(iblock_extents)) /
+                          ext2fs_log2_u32(extents_per_block));
        last_blocksize = handle->fs->blocksize;
        return last_result;
 }
index 61ab3f04a563b825505eff90ba4fcf5b7e7ff172..b2eab7ea11d14fb3532c7972e6cbc0d37ed26b6b 100644 (file)
@@ -25,15 +25,6 @@ struct ext2fs_progress_ops ext2fs_numeric_progress_ops = {
        .close          = ext2fs_numeric_progress_close,
 };
 
-static int int_log10(unsigned int arg)
-{
-       int     l;
-
-       for (l=0; arg ; l++)
-               arg = arg / 10;
-       return l;
-}
-
 void ext2fs_numeric_progress_init(ext2_filsys fs,
                                  struct ext2fs_numeric_progress_struct * progress,
                                  const char *label, __u64 max)
@@ -58,10 +49,10 @@ void ext2fs_numeric_progress_init(ext2_filsys fs,
 
 
        /*
-        * Figure out how many digits we need
+        * Figure out how many digits we need (always at least one)
         */
        progress->max = max;
-       progress->log_max = int_log10(max);
+       progress->log_max = ext2fs_log10_u64(max) + 1;
 
        if (label) {
                fputs(label, stdout);
index 63a3d435103e1f0b631629fe9b3643c52967dfce..d5d2df428cd764c9df1c02c52d710d873f0aa34f 100644 (file)
@@ -57,28 +57,16 @@ static void usage(const char *prog)
 #endif
 }
 
-static int ul_log2(unsigned long arg)
-{
-        int     l = 0;
-
-        arg >>= 1;
-        while (arg) {
-                l++;
-                arg >>= 1;
-        }
-        return l;
-}
-
 static void init_chunk_info(ext2_filsys fs, struct chunk_info *info)
 {
        int i;
 
-       info->blocksize_bits = ul_log2((unsigned long)fs->blocksize);
+       info->blocksize_bits = ext2fs_log2_u32(fs->blocksize);
        if (info->chunkbytes) {
-               info->chunkbits = ul_log2(info->chunkbytes);
+               info->chunkbits = ext2fs_log2_u32(info->chunkbytes);
                info->blks_in_chunk = info->chunkbytes >> info->blocksize_bits;
        } else {
-               info->chunkbits = ul_log2(DEFAULT_CHUNKSIZE);
+               info->chunkbits = ext2fs_log2_u32(DEFAULT_CHUNKSIZE);
                info->blks_in_chunk = DEFAULT_CHUNKSIZE >> info->blocksize_bits;
        }
 
@@ -97,7 +85,7 @@ static void update_chunk_stats(struct chunk_info *info,
 {
        unsigned long idx;
 
-       idx = ul_log2(chunk_size) + 1;
+       idx = ext2fs_log2_u32(chunk_size) + 1;
        if (idx >= MAX_HIST)
                idx = MAX_HIST-1;
        info->histogram.fc_chunks[idx]++;
index af907041c8c2d81004cf18271e1b93e2caa55fad..eae1965a895465af91a371ca4000cc5033f48074 100644 (file)
@@ -114,18 +114,6 @@ static const size_t hexchars_size = 16;
 #define EXT4_IOC_SET_ENCRYPTION_POLICY      _IOR('f', 19, struct ext4_encryption_policy)
 #define EXT4_IOC_GET_ENCRYPTION_POLICY      _IOW('f', 21, struct ext4_encryption_policy)
 
-static int int_log2(int arg)
-{
-       int     l = 0;
-
-       arg >>= 1;
-       while (arg) {
-               l++;
-               arg >>= 1;
-       }
-       return l;
-}
-
 static void validate_paths(int argc, char *argv[], int path_start_index)
 {
        int x;
@@ -386,7 +374,7 @@ static void set_policy(struct salt *set_salt, int pad,
                        EXT4_ENCRYPTION_MODE_AES_256_XTS;
                policy.filenames_encryption_mode =
                        EXT4_ENCRYPTION_MODE_AES_256_CTS;
-               policy.flags = int_log2(pad >> 2);
+               policy.flags = ext2fs_log2_u32(pad >> 2);
                memcpy(policy.master_key_descriptor, salt->key_desc,
                       EXT4_KEY_DESCRIPTOR_SIZE);
                rc = ioctl(fd, EXT4_IOC_SET_ENCRYPTION_POLICY, &policy);
index eaaa90a8bb6624bbda002b72da18cf2a06e92a3e..4641714c2ecfc365170e2f35ca75a3ae29b7b9ad 100644 (file)
@@ -76,30 +76,6 @@ const char *hex_fmt = "%4d: %*llx..%*llx: %*llx..%*llx: %6llx: %s\n";
 #define        EXT4_EXTENTS_FL                 0x00080000 /* Inode uses extents */
 #define        EXT3_IOC_GETFLAGS               _IOR('f', 1, long)
 
-static int ulong_log2(unsigned long arg)
-{
-       int     l = 0;
-
-       arg >>= 1;
-       while (arg) {
-               l++;
-               arg >>= 1;
-       }
-       return l;
-}
-
-static int ulong_log10(unsigned long long arg)
-{
-       int     l = 0;
-
-       arg = arg / 10;
-       while (arg) {
-               l++;
-               arg = arg / 10;
-       }
-       return l;
-}
-
 static unsigned int div_ceil(unsigned int a, unsigned int b)
 {
        if (!a)
@@ -483,20 +459,20 @@ static int frag_report(const char *filename)
        }
        last_device = st.st_dev;
 
-       width = ulong_log10(fsinfo.f_blocks);
+       width = ext2fs_log10_u64(fsinfo.f_blocks);
        if (width > physical_width)
                physical_width = width;
 
        numblocks = (st.st_size + blksize - 1) / blksize;
        if (blocksize != 0)
-               blk_shift = ulong_log2(blocksize);
+               blk_shift = ext2fs_log2_u32(blocksize);
        else
-               blk_shift = ulong_log2(blksize);
+               blk_shift = ext2fs_log2_u32(blksize);
 
        if (use_extent_cache)
                width = 10;
        else
-               width = ulong_log10(numblocks);
+               width = ext2fs_log10_u64(numblocks);
        if (width > logical_width)
                logical_width = width;
        if (verbose) {
index 3caaf1b684c3507e552fcf23dec82604fb4beeb8..f2957d8cc1ec83901d3ffc8b2b39cb55cccf1c1c 100644 (file)
@@ -417,7 +417,7 @@ errcode_t mk_hugefiles(ext2_filsys fs, const char *device_name)
        fn_prefix = get_string_from_profile(fs_types, "hugefiles_name",
                                            "hugefile");
        idx_digits = get_int_from_profile(fs_types, "hugefiles_digits", 5);
-       d = int_log10(num_files) + 1;
+       d = ext2fs_log10_u32(num_files) + 1;
        if (idx_digits > d)
                d = idx_digits;
        dsize = strlen(fn_prefix) + d + 16;
index f24076bc1a3bc442d26f9e8299d16dd03acc436a..fbd81624ab38380f706c44f4173993fb3fa4a271 100644 (file)
@@ -147,27 +147,6 @@ static void usage(void)
        exit(1);
 }
 
-static int int_log2(unsigned long long arg)
-{
-       int     l = 0;
-
-       arg >>= 1;
-       while (arg) {
-               l++;
-               arg >>= 1;
-       }
-       return l;
-}
-
-int int_log10(unsigned long long arg)
-{
-       int     l;
-
-       for (l=0; arg ; l++)
-               arg = arg / 10;
-       return l;
-}
-
 #ifdef __linux__
 static int parse_version_number(const char *s)
 {
@@ -782,7 +761,7 @@ skip_details:
                        continue;
                if (i != 1)
                        printf(", ");
-               need = int_log10(group_block) + 2;
+               need = ext2fs_log10_u64(group_block) + 3;
                if (need > col_left) {
                        printf("\n\t");
                        col_left = 72;
@@ -1753,8 +1732,8 @@ profile_error:
                                        blocksize);
                        if (blocksize > 0)
                                fs_param.s_log_block_size =
-                                       int_log2(blocksize >>
-                                                EXT2_MIN_BLOCK_LOG_SIZE);
+                                       ext2fs_log2_u32(blocksize >>
+                                                      EXT2_MIN_BLOCK_LOG_SIZE);
                        break;
                case 'c':       /* Check for bad blocks */
                        cflag++;
@@ -2042,7 +2021,7 @@ profile_error:
                blocksize = jfs->blocksize;
                printf(_("Using journal device's blocksize: %d\n"), blocksize);
                fs_param.s_log_block_size =
-                       int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
+                       ext2fs_log2_u32(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
                ext2fs_close_free(&jfs);
        }
 
@@ -2297,7 +2276,7 @@ profile_error:
        }
 
        fs_param.s_log_block_size =
-               int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
+               ext2fs_log2_u32(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
 
        /*
         * We now need to do a sanity check of fs_blocks_count for
@@ -2396,7 +2375,8 @@ profile_error:
                                                            "cluster_size",
                                                            blocksize*16);
                fs_param.s_log_cluster_size =
-                       int_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE);
+                       ext2fs_log2_u32(cluster_size >>
+                                       EXT2_MIN_CLUSTER_LOG_SIZE);
                if (fs_param.s_log_cluster_size &&
                    fs_param.s_log_cluster_size < fs_param.s_log_block_size) {
                        com_err(program_name, 0, "%s",
@@ -2686,7 +2666,7 @@ profile_error:
                                  "flex_bg size may not be specified"));
                        exit(1);
                }
-               fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
+               fs_param.s_log_groups_per_flex = ext2fs_log2_u32(flex_bg_size);
        }
 
        if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
index ce72cb3f597402e80d8a225f590ef026ef85f883..c718fcebafbe65d59cf8190f6021927e6e9dc0ac 100644 (file)
@@ -21,7 +21,6 @@ extern char *get_string_from_profile(char **types, const char *opt,
                                     const char *def_val);
 extern int get_int_from_profile(char **types, const char *opt, int def_val);
 extern int get_bool_from_profile(char **types, const char *opt, int def_val);
-extern int int_log10(unsigned long long arg);
 
 /* mk_hugefiles.c */
 extern errcode_t mk_hugefiles(ext2_filsys fs, const char *device_name);