diff --git a/src/threads/fixed-point.h b/src/threads/fixed-point.h index c56b3b6..69adb63 100644 --- a/src/threads/fixed-point.h +++ b/src/threads/fixed-point.h @@ -3,37 +3,40 @@ #define FIXED_POINT_H typedef struct -{ - int32_t raw; -} fp32_t; + { + int32_t raw; + } fp32_t; /* Fixed Point Arithmetic bit count constants */ #define NUM_FRAC_BITS 14 #define NUM_INT_BITS (31 - NUM_FRAC_BITS) -#define CONVERSION_CONST (1 << NUM_FRAC_BITS) /* f = 2^q, (2^20) */ +#define CONVERSION_FACTOR (1 << NUM_FRAC_BITS) /* f = 2^q, (2^14) */ /* Fixed Point Arithmetic conversion operations */ /* Converts an integer n to a fixed point number */ inline fp32_t -int_to_fp (int32_t n) +fp_from_int (int32_t n) { - return (fp32_t){ n * CONVERSION_CONST }; + return (fp32_t){ n * CONVERSION_FACTOR }; } -/* Handles conversion of fixed point to integer. First version truncates, second one rounds */ +/* Handles conversion of fixed point to integer, + with truncation */ inline int32_t fp_floor (fp32_t x) { - return x.raw / CONVERSION_CONST; + return x.raw / CONVERSION_FACTOR; } +/* Handles conversion of fixed point to integer, + with rounding */ inline int32_t fp_round (fp32_t x) { if (x.raw >= 0) - return (x.raw + CONVERSION_CONST / 2) / CONVERSION_CONST; + return (x.raw + CONVERSION_FACTOR / 2) / CONVERSION_FACTOR; else - return (x.raw - CONVERSION_CONST / 2) / CONVERSION_CONST; + return (x.raw - CONVERSION_FACTOR / 2) / CONVERSION_FACTOR; } /* Add two fixed points */ @@ -50,32 +53,20 @@ fp_sub (fp32_t x, fp32_t y) return (fp32_t){ x.raw - y.raw }; } -/* Add fixed point to integer */ -inline fp32_t -fp_add_int (fp32_t x, int32_t n) -{ - return (fp32_t){ x.raw + n * CONVERSION_CONST }; -} -/* Subtract integer from fixed point */ -inline fp32_t -fp_sub_int (fp32_t x, int32_t n) -{ - return (fp32_t){ x.raw - n * CONVERSION_CONST }; -} /* Multiple two fixed points */ inline fp32_t fp_mul (fp32_t x, fp32_t y) { - return (fp32_t){ ((int64_t)x.raw) * y.raw / CONVERSION_CONST }; + return (fp32_t){ ((int64_t)x.raw) * y.raw / CONVERSION_FACTOR }; } /* Divide two fixed points */ inline fp32_t fp_div (fp32_t x, fp32_t y) { - return (fp32_t){ ((int64_t)x.raw) * CONVERSION_CONST / y.raw }; + return (fp32_t){ ((int64_t)x.raw) * CONVERSION_FACTOR / y.raw }; } /* Multiply fixed point and integer */ @@ -92,4 +83,18 @@ fp_div_int (fp32_t x, int32_t n) return (fp32_t){ x.raw / n }; } +/* Add fixed point to integer */ +inline fp32_t +fp_add_int (fp32_t x, int32_t n) +{ + return (fp32_t){ x.raw + n * CONVERSION_FACTOR }; +} + +/* Subtract integer from fixed point */ +inline fp32_t +fp_sub_int (fp32_t x, int32_t n) +{ + return (fp32_t){ x.raw - n * CONVERSION_FACTOR }; +} + #endif //FIXED_POINT_H diff --git a/src/threads/thread.c b/src/threads/thread.c index cb60d1b..dd472c1 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -163,7 +163,7 @@ thread_tick (void) if (t != idle_thread) ready++; fp32_t old_coeff = fp_div_int (fp_mul_int(load_avg, 59), 60); - fp32_t new_coeff = fp_div_int (int_to_fp (ready), 60); + fp32_t new_coeff = fp_div_int (fp_from_int (ready), 60); load_avg = fp_add (old_coeff, new_coeff); thread_foreach (thread_update_recent_cpu, NULL);