From 4dee702332abee23086dc0d26e79f014a27a3bb3 Mon Sep 17 00:00:00 2001 From: Diego Gangl Date: Fri, 25 May 2018 22:17:15 +0200 Subject: Add number and memory size formatting throughout the UI This commit adds number formatting (thousands separator) to the baking panel. It also adds a new function to format memory sizes (KB/GB/etc) and applies it to the baking panel and scene stats. The new function is unit tested. Reviewers: Severin Tags: #user_interface Differential Revision: https://developer.blender.org/D1248 --- source/blender/blenlib/BLI_string.h | 1 + source/blender/blenlib/intern/string.c | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) (limited to 'source/blender/blenlib') diff --git a/source/blender/blenlib/BLI_string.h b/source/blender/blenlib/BLI_string.h index 48be9d1842f..faa8dc03615 100644 --- a/source/blender/blenlib/BLI_string.h +++ b/source/blender/blenlib/BLI_string.h @@ -71,6 +71,7 @@ char *BLI_sprintfN(const char *__restrict format, ...) ATTR_WARN_UNUSED_RESULT A size_t BLI_strescape(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL(); size_t BLI_str_format_int_grouped(char dst[16], int num) ATTR_NONNULL(); +void BLI_str_format_byte_unit(char dst[15], long long int size, const bool base_10) ATTR_NONNULL(); int BLI_strcaseeq(const char *a, const char *b) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); char *BLI_strcasestr(const char *s, const char *find) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c index 1a6fd082e95..da9f5a817b5 100644 --- a/source/blender/blenlib/intern/string.c +++ b/source/blender/blenlib/intern/string.c @@ -994,6 +994,41 @@ size_t BLI_str_format_int_grouped(char dst[16], int num) return (size_t)(p_dst - dst); } +/** + * Format a size in bytes using binary units. + * 1000 -> 1 KB + * Number of decimal places grows with the used unit (e.g. 1.5 MB, 1.55 GB, 1.545 TB). + * + * \param dst The resulting string. Dimension of 14 to support largest possible value for \a bytes (LLONG_MAX). + * \param bytes Number to format + * \param base_10 Calculate using base 10 (GB, MB, ...) or 2 (GiB, MiB, ...) + */ +void BLI_str_format_byte_unit(char dst[15], long long int bytes, const bool base_10) +{ + double bytes_converted = bytes; + int order = 0; + int decimals; + const int base = base_10 ? 1000 : 1024; + const char *units_base_10[] = {"B", "KB", "MB", "GB", "TB", "PB"}; + const char *units_base_2[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB"}; + const int tot_units = ARRAY_SIZE(units_base_2); + + BLI_STATIC_ASSERT(ARRAY_SIZE(units_base_2) == ARRAY_SIZE(units_base_10), "array size mismatch"); + + while ((ABS(bytes_converted) >= base) && ((order + 1) < tot_units)) { + bytes_converted /= base; + order++; + } + decimals = MAX2(order - 1, 0); + + /* Format value first, stripping away floating zeroes. */ + sprintf(dst, "%.*f", decimals, bytes_converted); + BLI_str_rstrip_float_zero(dst, '\0'); + /* Append unit. */ + sprintf(dst, "%s %s", dst, base_10 ? units_base_10[order] : units_base_2[order]); +} + + /** * Find the ranges needed to split \a str into its individual words. * -- cgit v1.2.3 From ef502854feb6b81119954206bff414d4507f4f3c Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Sat, 26 May 2018 22:29:10 +0200 Subject: Threads: add spinlock hit for hyperthreading processors on Windows. Suggested by Percy Ross Tiglao. --- source/blender/blenlib/intern/threads.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'source/blender/blenlib') diff --git a/source/blender/blenlib/intern/threads.c b/source/blender/blenlib/intern/threads.c index 761f3982e28..e6f5d9839ea 100644 --- a/source/blender/blenlib/intern/threads.c +++ b/source/blender/blenlib/intern/threads.c @@ -488,7 +488,8 @@ void BLI_spin_lock(SpinLock *spin) #elif defined(_MSC_VER) while (InterlockedExchangeAcquire(spin, 1)) { while (*spin) { - /* pass */ + /* Spinlock hint for processors with hyperthreading. */ + YieldProcessor(); } } #else -- cgit v1.2.3 From 12a9e9ae336c7d8d30d9e3f05eae67f14e2f5bb9 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sun, 27 May 2018 10:25:52 +0200 Subject: Fix restrict error in BLI_str_format_byte_unit Don't use sprintf to append a string to it's self. Also correct BLI_str_rstrip_float_zero's return value. --- source/blender/blenlib/intern/string.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'source/blender/blenlib') diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c index da9f5a817b5..49630347032 100644 --- a/source/blender/blenlib/intern/string.c +++ b/source/blender/blenlib/intern/string.c @@ -799,6 +799,7 @@ int BLI_str_rstrip_float_zero(char *str, const char pad) while (end_p != p && *end_p == '0') { *end_p = pad; end_p--; + totstrip++; } } } @@ -1022,13 +1023,13 @@ void BLI_str_format_byte_unit(char dst[15], long long int bytes, const bool base decimals = MAX2(order - 1, 0); /* Format value first, stripping away floating zeroes. */ - sprintf(dst, "%.*f", decimals, bytes_converted); - BLI_str_rstrip_float_zero(dst, '\0'); - /* Append unit. */ - sprintf(dst, "%s %s", dst, base_10 ? units_base_10[order] : units_base_2[order]); + const size_t dst_len = 15; + size_t len = BLI_snprintf_rlen(dst, dst_len, "%.*f", decimals, bytes_converted); + len -= (size_t)BLI_str_rstrip_float_zero(dst, '\0'); + dst[len++] = ' '; + BLI_strncpy(dst + len, base_10 ? units_base_10[order] : units_base_2[order], dst_len - len); } - /** * Find the ranges needed to split \a str into its individual words. * -- cgit v1.2.3