gcode_parse: limit decimal ranges by decfloat_to_int()'s demands.

Formerly, it was limited by variable bit ranges, which allowed
more than decfloat_to_int() can swallow.
This commit is contained in:
Markus Hitter 2011-02-22 22:14:51 +01:00
parent 0dc623d4eb
commit 2c53dbe2a8
2 changed files with 13 additions and 13 deletions

View File

@ -62,12 +62,16 @@ GCODE_COMMAND next_target __attribute__ ((__section__ (".bss")));
multiplicand: 1..105000 (17 bit)
denominator: 1 or 10 ( 4 bit)
*/
// accordingly:
#define DECFLOAT_EXP_MAX 4
#define DECFLOAT_MANT_MM_MAX 1048075
#define DECFLOAT_MANT_IN_MAX 32267
/*
utility functions
*/
extern const uint32_t powers[]; // defined in sermsg.c
const int32_t rounding[DECFLOAT_EXP_MAX] = {0, 5, 50, 500, 5000, 50000, 500000};
const int32_t rounding[DECFLOAT_EXP_MAX] = {0, 5, 50, 500};
static int32_t decfloat_to_int(decfloat *df, int32_t multiplicand, uint32_t denominator) {
int32_t r = df->mantissa;
@ -292,10 +296,12 @@ void gcode_parse_char(uint8_t c) {
default:
// can't do ranges in switch..case, so process actual digits here.
if ( c >= '0'
&& c <= '9'
&& read_digit.mantissa < (DECFLOAT_MANT_MAX / 10)
&& read_digit.exponent < DECFLOAT_EXP_MAX ) {
if (c >= '0' && c <= '9' &&
read_digit.exponent < DECFLOAT_EXP_MAX &&
((next_target.option_inches == 0 &&
read_digit.mantissa < DECFLOAT_MANT_MM_MAX) ||
(next_target.option_inches &&
read_digit.mantissa < DECFLOAT_MANT_IN_MAX))){
// this is simply mantissa = (mantissa * 10) + atoi(c) in different clothes
read_digit.mantissa = (read_digit.mantissa << 3) + (read_digit.mantissa << 1) + (c - '0');
if (read_digit.exponent)

View File

@ -18,16 +18,10 @@
// this is a very crude decimal-based floating point structure.
// a real floating point would at least have signed exponent.
// max (DECFLOAT_EXP_MAX - 1) digits after decimal point, because point is
// counted as well
#define DECFLOAT_EXP_WIDTH 3
#define DECFLOAT_EXP_MAX ((1 << DECFLOAT_EXP_WIDTH) - 1)
#define DECFLOAT_MANT_WIDTH (32 - 1 - DECFLOAT_EXP_WIDTH)
#define DECFLOAT_MANT_MAX (((uint32_t)1 << DECFLOAT_MANT_WIDTH) - 1)
typedef struct {
uint32_t sign :1;
uint32_t mantissa :DECFLOAT_MANT_WIDTH;
uint32_t exponent :DECFLOAT_EXP_WIDTH;
uint32_t mantissa :28;
uint32_t exponent :3;
} decfloat;
// this holds all the possible data from a received command