• R/O
  • HTTP
  • SSH
  • HTTPS

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

This is for exploring and demonstrating ways to extend the available integer math in C. Cコンパイラが提供する整数を拡張するための探険用のソースコードです。


File Info

Rev. 056c8c9d381867aa3d96bcf05e9a165e42771547
Size 1,026 bytes
Time 2013-07-29 11:34:41
Author Joel Matthew Rees
Log Message

Where I ran out of time last week. Demonstrats add/sub/mul/bitdiv.
C's lack of an overflow target for math (especially division) makes it hard to expand.
But division is hard anyway, and the carry/overflow for the rest really is not that bad.

Content

/* Multiplication functions for an eight-bit framework 
// for testing various arithmetic techniques.
// Written by Joel Matthew Rees
// Copyright 2013, Joel Matthew Rees
// Distribution and use permitted under terms of the GPL v. 3,
// See the included file, LICENSE.TXT,
// or on-line at <http://www.gnu.org/licenses/>. 
*/

#include <limits.h>

#include "nibBit.h"


/* Push order: left right 
// return order: lsbyte msbyte
// (most significant on top-of-stack -- low address)
*/
void nibUMul( void )
{
   uchar_t leftHi = NIBHIDOWN( mySP[ 1 ] ); 
   uchar_t leftLo = NIBLO( mySP[ 1 ] );
   uchar_t rightHi = NIBHIDOWN( mySP[ 0 ] );
   uchar_t rightLo = NIBLO( mySP[ 0 ] );
   uchar_t rLo = leftLo * rightLo;
   uchar_t rMid1 = leftLo * rightHi;
   uchar_t rMid2 = leftHi * rightLo;
   uchar_t accm = NIBHIDOWN( rLo ) + NIBLO( rMid1 ) + NIBLO( rMid2 );

   mySP[ 1 ] = NIBLO( rLo ) | NIBLOUP( accm );
   accm >>= NIBCT;
   mySP[ 0 ] = accm + leftHi * rightHi 
               + NIBHIDOWN( rMid1 ) + NIBHIDOWN( rMid2 );
}