From bdeb4827fca9b4b31ba8ae30b33fab0235422e77 Mon Sep 17 00:00:00 2001 From: Markus Hitter Date: Wed, 23 Feb 2011 14:37:56 +0100 Subject: [PATCH] gcode_parse.c: replace some ints by uints to double the range. This is without accuracy or other losses and should finally make decfloat_to_int() free of variable overflow within the decribed ranges. Surprisingly, this costs 14 bytes program size. --- gcode_parse.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/gcode_parse.c b/gcode_parse.c index 30d4e5c..9b51192 100644 --- a/gcode_parse.c +++ b/gcode_parse.c @@ -74,28 +74,28 @@ extern const uint32_t powers[]; // defined in sermsg.c const int32_t rounding[DECFLOAT_EXP_MAX] = {0, 5, 50, 500}; static int32_t decfloat_to_int(decfloat *df, uint32_t multiplicand, uint32_t denominator) { - int32_t r = df->mantissa; + uint32_t r = df->mantissa; uint8_t e = df->exponent; // e=1 means we've seen a decimal point but no digits after it, and e=2 means we've seen a decimal point with one digit so it's too high by one if not zero if (e) e--; - int32_t rnew1 = r * (multiplicand / denominator); + uint32_t rnew1 = r * (multiplicand / denominator); if (e) { - int32_t rnew2 = r * (multiplicand % denominator) / denominator; + uint32_t rnew2 = r * (multiplicand % denominator) / denominator; r = rnew1 + rnew2; r = (r + rounding[e]) / powers[e]; } else { - int32_t rnew2 = (r * (multiplicand % denominator) + (denominator / 2)) / denominator; + uint32_t rnew2 = (r * (multiplicand % denominator) + (denominator / 2)) / denominator; r = rnew1 + rnew2; } - return df->sign ? -r : r; + return df->sign ? -(int32_t)r : (int32_t)r; } /****************************************************************************