My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

math.h 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. /**
  24. * Optimized math functions for AVR
  25. */
  26. // intRes = longIn1 * longIn2 >> 24
  27. // uses:
  28. // A[tmp] to store 0
  29. // B[tmp] to store bits 16-23 of the 48bit result. The top bit is used to round the two byte result.
  30. // note that the lower two bytes and the upper byte of the 48bit result are not calculated.
  31. // this can cause the result to be out by one as the lower bytes may cause carries into the upper ones.
  32. // B A are bits 24-39 and are the returned value
  33. // C B A is longIn1
  34. // D C B A is longIn2
  35. //
  36. static FORCE_INLINE uint16_t MultiU24X32toH16(uint32_t longIn1, uint32_t longIn2) {
  37. uint8_t tmp1;
  38. uint8_t tmp2;
  39. uint16_t intRes;
  40. __asm__ __volatile__(
  41. A("clr %[tmp1]")
  42. A("mul %A[longIn1], %B[longIn2]")
  43. A("mov %[tmp2], r1")
  44. A("mul %B[longIn1], %C[longIn2]")
  45. A("movw %A[intRes], r0")
  46. A("mul %C[longIn1], %C[longIn2]")
  47. A("add %B[intRes], r0")
  48. A("mul %C[longIn1], %B[longIn2]")
  49. A("add %A[intRes], r0")
  50. A("adc %B[intRes], r1")
  51. A("mul %A[longIn1], %C[longIn2]")
  52. A("add %[tmp2], r0")
  53. A("adc %A[intRes], r1")
  54. A("adc %B[intRes], %[tmp1]")
  55. A("mul %B[longIn1], %B[longIn2]")
  56. A("add %[tmp2], r0")
  57. A("adc %A[intRes], r1")
  58. A("adc %B[intRes], %[tmp1]")
  59. A("mul %C[longIn1], %A[longIn2]")
  60. A("add %[tmp2], r0")
  61. A("adc %A[intRes], r1")
  62. A("adc %B[intRes], %[tmp1]")
  63. A("mul %B[longIn1], %A[longIn2]")
  64. A("add %[tmp2], r1")
  65. A("adc %A[intRes], %[tmp1]")
  66. A("adc %B[intRes], %[tmp1]")
  67. A("lsr %[tmp2]")
  68. A("adc %A[intRes], %[tmp1]")
  69. A("adc %B[intRes], %[tmp1]")
  70. A("mul %D[longIn2], %A[longIn1]")
  71. A("add %A[intRes], r0")
  72. A("adc %B[intRes], r1")
  73. A("mul %D[longIn2], %B[longIn1]")
  74. A("add %B[intRes], r0")
  75. A("clr r1")
  76. : [intRes] "=&r" (intRes),
  77. [tmp1] "=&r" (tmp1),
  78. [tmp2] "=&r" (tmp2)
  79. : [longIn1] "d" (longIn1),
  80. [longIn2] "d" (longIn2)
  81. : "cc"
  82. );
  83. return intRes;
  84. }
  85. // intRes = intIn1 * intIn2 >> 16
  86. // uses:
  87. // r26 to store 0
  88. // r27 to store the byte 1 of the 24 bit result
  89. static FORCE_INLINE uint16_t MultiU16X8toH16(uint8_t charIn1, uint16_t intIn2) {
  90. uint8_t tmp;
  91. uint16_t intRes;
  92. __asm__ __volatile__ (
  93. A("clr %[tmp]")
  94. A("mul %[charIn1], %B[intIn2]")
  95. A("movw %A[intRes], r0")
  96. A("mul %[charIn1], %A[intIn2]")
  97. A("add %A[intRes], r1")
  98. A("adc %B[intRes], %[tmp]")
  99. A("lsr r0")
  100. A("adc %A[intRes], %[tmp]")
  101. A("adc %B[intRes], %[tmp]")
  102. A("clr r1")
  103. : [intRes] "=&r" (intRes),
  104. [tmp] "=&r" (tmp)
  105. : [charIn1] "d" (charIn1),
  106. [intIn2] "d" (intIn2)
  107. : "cc"
  108. );
  109. return intRes;
  110. }