summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/utils.c16
-rw-r--r--src/common/utils.h3
2 files changed, 19 insertions, 0 deletions
diff --git a/src/common/utils.c b/src/common/utils.c
index 354bd3814..a9ce1389f 100644
--- a/src/common/utils.c
+++ b/src/common/utils.c
@@ -221,3 +221,19 @@ uint32 MakeDWord(uint16 word0, uint16 word1)
( (uint32)(word0 ) )|
( (uint32)(word1 << 0x10) );
}
+
+
+/// calculates the value of A / B, in percent (rounded down)
+unsigned int percent(const unsigned int A, const unsigned int B)
+{
+ if( B == 0 )
+ {
+ ShowError("percent(): divison by zero!\n");
+ return -1;
+ }
+
+ if( A < UINT_MAX/100 )
+ return 100*A/B;
+ else
+ return A/(B/100);
+} \ No newline at end of file
diff --git a/src/common/utils.h b/src/common/utils.h
index 21e8e090c..93563dc34 100644
--- a/src/common/utils.h
+++ b/src/common/utils.h
@@ -18,6 +18,9 @@ void findfile(const char *p, const char *pat, void (func)(const char*));
//Caps values to min/max
#define cap_value(a, min, max) ((a >= max) ? max : (a <= min) ? min : a)
+/// calculates the value of A / B, in percent (rounded down)
+unsigned int get_percentage(const unsigned int A, const unsigned int B);
+
//////////////////////////////////////////////////////////////////////////
// byte word dword access [Shinomori]
//////////////////////////////////////////////////////////////////////////