Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit 2c53dbe2a8109cbc00c19fd1dac093e8e6c7ff5f 1 parent 0dc623d
@Traumflug Traumflug authored
Showing with 13 additions and 13 deletions.
  1. +11 −5 gcode_parse.c
  2. +2 −8 gcode_parse.h
View
16 gcode_parse.c
@@ -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
10 gcode_parse.h
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.