00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef CVECTOR_HPP
00032 #define CVECTOR_HPP
00033
00034 #include <stdio.h>
00035 #include <math.h>
00036 #include "glue.h"
00037 #include "cuint.h"
00038 #include "cdecl.h"
00039
00040
00058 class CVector
00059 {
00060 private:
00061 CUInt aval;
00062 CUInt bval;
00063 CUInt hook;
00064 INT32 width;
00065 int sized;
00066
00067 int based;
00068
00069 int unbased;
00070
00071 int preferredBase;
00072
00073 int _signed;
00074 int overflowed;
00075
00076 static int bufferSize;
00077 static char* buffer;
00078 public:
00085 static CVector* AllocFromHeap( CObstack* aHeap, int width ) {
00086 CVector* v = new(aHeap) CVector( width );
00087 v->SetHeap( aHeap );
00088 return v;
00089 }
00090
00095 CVector( INT32 aWidth );
00099 ~CVector();
00106 unsigned Hash();
00111 int HasXZ() { return bval != 0; }
00116 int HasZ( void ) { return (~aval & bval) != 0; }
00121 int HasX( void ) { return (aval & bval) != 0; }
00126 int IsNegative()
00127 { return _signed && ((aval>>(width-1)) & 1) != 0 && !HasXZ(); }
00132 void SetPreferredBase( int base ) { preferredBase = base; }
00137 int GetPreferredBase() { return preferredBase; }
00143 int Sized() { return sized; }
00149 void Sized( int sized ) { this->sized = sized; }
00155 int Based() { return based; }
00161 void Based( int based ) { this->based = based; }
00167 int Unbased() { return unbased; }
00173 void Unbased( int unbased ) { this->unbased = unbased; }
00178 int Signed() const { return _signed; }
00183 void Signed( int _signed ) { this->_signed = _signed; }
00189 int Overflowed() { return overflowed; }
00194 INT32 GetWidth( void );
00202 void SetWidth( INT32 newWidth );
00206 void SetToX();
00211 const CVector& operator=( const CVector& v );
00216 UINT32 operator=( UINT32 v );
00221 int LoadDecimal( const char* string );
00226 int LoadBinary( const char* string );
00231 int LoadOctal( const char* string );
00236 int LoadHex( const char* string );
00241 int LoadString( const char* string );
00248 char* GetVString( void );
00255 char* GetDecimal( void );
00262 char* GetBinary( void );
00269 char* GetOctal( void );
00276 char* GetHex( void );
00284 char* GetString( void );
00290 int operator==( CVector& v );
00296 int operator==( UINT32 i );
00302 int operator!=( CVector& v ) { return !(*this == v); }
00308 int operator!=( UINT32 i ) { return !(*this == i); }
00314 INT32 GetINT32( void )
00315 {
00316 if( IsNegative() ) {
00317 CVector n(GetWidth());
00318 n.Signed(1);
00319 Neg( &n, this );
00320 return -(INT32)n.aval.GetUINT32();
00321 }
00322
00323 return aval.GetUINT32();
00324 }
00330 INT64 GetINT64( void )
00331 {
00332 if( IsNegative() ) {
00333 CVector n(GetWidth());
00334 n.Signed(1);
00335 Neg( &n, this );
00336 return -(INT64)n.aval.GetUINT64();
00337 }
00338
00339 return aval.GetUINT64();
00340 }
00345 void LoadINT32( INT32 v )
00346 {
00347 aval = (unsigned int)v;
00348 bval = 0;
00349 }
00354 void LoadReal( double d );
00359 double GetReal();
00364 const CUInt& Aval() {
00365 return aval;
00366 }
00371 const CUInt& Bval() {
00372 return bval;
00373 }
00378 void Aval( const CUInt& v ) {
00379 aval = v;
00380 }
00385 void Bval( const CUInt& v ) {
00386 bval = v;
00387 }
00388
00392 friend void Add( CVector* r, CVector* a, CVector* b );
00393 friend void Sub( CVector* r, CVector* a, CVector* b );
00394 friend void Mul( CVector* r, CVector* a, CVector* b );
00395 friend void Div( CVector* r, CVector* a, CVector* b );
00396 friend void Pow( CVector* r, CVector* a, CVector* b );
00397 friend void Lsha( CVector* r, CVector* a, CVector* b );
00398 friend void Rsha( CVector* r, CVector* a, CVector* b );
00399 friend void Lsh( CVector* r, CVector* a, CVector* b );
00400 friend void Rsh( CVector* r, CVector* a, CVector* b );
00401 friend void Cat( CVector* r, CVector* a, CVector* b );
00402 friend void Rep( CVector* r, CVector* a, CVector* b );
00403 friend void Mod( CVector* r, CVector* a, CVector* b );
00404 friend void And( CVector* r, CVector* a, CVector* b );
00405 friend void Or( CVector* r, CVector* a, CVector* b );
00406 friend void Xor( CVector* r, CVector* a, CVector* b );
00407 friend void Xnor( CVector* r, CVector* a, CVector* b );
00408 friend void Com( CVector* r, CVector* a );
00409 friend void Neg( CVector* r, CVector* a );
00410 friend void Plus( CVector* r, CVector* a );
00411 friend void Not( CVector* r, CVector* a );
00412 friend void Le( CVector* r, CVector* a, CVector* b );
00413 friend void Lt( CVector* r, CVector* a, CVector* b );
00414 friend void Ge( CVector* r, CVector* a, CVector* b );
00415 friend void Gt( CVector* r, CVector* a, CVector* b );
00416 friend void Eq( CVector* r, CVector* a, CVector* b );
00417 friend void Ne( CVector* r, CVector* a, CVector* b );
00418 friend void Ceq( CVector* r, CVector* a, CVector* b );
00419 friend void Cne( CVector* r, CVector* a, CVector* b );
00420 friend void Lor( CVector* r, CVector* a, CVector* b );
00421 friend void Land( CVector* r, CVector* a, CVector* b );
00422 friend void Rand( CVector* r, CVector* a );
00423 friend void Ror( CVector* r, CVector* a );
00424 friend void Rnand( CVector* r, CVector* a );
00425 friend void Rnor( CVector* r, CVector* a );
00426 friend void Rxor( CVector* r, CVector* a );
00427 friend void Rxnor( CVector* r, CVector* a );
00428 friend void Hook( CVector* r, CVector* c, CVector* a, CVector* b );
00432 int IsWidthConstant() { return TRUE; }
00433 int IsWidthVolatile() { return FALSE; }
00434 int IsWidthEvaluateable() { return TRUE; }
00435 CNode* GetWidthExp();
00436 virtual NodeType_t GetNodeType( void );
00437 private:
00438 void Adjust( void );
00439 void GrowBuffer( int bytes )
00440 {
00441 if( bytes <= bufferSize ) {
00442 return;
00443 }
00444 if( buffer != NULL ) {
00445 shell_xfree( buffer, bufferSize );
00446 }
00447 bufferSize = bytes * 2;
00448 buffer = (char*) shell_xmalloc( bufferSize );
00449 }
00450 void SetHeap( CObstack* aHeap ) {
00451 aval.SetHeap( aHeap );
00452 bval.SetHeap( aHeap );
00453 }
00460 void* operator new(size_t size, CObstack* heap) {
00461 return heap->Alloc( size );
00462 }
00466 };
00467
00473 inline void Le( CVector* r, double* a, double* b )
00474 {
00475 r->LoadINT32( *a <= *b );
00476 }
00477
00478
00479 inline void Le( CVector* r, INT32 a, INT32 b )
00480 {
00481 r->LoadINT32( a <= b );
00482 }
00483
00484 inline void Lt( CVector* r, double* a, double* b )
00485 {
00486 r->LoadINT32( *a < *b );
00487 }
00488
00489
00490 inline void Lt( CVector* r, INT32 a, INT32 b )
00491 {
00492 r->LoadINT32( a < b );
00493 }
00494
00495 inline void Ge( CVector* r, double* a, double* b )
00496 {
00497 r->LoadINT32( *a >= *b );
00498 }
00499
00500
00501 inline void Ge( CVector* r, INT32 a, INT32 b )
00502 {
00503 r->LoadINT32( a >= b );
00504 }
00505
00506 inline void Gt( CVector* r, double* a, double* b )
00507 {
00508 r->LoadINT32( *a > *b );
00509 }
00510
00511
00512 inline void Gt( CVector* r, INT32 a, INT32 b )
00513 {
00514 r->LoadINT32( a > b );
00515 }
00516
00517
00518 inline void Ne( CVector* r, double* a, double* b )
00519 {
00520 r->LoadINT32( *a != *b );
00521 }
00522
00523 inline void Ne( CVector* r, INT32 a, INT32 b )
00524 {
00525 r->LoadINT32( a != b );
00526 }
00527
00528 inline void Eq( CVector* r, double* a, double* b )
00529 {
00530 r->LoadINT32( *a == *b );
00531 }
00532
00533 inline void Eq( CVector* r, INT32 a, INT32 b )
00534 {
00535 r->LoadINT32( a == b );
00536 }
00537
00538 inline void Cne( CVector*, double*, double* )
00539 {
00540 fatal( NULL, "!== illegal for reals" );
00541 }
00542
00543 inline void Cne( CVector* r, INT32 a, INT32 b )
00544 {
00545 r->LoadINT32( a != b );
00546 }
00547
00548 inline void Ceq( CVector*, double*, double* )
00549 {
00550 fatal( NULL, "=== illegal for reals" );
00551 }
00552
00553 inline void Ceq( CVector* r, INT32 a, INT32 b )
00554 {
00555 r->LoadINT32( a == b );
00556 }
00557
00561 inline void LogicalValue( CVector* dst, CVector* a )
00562 {
00563 CVector zero(a->GetWidth());
00564 Ne( dst, a, &zero );
00565 }
00566
00567 inline void Hook( double* r, CVector* c, double* a, double* b )
00568 {
00569 CVector cond(1);
00570 LogicalValue( &cond, c );
00571 if( cond.HasXZ() ) {
00572 *r = 0;
00573 } else if( cond == 0) {
00574 *r = *b;
00575 } else {
00576 *r = *a;
00577 }
00578 }
00579
00580 inline void Cat( double*, double*, double* )
00581 {
00582 fatal( NULL, "=== illegal for reals" );
00583 }
00588 #endif // CVECTOR_HPP