[pacman-dev] [PATCH] Remove need for floating point division in backend

Dan McGee dan at archlinux.org
Sun Jan 9 21:50:05 EST 2011


All of these can be done with integer division; the only slightly
interesting part is ensuring we round up like before with calling the
ceil() function.

Signed-off-by: Dan McGee <dan at archlinux.org>
---
 lib/libalpm/add.c       |   14 +++++++-------
 lib/libalpm/conflict.c  |    4 ++--
 lib/libalpm/diskspace.c |   18 ++++++++++--------
 lib/libalpm/remove.c    |    7 +++----
 4 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/lib/libalpm/add.c b/lib/libalpm/add.c
index 2f341eb..477c8f5 100644
--- a/lib/libalpm/add.c
+++ b/lib/libalpm/add.c
@@ -477,8 +477,8 @@ static int extract_single_file(struct archive *archive,
 	return(errors);
 }
 
-static int commit_single_pkg(pmpkg_t *newpkg, size_t pkg_current, size_t pkg_count,
-		pmtrans_t *trans, pmdb_t *db)
+static int commit_single_pkg(pmpkg_t *newpkg, size_t pkg_current,
+		size_t pkg_count, pmtrans_t *trans, pmdb_t *db)
 {
 	int i, ret = 0, errors = 0;
 	char scriptlet[PATH_MAX+1];
@@ -605,14 +605,14 @@ static int commit_single_pkg(pmpkg_t *newpkg, size_t pkg_current, size_t pkg_cou
 		}
 
 		for(i = 0; archive_read_next_header(archive, &entry) == ARCHIVE_OK; i++) {
-			double percent;
+			int percent;
 
 			if(newpkg->size != 0) {
 				/* Using compressed size for calculations here, as newpkg->isize is not
 				 * exact when it comes to comparing to the ACTUAL uncompressed size
 				 * (missing metadata sizes) */
 				int64_t pos = archive_position_compressed(archive);
-				percent = (double)pos / (double)newpkg->size;
+				percent = (pos * 100) / newpkg->size;
 				_alpm_log(PM_LOG_DEBUG, "decompression progress: "
 						"%f%% (%"PRId64" / %jd)\n",
 						percent*100.0, pos, (intmax_t)newpkg->size);
@@ -620,16 +620,16 @@ static int commit_single_pkg(pmpkg_t *newpkg, size_t pkg_current, size_t pkg_cou
 					percent = 1.0;
 				}
 			} else {
-				percent = 0.0;
+				percent = 0;
 			}
 
 			if(is_upgrade) {
 				PROGRESS(trans, PM_TRANS_PROGRESS_UPGRADE_START,
-						alpm_pkg_get_name(newpkg), (int)(percent * 100), pkg_count,
+						alpm_pkg_get_name(newpkg), percent, pkg_count,
 						pkg_current);
 			} else {
 				PROGRESS(trans, PM_TRANS_PROGRESS_ADD_START,
-						alpm_pkg_get_name(newpkg), (int)(percent * 100), pkg_count,
+						alpm_pkg_get_name(newpkg), percent, pkg_count,
 						pkg_current);
 			}
 
diff --git a/lib/libalpm/conflict.c b/lib/libalpm/conflict.c
index 0eaf437..fc25e7d 100644
--- a/lib/libalpm/conflict.c
+++ b/lib/libalpm/conflict.c
@@ -425,8 +425,8 @@ alpm_list_t *_alpm_db_find_fileconflicts(pmdb_t *db, pmtrans_t *trans,
 			continue;
 		}
 
-		double percent = (double)current / (double)numtargs;
-		PROGRESS(trans, PM_TRANS_PROGRESS_CONFLICTS_START, "", (int)(percent * 100),
+		int percent = (current * 100) / numtargs;
+		PROGRESS(trans, PM_TRANS_PROGRESS_CONFLICTS_START, "", percent,
 		         numtargs, current);
 		/* CHECK 1: check every target against every target */
 		_alpm_log(PM_LOG_DEBUG, "searching for file conflicts: %s\n",
diff --git a/lib/libalpm/diskspace.c b/lib/libalpm/diskspace.c
index 87dca84..d0f6c64 100644
--- a/lib/libalpm/diskspace.c
+++ b/lib/libalpm/diskspace.c
@@ -178,8 +178,9 @@ static int calculate_removed_size(const alpm_list_t *mount_points,
 		}
 
 		data = mp->data;
-		data->blocks_needed -= (long)ceil((double)(st.st_size) /
-		                            (double)(data->fsp.f_bsize));
+		/* the addition of (divisor - 1) performs ceil() with integer division */
+		data->blocks_needed -=
+			(st.st_size + data->fsp.f_bsize - 1) / data->fsp.f_bsize;
 		data->used = 1;
 	}
 
@@ -229,8 +230,9 @@ static int calculate_installed_size(const alpm_list_t *mount_points,
 		}
 
 		data = mp->data;
-		data->blocks_needed += (long)ceil((double)(archive_entry_size(entry)) /
-		                            (double)(data->fsp.f_bsize));
+		/* the addition of (divisor - 1) performs ceil() with integer division */
+		data->blocks_needed +=
+			(archive_entry_size(entry) + data->fsp.f_bsize - 1) / data->fsp.f_bsize;
 		data->used = 1;
 	}
 
@@ -259,8 +261,8 @@ int _alpm_check_diskspace(pmtrans_t *trans, pmdb_t *db_local)
 	if(replaces) {
 		numtargs += replaces;
 		for(targ = trans->remove; targ; targ = targ->next, current++) {
-			double percent = (double)current / (double)numtargs;
-			PROGRESS(trans, PM_TRANS_PROGRESS_DISKSPACE_START, "", (int)(percent * 100),
+			int percent = (current * 100) / numtargs;
+			PROGRESS(trans, PM_TRANS_PROGRESS_DISKSPACE_START, "", percent,
 					numtargs, current);
 
 			pkg = targ->data;
@@ -269,8 +271,8 @@ int _alpm_check_diskspace(pmtrans_t *trans, pmdb_t *db_local)
 	}
 
 	for(targ = trans->add; targ; targ = targ->next, current++) {
-		double percent = (double)current / (double)numtargs;
-		PROGRESS(trans, PM_TRANS_PROGRESS_DISKSPACE_START, "", (int)(percent * 100),
+		int percent = (current * 100) / numtargs;
+		PROGRESS(trans, PM_TRANS_PROGRESS_DISKSPACE_START, "", percent,
 				numtargs, current);
 
 		pkg = targ->data;
diff --git a/lib/libalpm/remove.c b/lib/libalpm/remove.c
index e1249fd..d4e3b94 100644
--- a/lib/libalpm/remove.c
+++ b/lib/libalpm/remove.c
@@ -425,14 +425,13 @@ int _alpm_remove_packages(pmtrans_t *trans, pmdb_t *db)
 			/* iterate through the list backwards, unlinking files */
 			newfiles = alpm_list_reverse(files);
 			for(lp = newfiles; lp; lp = alpm_list_next(lp)) {
-				double percent;
+				int percent;
 				unlink_file(info, lp->data, NULL, trans->flags & PM_TRANS_FLAG_NOSAVE);
 
 				/* update progress bar after each file */
-				percent = (double)position / (double)filenum;
+				percent = (position * 100) / filenum;
 				PROGRESS(trans, PM_TRANS_PROGRESS_REMOVE_START, info->name,
-						(int)(percent * 100), pkg_count,
-						(pkg_count - targcount + 1));
+						percent, pkg_count, (pkg_count - targcount + 1));
 				position++;
 			}
 			alpm_list_free(newfiles);
-- 
1.7.3.5



More information about the pacman-dev mailing list