- Added clog2 function

- negative numbers can now be displayed in 2's compliment binary
This commit is contained in:
Joe Crop
2022-12-06 11:10:01 -08:00
parent 42d8fea8c6
commit fc95028b57
20 changed files with 97 additions and 9 deletions

View File

@@ -1,6 +1,8 @@
#include "integer_list_controller.h"
#include <poincare/based_integer.h>
#include <poincare/opposite.h>
#include <poincare/integer.h>
#include <poincare/logarithm.h>
#include <poincare/empty_layout.h>
#include <poincare/factor.h>
#include "../app.h"
@@ -26,10 +28,30 @@ Integer::Base baseAtIndex(int index) {
void IntegerListController::setExpression(Poincare::Expression e) {
ExpressionsListController::setExpression(e);
static_assert(k_maxNumberOfRows >= k_indexOfFactorExpression + 1, "k_maxNumberOfRows must be greater than k_indexOfFactorExpression");
assert(!m_expression.isUninitialized() && m_expression.type() == ExpressionNode::Type::BasedInteger);
Integer integer = static_cast<BasedInteger &>(m_expression).integer();
for (int index = 0; index < k_indexOfFactorExpression; ++index) {
m_layouts[index] = integer.createLayout(baseAtIndex(index));
assert(!m_expression.isUninitialized() && m_expression.type() == ExpressionNode::Type::BasedInteger || (m_expression.type() == ExpressionNode::Type::Opposite && m_expression.childAtIndex(0).type() == ExpressionNode::Type::BasedInteger));
assert(!m_expression.isUninitialized());
if (m_expression.type() == ExpressionNode::Type::BasedInteger) {
Integer integer = static_cast<BasedInteger &>(m_expression).integer();
for (int index = 0; index < k_indexOfFactorExpression; ++index) {
m_layouts[index] = integer.createLayout(baseAtIndex(index));
}
}
else
{
Opposite b = static_cast<Opposite &>(m_expression);
Expression e = b.childAtIndex(0);
Integer childInt = static_cast<BasedInteger &>(e).integer();
childInt.setNegative(true);
Integer num_bits = Integer::CeilingLog2(childInt);
Integer integer = Integer::TwosComplementToBits(childInt, num_bits);
for (int index = 0; index < k_indexOfFactorExpression; ++index) {
if(baseAtIndex(index) == Integer::Base::Decimal) {
m_layouts[index] = childInt.createLayout(baseAtIndex(index));
} else {
m_layouts[index] = integer.createLayout(baseAtIndex(index));
}
}
}
// Computing factorExpression
Expression factor = Factor::Builder(m_expression.clone());

View File

@@ -109,7 +109,8 @@ const ToolboxMessageTree logicChildren[] = {
ToolboxMessageTree::Leaf(I18n::Message::LogicalBitClearCommandWithArg, I18n::Message::LogicalBitClear),
ToolboxMessageTree::Leaf(I18n::Message::LogicalBitFlipCommandWithArg, I18n::Message::LogicalBitFlip),
ToolboxMessageTree::Leaf(I18n::Message::LogicalBitsClearCommandWithArg, I18n::Message::LogicalBitsClear),
ToolboxMessageTree::Leaf(I18n::Message::TwosComplementToBitsCommandWithArg, I18n::Message::TwosComplementToBits)
ToolboxMessageTree::Leaf(I18n::Message::TwosComplementToBitsCommandWithArg, I18n::Message::TwosComplementToBits),
ToolboxMessageTree::Leaf(I18n::Message::CeilingLog2CommandWithArg, I18n::Message::CeilingLog2)
};
#if LIST_ARE_DEFINED

View File

@@ -511,3 +511,4 @@ LogicalRotateRightExplicitCommandWithArg = "ror(a,r,n)"
LogicalXor = "a XOR b"
LogicalXorCommandWithArg = "xor(a,r)"
TwosComplementToBitsCommandWithArg = "tc(a,n)"
CeilingLog2CommandWithArg = "clog2(a)"

View File

@@ -522,4 +522,5 @@ LogicalShiftRight = "Bitverschiebung rechts von a um s"
LogicalRotateLeft = "Rotieren von a um r Bit n. links"
LogicalRotateRight= "Rotieren von a um r Bit n. rechts"
TwosComplementToBits = "Äquivalent im Zweierkomplement"
CeilingLog2 = "Anzahl der Bits, die zum Speichern von a benötigt werden"
ExplicitNumberOfBits = "Explizite Bitbreite"

View File

@@ -522,4 +522,5 @@ LogicalShiftRight = "Logical shift right [a >> s]"
LogicalRotateLeft = "Rotate r bits of a to the left"
LogicalRotateRight= "Rotate r bits of a to the right"
TwosComplementToBits = "Two's complement equivalent"
CeilingLog2 = "Number of bits needed to store a"
ExplicitNumberOfBits = "Explicit number of bits"

View File

@@ -522,4 +522,5 @@ LogicalShiftRight = "Desplazamiento lógico derecha"
LogicalRotateLeft = "Gire r bits de a hacia izquierda"
LogicalRotateRight= "Gire r bits de a hacia derecha"
TwosComplementToBits = "Equivalente en complemento a dos"
CeilingLog2 = "Número de bits necesarios para almacenar a"
ExplicitNumberOfBits = "Número explícito de bits"

View File

@@ -526,4 +526,5 @@ LogicalShiftRight = "Décalage logique droite [a >> s]"
LogicalRotateLeft = "Rotation gauche de a par r bits"
LogicalRotateRight= "Rotation droite de a par r bits"
TwosComplementToBits = "Equivalent en complément à deux"
CeilingLog2 = "Nombre de bits nécessaires pour stocker a"
ExplicitNumberOfBits = "Nombre indiqué de bits"

View File

@@ -522,4 +522,5 @@ LogicalShiftRight = "Logikai eltolás jobbra [a >> s]"
LogicalRotateLeft = "Forog r bitek nak a balra"
LogicalRotateRight= "Forog r bitek nak a jobbra"
TwosComplementToBits = "Kettő komplementere egyenértékű"
CeilingLog2 = "Az a tárolásához szükséges bitek száma"
ExplicitNumberOfBits = "Explicit bitszám"

View File

@@ -522,4 +522,5 @@ LogicalShiftRight = "Spostamento logico a destra"
LogicalRotateLeft = "Ruota r bit di a verso sinistra"
LogicalRotateRight= "Ruota r bit di a verso destra"
TwosComplementToBits = "Equivalente in complemento di due"
CeilingLog2 = "Numero di bit necessari per memorizzare a"
ExplicitNumberOfBits = "Numero esplicito di bit"

View File

@@ -522,4 +522,5 @@ LogicalShiftRight = "Logische verschuiving naar rechts"
LogicalRotateLeft = "Draai r stukjes a naar links"
LogicalRotateRight= "Draai r stukjes a naar rechts"
TwosComplementToBits = "Tweeën vullen equivalent aan"
CeilingLog2 = "Aantal bits dat nodig is om a op te slaan"
ExplicitNumberOfBits = "Expliciet aantal bits"

View File

@@ -522,4 +522,5 @@ LogicalShiftRight = "Mudar lógica para a direita"
LogicalRotateLeft = "Girar r bits de a para a esquerda"
LogicalRotateRight= "Girar r bits de a para a direita"
TwosComplementToBits = "Complementar de dois equivalente"
CeilingLog2 = "Número de bits necessários para armazenar a"
ExplicitNumberOfBits = "Número explícito de bits"