Canola  0.8.D001
lib/number/positive_plus_positive.cc
Go to the documentation of this file.
00001 //
00002 // canola - canon canola 1614p emulator
00003 // Copyright (C) 2012 Peter Miller
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License, version 3, as
00007 // published by the Free Software Foundation.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License along
00015 // with this program. If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 
00018 #include <lib/ac/assert.h>
00019 
00020 #include <lib/number.h>
00021 
00022 
00023 number
00024 number::positive_plus_positive(const number &lhs, const number &rhs)
00025 {
00026     assert(!lhs.negative);
00027     assert(!rhs.negative);
00028 
00029     // short circuit the easy cases
00030     if (lhs.value.is_zero())
00031         return rhs;
00032     if (rhs.value.is_zero())
00033         return lhs;
00034 
00035     // In this case, both lhs and rhs are positive, meaning we
00036     // can do it with Z arithmetic alone.
00037     number_z t1(lhs.value);
00038     number_z t2(rhs.value);
00039     unsigned t3dec = max(lhs.decimal, rhs.decimal);
00040     if (t3dec > lhs.decimal)
00041         t1 = t1.shift_left(t3dec - lhs.decimal);
00042     if (t3dec > rhs.decimal)
00043         t2 = t2.shift_left(t3dec - rhs.decimal);
00044     number_z t3(t1 + t2);
00045 
00046     // Instruction Manual, p. 21:
00047     // "If the number of digits in the result exceeds 16, the leftmost
00048     // digit priority system functions and excess digits at the right
00049     // are dropped."
00050     truncate_floating_operation(t3, t3dec);
00051 
00052     return number(t3, t3dec);
00053 }
00054 
00055 
00056 // vim: set ts=8 sw=4 et :