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 void GetAval( CVector& v ) {
00365 v.aval = aval;
00366 bval = 0;
00367 }
00372 void GetBval( CVector& v ) {
00373 v.aval = bval;
00374 bval = 0;
00375 }
00376
00380 friend void Add( CVector* r, CVector* a, CVector* b );
00381 friend void Sub( CVector* r, CVector* a, CVector* b );
00382 friend void Mul( CVector* r, CVector* a, CVector* b );
00383 friend void Div( CVector* r, CVector* a, CVector* b );
00384 friend void Pow( CVector* r, CVector* a, CVector* b );
00385 friend void Lsha( CVector* r, CVector* a, CVector* b );
00386 friend void Rsha( CVector* r, CVector* a, CVector* b );
00387 friend void Lsh( CVector* r, CVector* a, CVector* b );
00388 friend void Rsh( CVector* r, CVector* a, CVector* b );
00389 friend void Cat( CVector* r, CVector* a, CVector* b );
00390 friend void Rep( CVector* r, CVector* a, CVector* b );
00391 friend void Mod( CVector* r, CVector* a, CVector* b );
00392 friend void And( CVector* r, CVector* a, CVector* b );
00393 friend void Or( CVector* r, CVector* a, CVector* b );
00394 friend void Xor( CVector* r, CVector* a, CVector* b );
00395 friend void Xnor( CVector* r, CVector* a, CVector* b );
00396 friend void Com( CVector* r, CVector* a );
00397 friend void Neg( CVector* r, CVector* a );
00398 friend void Plus( CVector* r, CVector* a );
00399 friend void Not( CVector* r, CVector* a );
00400 friend void Le( CVector* r, CVector* a, CVector* b );
00401 friend void Lt( CVector* r, CVector* a, CVector* b );
00402 friend void Ge( CVector* r, CVector* a, CVector* b );
00403 friend void Gt( CVector* r, CVector* a, CVector* b );
00404 friend void Eq( CVector* r, CVector* a, CVector* b );
00405 friend void Ne( CVector* r, CVector* a, CVector* b );
00406 friend void Ceq( CVector* r, CVector* a, CVector* b );
00407 friend void Cne( CVector* r, CVector* a, CVector* b );
00408 friend void Lor( CVector* r, CVector* a, CVector* b );
00409 friend void Land( CVector* r, CVector* a, CVector* b );
00410 friend void Rand( CVector* r, CVector* a );
00411 friend void Ror( CVector* r, CVector* a );
00412 friend void Rnand( CVector* r, CVector* a );
00413 friend void Rnor( CVector* r, CVector* a );
00414 friend void Rxor( CVector* r, CVector* a );
00415 friend void Rxnor( CVector* r, CVector* a );
00416 friend void Hook( CVector* r, CVector* c, CVector* a, CVector* b );
00420 int IsWidthConstant() { return TRUE; }
00421 int IsWidthVolatile() { return FALSE; }
00422 int IsWidthEvaluateable() { return TRUE; }
00423 CNode* GetWidthExp();
00424 virtual NodeType_t GetNodeType( void );
00425 private:
00426 void Adjust( void );
00427 void GrowBuffer( int bytes )
00428 {
00429 if( bytes <= bufferSize ) {
00430 return;
00431 }
00432 if( buffer != NULL ) {
00433 shell_xfree( buffer, bufferSize );
00434 }
00435 bufferSize = bytes * 2;
00436 buffer = (char*) shell_xmalloc( bufferSize );
00437 }
00438 void SetHeap( CObstack* aHeap ) {
00439 aval.SetHeap( aHeap );
00440 bval.SetHeap( aHeap );
00441 }
00448 void* operator new(size_t size, CObstack* heap) {
00449 return heap->Alloc( size );
00450 }
00454 };
00455
00461 inline void Le( CVector* r, double* a, double* b )
00462 {
00463 r->LoadINT32( *a <= *b );
00464 }
00465
00466
00467 inline void Le( CVector* r, INT32 a, INT32 b )
00468 {
00469 r->LoadINT32( a <= b );
00470 }
00471
00472 inline void Lt( CVector* r, double* a, double* b )
00473 {
00474 r->LoadINT32( *a < *b );
00475 }
00476
00477
00478 inline void Lt( CVector* r, INT32 a, INT32 b )
00479 {
00480 r->LoadINT32( a < b );
00481 }
00482
00483 inline void Ge( CVector* r, double* a, double* b )
00484 {
00485 r->LoadINT32( *a >= *b );
00486 }
00487
00488
00489 inline void Ge( CVector* r, INT32 a, INT32 b )
00490 {
00491 r->LoadINT32( a >= b );
00492 }
00493
00494 inline void Gt( CVector* r, double* a, double* b )
00495 {
00496 r->LoadINT32( *a > *b );
00497 }
00498
00499
00500 inline void Gt( CVector* r, INT32 a, INT32 b )
00501 {
00502 r->LoadINT32( a > b );
00503 }
00504
00505
00506 inline void Ne( CVector* r, double* a, double* b )
00507 {
00508 r->LoadINT32( *a != *b );
00509 }
00510
00511 inline void Ne( CVector* r, INT32 a, INT32 b )
00512 {
00513 r->LoadINT32( a != b );
00514 }
00515
00516 inline void Eq( CVector* r, double* a, double* b )
00517 {
00518 r->LoadINT32( *a == *b );
00519 }
00520
00521 inline void Eq( CVector* r, INT32 a, INT32 b )
00522 {
00523 r->LoadINT32( a == b );
00524 }
00525
00526 inline void Cne( CVector*, double*, double* )
00527 {
00528 fatal( NULL, "!== illegal for reals" );
00529 }
00530
00531 inline void Cne( CVector* r, INT32 a, INT32 b )
00532 {
00533 r->LoadINT32( a != b );
00534 }
00535
00536 inline void Ceq( CVector*, double*, double* )
00537 {
00538 fatal( NULL, "=== illegal for reals" );
00539 }
00540
00541 inline void Ceq( CVector* r, INT32 a, INT32 b )
00542 {
00543 r->LoadINT32( a == b );
00544 }
00545
00549 inline void LogicalValue( CVector* dst, CVector* a )
00550 {
00551 CVector zero(a->GetWidth());
00552 Ne( dst, a, &zero );
00553 }
00554
00555 inline void Hook( double* r, CVector* c, double* a, double* b )
00556 {
00557 CVector cond(1);
00558 LogicalValue( &cond, c );
00559 if( cond.HasXZ() ) {
00560 *r = 0;
00561 } else if( cond == 0) {
00562 *r = *b;
00563 } else {
00564 *r = *a;
00565 }
00566 }
00567
00568 inline void Cat( double*, double*, double* )
00569 {
00570 fatal( NULL, "=== illegal for reals" );
00571 }
00576 #endif // CVECTOR_HPP