From 7155e47537e3707d42a675d2603386963c6d9156 Mon Sep 17 00:00:00 2001 From: Stefan Tauner Date: Fri, 13 Apr 2012 01:45:02 +0200 Subject: Support shifts with keyboard This patch also changes the shift operation itself to use MPNumber for the multiplier too. Previously a signed int was used, which led to "interesting" results for bigger numbers. This was hidden because the GUI did not allow shifts with more than 15 places. Signed-off-by: Stefan Tauner --- src/math-buttons.c | 4 ++-- src/mp-binary.c | 15 ++++++++------- src/mp-equation-lexer.l | 4 ++++ src/mp-equation-parser.y | 3 +++ 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/math-buttons.c b/src/math-buttons.c index fc13976..19ca08e 100644 --- a/src/math-buttons.c +++ b/src/math-buttons.c @@ -252,10 +252,10 @@ static ButtonData button_data[] = { N_("Undo [Ctrl+Z]")}, {"shift_left", NULL, ACTION, /* Tooltip for the shift left button */ - N_("Shift Left")}, + N_("Shift Left [<<]")}, {"shift_right", NULL, ACTION, /* Tooltip for the shift right button */ - N_("Shift Right")}, + N_("Shift Right [>>]")}, {"finc_compounding_term", NULL, FUNCTION, /* Tooltip for the compounding term button */ N_("Compounding Term")}, diff --git a/src/mp-binary.c b/src/mp-binary.c index e4cedba..f85b3ee 100644 --- a/src/mp-binary.c +++ b/src/mp-binary.c @@ -167,7 +167,9 @@ mp_mask(const MPNumber *x, int wordlen, MPNumber *z) void mp_shift(const MPNumber *x, int count, MPNumber *z) { - int i, multiplier = 1; + int i; + MPNumber multiplier; + mp_set_from_integer(1, &multiplier); if (!mp_is_integer(x)) { /* Translators: Error displayed when bit shift attempted on non-integer values */ @@ -177,15 +179,14 @@ mp_shift(const MPNumber *x, int count, MPNumber *z) if (count >= 0) { for (i = 0; i < count; i++) - multiplier *= 2; - mp_multiply_integer(x, multiplier, z); + mp_multiply_integer(&multiplier, 2, &multiplier); + mp_multiply(x, &multiplier, z); } else { - MPNumber temp; for (i = 0; i < -count; i++) - multiplier *= 2; - mp_divide_integer(x, multiplier, &temp); - mp_floor(&temp, z); + mp_multiply_integer(&multiplier, 2, &multiplier); + mp_divide(x, &multiplier, z); + mp_floor(z, z); } } diff --git a/src/mp-equation-lexer.l b/src/mp-equation-lexer.l index bbc9765..74eb7d5 100644 --- a/src/mp-equation-lexer.l +++ b/src/mp-equation-lexer.l @@ -75,6 +75,8 @@ MOD [mM][oO][dD] AND "∧"|[aA][nN][dD] OR "∨"|[oO][rR] XOR "⊻"|"⊕"|[xX][oO][rR] +LSHIFT "<<" +RSHIFT ">>" NOT "¬"|"~"|[nN][oO][tT] RE "⃰ℜ" IM "ℑ" @@ -94,6 +96,8 @@ IN [iI][nN] "√" {return tROOT;} "∛" {return tROOT3;} "∜" {return tROOT4;} +{LSHIFT} {return tLSHIFT;} +{RSHIFT} {return tRSHIFT;} {NOT} {return tNOT;} {AND} {return tAND;} {OR} {return tOR;} diff --git a/src/mp-equation-parser.y b/src/mp-equation-parser.y index 064f90f..bb623cd 100644 --- a/src/mp-equation-parser.y +++ b/src/mp-equation-parser.y @@ -182,6 +182,7 @@ static void do_conversion(yyscan_t yyscanner, const MPNumber *x, const char *x_u %left UNARY_PLUS %left tADD tSUBTRACT %left tAND tOR tXOR tXNOR +%left tLSHIFT tRSHIFT %left tMULTIPLY tDIVIDE tMOD MULTIPLICATION %left tNOT %left tROOT tROOT3 tROOT4 @@ -242,6 +243,8 @@ exp: | exp tOR exp %prec BOOLEAN_OPERATOR {mp_or(&$1, &$3, &$$);} | exp tXOR exp %prec BOOLEAN_OPERATOR {mp_xor(&$1, &$3, &$$);} | tNUMBER {mp_set_from_mp(&$1, &$$);} +| exp tLSHIFT exp {mp_shift(&$1, mp_cast_to_int(&$3), &$$);} +| exp tRSHIFT exp {mp_shift(&$1, -mp_cast_to_int(&$3), &$$);} ; -- cgit v1.2.1