Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Fix narrowing conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Jun 3, 2023
1 parent ac954b5 commit 0c14aa0
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 19 deletions.
15 changes: 15 additions & 0 deletions Marlin/src/core/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,11 @@ struct XYval {
FI XYval<T>& operator>>=(const int &p) { _RSE(x); _RSE(y); return *this; }
FI XYval<T>& operator<<=(const int &p) { _LSE(x); _LSE(y); return *this; }

// Absolute difference between two objects
FI constexpr XYval<T> diff(const XYZEval<T> &rs) const { return { TERN(HAS_X_AXIS, T(_ABS(x - rs.x)), x), TERN(HAS_Y_AXIS, T(_ABS(y - rs.y)), y) }; }
FI constexpr XYval<T> diff(const XYZval<T> &rs) const { return { TERN(HAS_X_AXIS, T(_ABS(x - rs.x)), x), TERN(HAS_Y_AXIS, T(_ABS(y - rs.y)), y) }; }
FI constexpr XYval<T> diff(const XYval<T> &rs) const { return { T(_ABS(x - rs.x)), T(_ABS(y - rs.y)) }; }

// Exact comparisons. For floats a "NEAR" operation may be better.
FI bool operator==(const XYval<T> &rs) const { return x == rs.x && y == rs.y; }
FI bool operator==(const XYZval<T> &rs) const { return ENABLED(HAS_X_AXIS) XY_GANG(&& x == rs.x, && y == rs.y); }
Expand Down Expand Up @@ -607,6 +612,11 @@ struct XYZval {
FI constexpr XYZval<T> operator<<(const int &p) const { return NUM_AXIS_ARRAY(_LS(x), _LS(y), _LS(z), _LS(i), _LS(j), _LS(k), _LS(u), _LS(v), _LS(w)); }
FI constexpr XYZval<T> operator-() const { return NUM_AXIS_ARRAY(-x, -y, -z, -i, -j, -k, -u, -v, -w); }

// Absolute difference between two objects
FI constexpr XYZval<T> diff(const XYZEval<T> &rs) const { return NUM_AXIS_ARRAY(T(_ABS(x - rs.x)), T(_ABS(y - rs.y)), T(_ABS(z - rs.z)), T(_ABS(i - rs.i)), T(_ABS(j - rs.j)), T(_ABS(k - rs.k)), T(_ABS(u - rs.u)), T(_ABS(v - rs.v)), T(_ABS(w - rs.w)) ); }
FI constexpr XYZval<T> diff(const XYZval<T> &rs) const { return NUM_AXIS_ARRAY(T(_ABS(x - rs.x)), T(_ABS(y - rs.y)), T(_ABS(z - rs.z)), T(_ABS(i - rs.i)), T(_ABS(j - rs.j)), T(_ABS(k - rs.k)), T(_ABS(u - rs.u)), T(_ABS(v - rs.v)), T(_ABS(w - rs.w)) ); }
FI constexpr XYZval<T> diff(const XYval<T> &rs) const { return NUM_AXIS_ARRAY(T(_ABS(x - rs.x)), T(_ABS(y - rs.y)), z, i, j, k, u, v, w ); }

// Modifier operators
FI XYZval<T>& operator+=(const XYval<T> &rs) { XY_CODE(x += rs.x, y += rs.y); return *this; }
FI XYZval<T>& operator-=(const XYval<T> &rs) { XY_CODE(x -= rs.x, y -= rs.y); return *this; }
Expand Down Expand Up @@ -749,6 +759,11 @@ struct XYZEval {
FI constexpr XYZEval<T> operator<<(const int &p) const { return LOGICAL_AXIS_ARRAY(_LS(e), _LS(x), _LS(y), _LS(z), _LS(i), _LS(j), _LS(k), _LS(u), _LS(v), _LS(w)); }
FI constexpr XYZEval<T> operator-() const { return LOGICAL_AXIS_ARRAY(-e, -x, -y, -z, -i, -j, -k, -u, -v, -w); }

// Absolute difference between two objects
FI constexpr XYZEval<T> diff(const XYZEval<T> &rs) const { return LOGICAL_AXIS_ARRAY(T(_ABS(e - rs.e)), T(_ABS(x - rs.x)), T(_ABS(y - rs.y)), T(_ABS(z - rs.z)), T(_ABS(i - rs.i)), T(_ABS(j - rs.j)), T(_ABS(k - rs.k)), T(_ABS(u - rs.u)), T(_ABS(v - rs.v)), T(_ABS(w - rs.w)) ); }
FI constexpr XYZEval<T> diff(const XYZval<T> &rs) const { return LOGICAL_AXIS_ARRAY(0 , T(_ABS(x - rs.x)), T(_ABS(y - rs.y)), T(_ABS(z - rs.z)), T(_ABS(i - rs.i)), T(_ABS(j - rs.j)), T(_ABS(k - rs.k)), T(_ABS(u - rs.u)), T(_ABS(v - rs.v)), T(_ABS(w - rs.w)) ); }
FI constexpr XYZEval<T> diff(const XYval<T> &rs) const { return LOGICAL_AXIS_ARRAY(0 , T(_ABS(x - rs.x)), T(_ABS(y - rs.y)), z, i, j, k, u, v, w ); }

// Modifier operators
FI XYZEval<T>& operator+=(const XYval<T> &rs) { XY_CODE(x += rs.x, y += rs.y); return *this; }
FI XYZEval<T>& operator-=(const XYval<T> &rs) { XY_CODE(x -= rs.x, y -= rs.y); return *this; }
Expand Down
10 changes: 5 additions & 5 deletions Marlin/src/feature/bedlevel/mbl/mesh_bed_leveling.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,18 @@ class mesh_bed_leveling {
static float get_mesh_x(const uint8_t i) { return index_to_xpos[i]; }
static float get_mesh_y(const uint8_t i) { return index_to_ypos[i]; }

static int8_t cell_index_x(const_float_t x) {
static uint8_t cell_index_x(const_float_t x) {
int8_t cx = (x - (MESH_MIN_X)) * RECIPROCAL(MESH_X_DIST);
return constrain(cx, 0, GRID_MAX_CELLS_X - 1);
}
static int8_t cell_index_y(const_float_t y) {
static uint8_t cell_index_y(const_float_t y) {
int8_t cy = (y - (MESH_MIN_Y)) * RECIPROCAL(MESH_Y_DIST);
return constrain(cy, 0, GRID_MAX_CELLS_Y - 1);
}
static xy_int8_t cell_indexes(const_float_t x, const_float_t y) {
static xy_uint8_t cell_indexes(const_float_t x, const_float_t y) {
return { cell_index_x(x), cell_index_y(y) };
}
static xy_int8_t cell_indexes(const xy_pos_t &xy) { return cell_indexes(xy.x, xy.y); }
static xy_uint8_t cell_indexes(const xy_pos_t &xy) { return cell_indexes(xy.x, xy.y); }

static int8_t probe_index_x(const_float_t x) {
int8_t px = (x - (MESH_MIN_X) + 0.5f * (MESH_X_DIST)) * RECIPROCAL(MESH_X_DIST);
Expand All @@ -107,7 +107,7 @@ class mesh_bed_leveling {
static float get_z_offset() { return z_offset; }

static float get_z_correction(const xy_pos_t &pos) {
const xy_int8_t ind = cell_indexes(pos);
const xy_uint8_t ind = cell_indexes(pos);
const float x1 = index_to_xpos[ind.x], x2 = index_to_xpos[ind.x+1],
y1 = index_to_ypos[ind.y], y2 = index_to_ypos[ind.y+1],
z1 = calc_z0(pos.x, x1, z_values[ind.x][ind.y ], x2, z_values[ind.x+1][ind.y ]),
Expand Down
12 changes: 6 additions & 6 deletions Marlin/src/feature/bedlevel/ubl/ubl.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,26 +141,26 @@ class unified_bed_leveling {
return FLOOR((y - (MESH_MIN_Y)) * RECIPROCAL(MESH_Y_DIST));
}

static int8_t cell_index_x_valid(const_float_t x) {
static bool cell_index_x_valid(const_float_t x) {
return WITHIN(cell_index_x_raw(x), 0, GRID_MAX_CELLS_X - 1);
}

static int8_t cell_index_y_valid(const_float_t y) {
static bool cell_index_y_valid(const_float_t y) {
return WITHIN(cell_index_y_raw(y), 0, GRID_MAX_CELLS_Y - 1);
}

static int8_t cell_index_x(const_float_t x) {
static uint8_t cell_index_x(const_float_t x) {
return constrain(cell_index_x_raw(x), 0, GRID_MAX_CELLS_X - 1);
}

static int8_t cell_index_y(const_float_t y) {
static uint8_t cell_index_y(const_float_t y) {
return constrain(cell_index_y_raw(y), 0, GRID_MAX_CELLS_Y - 1);
}

static xy_int8_t cell_indexes(const_float_t x, const_float_t y) {
static xy_uint8_t cell_indexes(const_float_t x, const_float_t y) {
return { cell_index_x(x), cell_index_y(y) };
}
static xy_int8_t cell_indexes(const xy_pos_t &xy) { return cell_indexes(xy.x, xy.y); }
static xy_uint8_t cell_indexes(const xy_pos_t &xy) { return cell_indexes(xy.x, xy.y); }

static int8_t closest_x_index(const_float_t x) {
const int8_t px = (x - (MESH_MIN_X) + (MESH_X_DIST) * 0.5) * RECIPROCAL(MESH_X_DIST);
Expand Down
8 changes: 4 additions & 4 deletions Marlin/src/feature/bedlevel/ubl/ubl_motion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
const xyze_pos_t &start = current_position, &end = destination;
#endif

const xy_int8_t istart = cell_indexes(start), iend = cell_indexes(end);
const xy_uint8_t istart = cell_indexes(start), iend = cell_indexes(end);

// A move within the same cell needs no splitting
if (istart == iend) {
Expand Down Expand Up @@ -108,7 +108,7 @@

const xy_float_t dist = end - start;
const xy_bool_t neg { dist.x < 0, dist.y < 0 };
const xy_int8_t ineg { int8_t(neg.x), int8_t(neg.y) };
const xy_uint8_t ineg { uint8_t(neg.x), uint8_t(neg.y) };
const xy_float_t sign { neg.x ? -1.0f : 1.0f, neg.y ? -1.0f : 1.0f };
const xy_int8_t iadd { int8_t(iend.x == istart.x ? 0 : sign.x), int8_t(iend.y == istart.y ? 0 : sign.y) };

Expand All @@ -131,7 +131,7 @@
const bool inf_normalized_flag = isinf(e_normalized_dist);
#endif

xy_int8_t icell = istart;
xy_uint8_t icell = istart;

const float ratio = dist.y / dist.x, // Allow divide by zero
c = start.y - ratio * start.x;
Expand Down Expand Up @@ -252,7 +252,7 @@
* Generic case of a line crossing both X and Y Mesh lines.
*/

xy_int8_t cnt = (istart - iend).ABS();
xy_uint8_t cnt = istart.diff(iend);

icell += ineg;

Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/libs/numtostr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ template <typename T1, typename T2>
constexpr char DIGIMOD(const T1 n, const T2 f) { return DIGIT((n / f) % 10); }

template <typename T1, typename T2>
constexpr char RJDIGIT(const T1 n, const T2 f) { return (n >= f ? DIGIMOD(n, f) : ' '); }
constexpr char RJDIGIT(const T1 n, const T2 f) { return (n >= (T1)f ? DIGIMOD(n, f) : ' '); }

template <typename T>
constexpr char MINUSOR(T &n, const char alt) { return (n >= 0) ? alt : (n = -n) ? '-' : '-'; }
Expand Down
3 changes: 0 additions & 3 deletions Marlin/src/module/motion.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,6 @@ void restore_feedrate_and_scaling();
typedef bits_t(NUM_AXES) main_axes_bits_t;
constexpr main_axes_bits_t main_axes_mask = _BV(NUM_AXES) - 1;

typedef bits_t(NUM_AXES + EXTRUDERS) e_axis_bits_t;
constexpr e_axis_bits_t e_axis_mask = (_BV(EXTRUDERS) - 1) << NUM_AXES;

void set_axis_is_at_home(const AxisEnum axis);

#if HAS_ENDSTOPS
Expand Down
3 changes: 3 additions & 0 deletions Marlin/src/module/stepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ typedef struct {
};
} stepper_flags_t;

typedef bits_t(NUM_AXES + E_STATES) e_axis_bits_t;
constexpr e_axis_bits_t e_axis_mask = (_BV(E_STATES) - 1) << NUM_AXES;

// All the stepper enable pins
constexpr pin_t ena_pins[] = {
NUM_AXIS_LIST_(X_ENABLE_PIN, Y_ENABLE_PIN, Z_ENABLE_PIN, I_ENABLE_PIN, J_ENABLE_PIN, K_ENABLE_PIN, U_ENABLE_PIN, V_ENABLE_PIN, W_ENABLE_PIN)
Expand Down

0 comments on commit 0c14aa0

Please sign in to comment.