This is for exploring and demonstrating ways to extend the available integer math in C. Cコンパイラが提供する整数を拡張するための探険用のソースコードです。
Rev. | 056c8c9d381867aa3d96bcf05e9a165e42771547 |
---|---|
サイズ | 2,672 バイト |
日時 | 2013-07-29 11:34:41 |
作者 | Joel Matthew Rees |
ログメッセージ | Where I ran out of time last week. Demonstrats add/sub/mul/bitdiv.
|
/* Subtraction tester 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 <stdio.h>
#include "nibBit.h"
int main( int argc, char * argv[] )
{ unsigned i, j;
long bad = 0;
long rangeMiss = 0;
long start = 0;
long limit = 0xffff;
/* long rangeCount = limit - start + 1; */
long long runningCount = 0;
if ( argc > 2 )
{ char * scan;
start = strtoul( argv[ 1 ], &scan, 0 );
limit = strtoul( argv[ 2 ], &scan, 0 );
}
else if ( argc > 1 )
{ char * scan;
limit = strtoul( argv[ 1 ], &scan, 0 );
}
initMyStack();
for ( i = start; i <= limit; ++i )
{ for ( j = start; j <= limit; ++j )
{ unsigned difference = i - j;
unsigned synthetic;
( * --mySP ) = (uchar_t) i;
( * --mySP ) = (uchar_t) ( i >> BYTEBITS );
( * --mySP ) = (uchar_t) j;
( * --mySP ) = (uchar_t) ( j >> BYTEBITS );
/* printf( "@%p:%02x,%02x,%02x,%02x\n", mySP, mySP[ 3 ], mySP[ 2 ], mySP[ 1 ], mySP[ 0 ] ); */
nibDSub();
/* printf( "@%p:%02x,%02x\n", mySP, mySP[ 1 ], mySP[ 0 ] ); */
/* Why doesn't the compiler seem to understand this?
// Intel's byte order shenanigans?
// Oh. Bit or is not a time point, and some stupid says right to left.
// Yeah, blame it on Intel's little-endian.
// synthetic = ( ( * mySP++ ) << BYTEBITS ) | ( * mySP++ );
*/
synthetic = ( * mySP++ ) << BYTEBITS;
synthetic |= ( * mySP++ );
++runningCount;
if ( difference != synthetic )
{ if ( ( difference > 0xffff ) && ( ( difference & 0xffff ) == synthetic ) )
{ rangeMiss += 1;
/*
printf( "out-of-range(%ld): ", rangeMiss );
printf( "%u - %u = %u synth: %u\n",
i, j, difference, synthetic );
printf( "0x%05x - 0x%05x = 0x%05x synth: 0x%05x\n",
i, j, difference, synthetic );
*/
}
else
{ bad += 1;
printf( "*** bad(%ld): ", bad );
printf( "%u - %u = %u synth: %u\n",
i, j, difference, synthetic );
printf( "0x%05x - 0x%05x = 0x%05x synth: 0x%05x\n",
i, j, difference, synthetic );
}
}
}
printf( "i: %x -- Range miss: %ld, bad count: %ld (of %lld)\n",
i, rangeMiss, bad, runningCount );
}
return bad;
}