Преглед изворни кода

Least stack-usage self-contained ftostr32np()

This is the optimal code for a self-contained formatter, although the
original code is crafty in being smaller and simpler, and can be
evaluated as using the original output as a scratch pad for state,
making the final formatter more straightforward. While this code is
longer, all code-paths are minimal.
Scott Lahteine пре 10 година
родитељ
комит
449dad71f2
1 измењених фајлова са 41 додато и 8 уклоњено
  1. 41
    8
      Marlin/ultralcd.cpp

+ 41
- 8
Marlin/ultralcd.cpp Прегледај датотеку

@@ -1498,16 +1498,49 @@ char *ftostr12ns(const float &x)
1498 1498
 
1499 1499
 //  convert float to space-padded string with -_23.4_ format
1500 1500
 char *ftostr32np(const float &x) {
1501
-  char *c = ftostr32(x);
1502
-  if (c[0] == '0' || c[0] == '-') {
1503
-    if (c[0] == '0') c[0] = ' ';
1504
-    if (c[1] == '0') c[1] = ' ';
1501
+  long xx = abs(x * 100);
1502
+  uint8_t dig;
1503
+
1504
+  if (x < 0) { // negative val = -_0
1505
+    conv[0] = '-';
1506
+    dig = (xx / 1000) % 10;
1507
+    conv[1] = dig ? '0' + dig : ' ';
1508
+  }
1509
+  else { // positive val = __0
1510
+    dig = (xx / 10000) % 10;
1511
+    if (dig) {
1512
+      conv[0] = '0' + dig;
1513
+      conv[1] = '0' + (xx / 1000) % 10;
1514
+    }
1515
+    else {
1516
+      conv[0] = ' ';
1517
+      dig = (xx / 1000) % 10;
1518
+      conv[1] = dig ? '0' + dig : ' ';
1519
+    }
1520
+  }
1521
+
1522
+  conv[2] = '0' + (xx / 100) % 10; // lsd always
1523
+
1524
+  dig = xx % 10;
1525
+  if (dig) { // 2 decimal places
1526
+    conv[5] = '0' + dig;
1527
+    dig = (xx / 10) % 10;
1528
+    conv[4] = '0' + dig;
1529
+    conv[3] = '.';
1505 1530
   }
1506
-  if (c[5] == '0') {
1507
-    c[5] = ' ';
1508
-    if (c[4] == '0') c[4] = c[3] = ' ';
1531
+  else { // 1 or 0 decimal place
1532
+    dig = (xx / 10) % 10;
1533
+    if (dig) {
1534
+      conv[4] = '0' + dig;
1535
+      conv[3] = '.';
1536
+    }
1537
+    else {
1538
+      conv[3] = conv[4] = ' ';
1539
+    }
1540
+    conv[5] = ' ';
1509 1541
   }
1510
-  return c;
1542
+  conv[6] = '\0';
1543
+  return conv;
1511 1544
 }
1512 1545
 
1513 1546
 char *itostr31(const int &xx)

Loading…
Откажи
Сачувај