baud@GATECH.EDU (Kurt Baudendistel) (10/14/88)
BUG REPORT 16 BEHAVIOR: if the abs function takes its argument by value "abs(Fix x)" it works correctly. if, however, it takes its argument by reference "abs(Fix& x)", the program bombs in the Fix destructor. the abs function is defined in Fix.h. the version of Fix.h given has it defined to take its argument by reference. program output with value argument: Fix(35,.1) len = 35 siz = 3 ref = 1 man = cccccccc000 val = 0.1 abs(Fix(35,.1)) len = 35 siz = 3 ref = 1 man = cccccccc000 val = 0.1 program output with reference argument: Fix(35,.1) len = 35 siz = 3 ref = 1 man = cccccccc000 val = 0.1 abs(Fix(35,.1)) Memory fault - core dumped COMPILER VERSION: GNU C++ compiler driver, version 1.25.0 INPUT FILES: bug.cc Fix.h Fix.cc type.h COMMAND LINE: g++ -v -g bug.cc Fix.cc FILE NAMES: tm.h = tm-vax.h md = vax.md MACHINE: vax 11/780 with BRL UNIX (4.2 BSD) SOURCE FILES: ---------------------------------------------------------------------------- // bug.cc #define make(x) cout << #x << "\n"; show(x) #include <Fix.h> main() { make(Fix(35,.1)); make(abs(Fix(35,.1))); } ---------------------------------------------------------------------------- // Fix.h #ifndef _Fix_h #define _Fix_h 1 #include <stream.h> #include <std.h> #include <stddef.h> #include <type.h> #include <Integer.h> #define _Fix_min_length 1 #define _Fix_max_length 65535 #define _Fix_min_value -1.0 #define _Fix_max_value 1.0 struct _Frep // internal Fix representation { uint16 len; // length in bits uint16 siz; // allocated storage int16 ref; // reference count uint16 s[1]; // start of ushort array represention }; typedef struct _Frep* _Fix; extern _Frep _Frep_0; extern _Frep _Frep_m1; extern _Frep _Frep_quotient_bump; extern uint16 Fix_default_length; class Fix { _Fix rep; Fix(_Fix); void unique(); void copy(int, uint16*); public: Fix(); Fix(Fix&); Fix(double&); Fix(long); Fix(long, Fix&); Fix(long, double&); ~Fix(); Fix operator = (Fix&); friend int operator == (Fix&, Fix& ); friend int operator != (Fix&, Fix&); friend int operator < (Fix&, Fix&); friend int operator <= (Fix&, Fix&); friend int operator > (Fix&, Fix&); friend int operator >= (Fix&, Fix&); Fix& operator + (); Fix operator - (); friend Fix operator + (Fix&, Fix&); friend Fix operator - (Fix&, Fix&); friend Fix operator * (Fix&, Fix&); friend Fix operator / (Fix&, Fix&); friend Fix operator * (Fix&, long); friend Fix operator * (long, Fix&); friend Fix operator % (Fix&, long); friend Fix operator << (Fix&, long); friend Fix operator >> (Fix&, long); friend Fix operator <? (Fix&, Fix&); // min friend Fix operator >? (Fix&, Fix&); // max Fix operator += (Fix&); Fix operator -= (Fix&); Fix operator *= (Fix&); Fix operator /= (Fix&); Fix operator *= (long); Fix operator %= (long); Fix operator <<=(long); Fix operator >>=(long); friend char* Ftoa(Fix&, int width = 8); friend Fix atoF(const char*, long len = 32); friend istream& operator >> (istream&, Fix&); friend ostream& operator << (ostream&, Fix&); // built-in functions friend Fix abs(Fix&); // absolute value friend int sgn(Fix&); // -1, 0, +1 friend Integer mantissa(Fix&); // integer representation friend double value(Fix&); // double value friend long length(Fix&); // field length friend void show(Fix&); // show contents // error handlers void error(char* msg); // error handler void range_error(char* msg); // range error handler // internal class functions friend void mask(_Fix); friend int compare(_Fix, _Fix = &_Frep_0); friend _Fix new_Fix(uint16); friend _Fix new_Fix(uint16, _Fix); friend _Fix new_Fix(uint16, double); friend _Fix copy(_Fix, _Fix); friend _Fix negate(_Fix, _Fix = NULL); friend _Fix add(_Fix, _Fix, _Fix = NULL); friend _Fix subtract(_Fix, _Fix, _Fix = NULL); friend _Fix multiply(_Fix, _Fix, _Fix = NULL); friend _Fix multiply(_Fix, long, _Fix = NULL); friend _Fix divide(_Fix, _Fix, _Fix = NULL, _Fix = NULL); friend _Fix shift(_Fix, long, _Fix = NULL); // non-operator versions for user friend void negate(Fix& x, Fix& r); friend void add(Fix& x, Fix& y, Fix& r); friend void subtract(Fix& x, Fix& y, Fix& r); friend void multiply(Fix& x, Fix& y, Fix& r); friend void divide(Fix& x, Fix& y, Fix& q, Fix& r); friend void shift(Fix& x, long y, Fix& r); }; // error handlers extern void default_Fix_error_handler(char*), default_Fix_range_error_handler(char*); extern one_arg_error_handler_t Fix_error_handler, Fix_range_error_handler; extern one_arg_error_handler_t set_Fix_error_handler(one_arg_error_handler_t f), set_Fix_range_error_handler(one_arg_error_handler_t f); typedef void (*Fix_peh)(_Fix&); extern Fix_peh Fix_overflow_handler; extern void Fix_overflow_saturate(_Fix&), Fix_overflow_wrap(_Fix&), Fix_overflow_warning_saturate(_Fix&), Fix_overflow_warning(_Fix&), Fix_overflow_error(_Fix&); extern Fix_peh set_overflow_handler(Fix_peh); extern long Fix_set_default_length(long); //#ifdef __OPTIMIZE__ inline void Fix::unique() { if ( rep->ref > 1 ) { rep->ref--; rep = new_Fix(rep->len,rep); } } inline void Fix::copy(int l, uint16* t) { uint16 *s = rep->s; while ( l-- ) *s++ = *t++; } inline void mask (_Fix x) { int n = x->len & 0x0f; if ( n ) x->s[x->siz - 1] &= 0xffff0000 >> n; } inline _Fix copy(_Fix from, _Fix to) { uint16 *ts = to->s, *fs = from->s; int ilim = to->siz < from->siz ? to->siz : from->siz; for ( int i=0; i < ilim; i++ ) *ts++ = *fs++; for ( ; i < to->siz; i++ ) *ts++ = 0; mask(to); return to; } inline Fix::Fix(_Fix f) { rep = f; } inline Fix::Fix() { rep = new_Fix(Fix_default_length); } inline Fix::Fix(long len) { if ( len < _Fix_min_length || len > _Fix_max_length ) error("illegal length in declaration"); rep = new_Fix((uint16 )len); } inline Fix::Fix(double& d) { rep = new_Fix(Fix_default_length,d); } inline Fix::Fix(Fix& y) { rep = y.rep, rep->ref++; } inline Fix::Fix(long len, Fix& y) { if ( len < _Fix_min_length || len > _Fix_max_length ) error("illegal length in declaration"); rep = new_Fix((uint16 )len,y.rep); } inline Fix::Fix(long len, double& d) { if ( len < _Fix_min_length || len > _Fix_max_length ) error("illegal length in declaration"); rep = new_Fix((uint16 )len,d); } inline Fix::~Fix() { if ( --rep->ref <= 0 ) delete rep; } inline Fix Fix::operator = (Fix& y) { if ( --rep->ref <= 0 ) delete rep; if ( rep->len == y.rep->len ) rep = y.rep, rep->ref++; else copy(rep->len,y.rep->s); return *this; } inline int operator == (Fix& x, Fix& y) { return compare(x.rep, y.rep) == 0; } inline int operator != (Fix& x, Fix& y) { return compare(x.rep, y.rep) != 0; } inline int operator < (Fix& x, Fix& y) { return compare(x.rep, y.rep) < 0; } inline int operator <= (Fix& x, Fix& y) { return compare(x.rep, y.rep) <= 0; } inline int operator > (Fix& x, Fix& y) { return compare(x.rep, y.rep) > 0; } inline int operator >= (Fix& x, Fix& y) { return compare(x.rep, y.rep) >= 0; } inline Fix& Fix::operator + () { return *this; } inline Fix Fix::operator - () { _Fix r = negate(rep); return r; } inline Fix operator + (Fix& x, Fix& y) { _Fix r = add(x.rep, y.rep); return r; } inline Fix operator - (Fix& x, Fix& y) { _Fix r = subtract(x.rep, y.rep); return r; } inline Fix operator * (Fix& x, Fix& y) { _Fix r = multiply(x.rep, y.rep); return r; } inline Fix operator * (Fix& x, long y) { _Fix r = multiply(x.rep, y); return r; } inline Fix operator * (long y, Fix& x) { _Fix r = multiply(x.rep, y); return r; } inline Fix operator / (Fix& x, Fix& y) { _Fix r = divide(x.rep, y.rep); return r; } inline Fix Fix::operator += (Fix& y) { unique(); add(rep, y.rep, rep); return *this; } inline Fix Fix::operator -= (Fix& y) { unique(); subtract(rep, y.rep, rep); return *this; } inline Fix Fix::operator *= (Fix& y) { unique(); multiply(rep, y.rep, rep); return *this; } inline Fix Fix::operator *= (long y) { unique(); multiply(rep, y, rep); return *this; } inline Fix Fix::operator /= (Fix& y) { unique(); divide(rep, y.rep, rep); return *this; } inline Fix operator % (Fix& x, long y) { Fix r((long )x.rep->len + y, x); return r; } inline Fix Fix::operator %= (long y) { Fix r((long )rep->len + y, *this); return *this = r; } inline Fix operator << (Fix& x, long y) { _Fix rep = shift(x.rep, y); return rep; } inline Fix operator >> (Fix& x, long y) { _Fix rep = shift(x.rep, -y); return rep; } inline Fix Fix::operator <<= (long y) { unique(); shift(rep, y, rep); return *this; } inline Fix Fix::operator >>= (long y) { unique(); shift(rep, -y, rep); return *this; } inline Fix operator <? (Fix& x, Fix& y) { if ( compare(x.rep, y.rep) <= 0 ) return x; else return y; } inline Fix operator >? (Fix& x, Fix& y) { if ( compare(x.rep, y.rep) >= 0 ) return x; else return y; } inline Fix abs(Fix& x) { return compare(x.rep) >= 0 ? x : -x; } inline int sgn(Fix& x) { int a = compare(x.rep); return a == 0 ? 0 : (a > 0 ? 1 : -1); } inline long length(Fix& x) { return x.rep->len; } inline ostream& operator << (ostream& s, Fix& y) { return s << Ftoa(y); } inline void negate (Fix& x, Fix& r) { negate(x.rep, r.rep); } inline void add (Fix& x, Fix& y, Fix& r) { add(x.rep, y.rep, r.rep); } inline void subtract (Fix& x, Fix& y, Fix& r) { subtract(x.rep, y.rep, r.rep); } inline void multiply (Fix& x, Fix& y, Fix& r) { multiply(x.rep, y.rep, r.rep); } inline void divide (Fix& x, Fix& y, Fix& q, Fix& r) { divide(x.rep, y.rep, q.rep, r.rep); } inline void shift (Fix& x, long y, Fix& r) { shift(x.rep, y, r.rep); } //#endif #endif ---------------------------------------------------------------------------- // Fix.cc #include <Fix.h> #include <Obstack.h> #include <std.h> // default parameters uint16 Fix_default_length = 16; Fix_peh Fix_overflow_handler = Fix_overflow_saturate; _Frep _Frep_0 = { 16, 1, 1, { 0 } }; _Frep _Frep_m1 = { 16, 1, 1, { 0x8000 } }; _Frep _Frep_quotient_bump = { 16, 1, 1, { 0x4000 } }; // error handling void default_Fix_error_handler(char* msg) { cerr << "Fix: " << msg << "\n"; abort(); } void default_Fix_range_error_handler(char* msg) { cerr << "Fix: range error in " << msg << "\n"; //abort(); } one_arg_error_handler_t Fix_error_handler = default_Fix_error_handler; one_arg_error_handler_t Fix_range_error_handler = default_Fix_range_error_handler; one_arg_error_handler_t set_Fix_error_handler(one_arg_error_handler_t f) { one_arg_error_handler_t old = Fix_error_handler; Fix_error_handler = f; return old; } one_arg_error_handler_t set_Fix_range_error_handler(one_arg_error_handler_t f) { one_arg_error_handler_t old = Fix_range_error_handler; Fix_range_error_handler = f; return old; } void Fix::error(char* msg) { (*Fix_error_handler)(msg); } void Fix::range_error(char* msg) { (*Fix_range_error_handler)(msg); } // allocate a new _Frep inline _Fix _new_Fix(uint16 len) { int siz = (len + 15) >> 4; _Fix z = malloc(sizeof(_Frep) + (siz - 1) * sizeof(uint16)); if (z == 0) (*Fix_error_handler)("out of memory"); z->len = len; z->siz = siz; z->ref = 1; return z; } _Fix new_Fix(uint16 len) { return _new_Fix(len); } _Fix new_Fix(uint16 len, _Fix x) { _Fix z = _new_Fix(len); return copy(x,z); } _Fix new_Fix(uint16 len, double d) { _Fix z = _new_Fix(len); if ( d == _Fix_max_value ) { z->s[0] = 0x7fff; for ( int i=1; i < z->siz; i++ ) z->s[i] = 0xffff; } else if ( d < _Fix_min_value || d > _Fix_max_value ) (*Fix_range_error_handler)("declaration"); else { d *= 32768; for ( int i=0; i < z->siz; i++ ) { z->s[i] = (int16 )d; d -= z->s[i]; d *= 65536; } if ( d >= 32768 ) z->s[z->siz-1]++; } mask(z); return z; } // convert to a double double value(Fix& x) { double d = 0.0; for ( int i=x.rep->siz-1; i >= 0; i-- ) { d += x.rep->s[i]; d *= 1./65536.; } d *= 2.; return d < 1. ? d : d - 2.; } // extract mantissa to Integer Integer mantissa(Fix& x) { Integer a = 0; for ( int i=0; i < x.rep->siz; i++ ) a = (a << 16) + x.rep->s[i]; int n = x.rep->len & 0x0f; if ( n ) a >>= 16 - n; return a; } // comparison functions inline static int docmp(uint16* x, uint16* y, int siz) { int diff = (int16 )*x - (int16 )*y; while ( --siz && !diff ) diff = (int32 )(uint32 )*++x - (int32 )(uint32 )*++y; return diff; } inline static int docmpz(uint16* x, int siz) { while ( siz-- ) if ( *x++ ) return 1; return 0; } int compare(_Fix x, _Fix y = &_Frep_0) { if ( x->siz == y->siz ) return docmp(x->s, y->s, x->siz); else { int r; _Fix longer, shorter; if ( x->siz > y->siz ) { longer = x; shorter = y; r = 1; } else { longer = y; shorter = x; r = -1; } int diff = docmp(x->s, y->s, shorter->siz); if ( diff ) return diff; else if ( docmpz(&longer->s[shorter->siz], longer->siz-shorter->siz) ) return r; else return 0; } } // arithmetic functions _Fix add(_Fix x, _Fix y, _Fix r = NULL) { uint16 xsign = x->s[0], ysign = y->s[0]; _Fix longer, shorter; if ( x->len >= y->len ) longer = x, shorter = y; else longer = y, shorter = x; if ( r == NULL ) r = new_Fix(longer->len); for ( int i=r->siz-1; i >= longer->siz; i-- ) r->s[i] = 0; for ( ; i >= shorter->siz; i-- ) r->s[i] = longer->s[i]; uint16 carry = 0; uint32 sum; for ( ; i >= 0; i-- ) { sum = (uint32 )carry + (uint32 )x->s[i] + (uint32 )y->s[i]; carry = sum >> 16; r->s[i] = sum; } if ( (xsign ^ sum) & (ysign ^ sum) & 0x8000 ) (*Fix_overflow_handler)(r); return r; } _Fix subtract(_Fix x, _Fix y, _Fix r = NULL) { uint16 xsign = x->s[0], ysign = y->s[0]; _Fix longer, shorter; if ( x->len >= y->len ) longer = x, shorter = y; else longer = y, shorter = x; if ( r == NULL ) r = new_Fix(longer->len); for ( int i=r->siz-1; i >= longer->siz; i-- ) r->s[i] = 0; for ( ; i >= shorter->siz; i-- ) r->s[i] = (longer == x ? x->s[i] : -y->s[i]); int16 carry = 0; uint32 sum; for ( ; i >= 0; i-- ) { sum = (int32 )carry + (uint32 )x->s[i] - (uint32 )y->s[i]; carry = sum >> 16; r->s[i] = sum; } if ( (xsign ^ sum) & (~ysign ^ sum) & 0x8000 ) (*Fix_overflow_handler)(r); return r; } _Fix multiply(_Fix x, _Fix y, _Fix r = NULL) { if ( r == NULL ) r = new_Fix(x->len + y->len - 1); int xsign = x->s[0] & 0x8000, ysign = y->s[0] & 0x8000; Fix X(x->len), Y(y->len); if ( xsign ) x = negate(x,X.rep); if ( ysign ) y = negate(y,Y.rep); for ( int i=0; i < r->siz; i++ ) r->s[i] = 0; for ( i=x->siz-1; i >= 0; i-- ) { uint32 carry = 0; for ( int j=y->siz-1; j >= 0; j-- ) { int k = i + j + 1; uint32 a = (uint32 )x->s[i] * (uint32 )y->s[j]; uint32 b = ((a << 1) & 0xffff) + carry; if ( k < r->siz ) { b += r->s[k]; r->s[k] = b; } if ( k < r->siz + 1 ) carry = ((a >> 15) & 0x0001ffff) + (b >> 16); } r->s[i] = carry; } if ( xsign != ysign ) negate(r,r); return r; } _Fix multiply(_Fix x, long y, _Fix r = NULL ) { uint32 ay = abs(y); int16 x0 = x->s[0]; Fix X(x->len); if ( x0 < 0 ) x = negate(x,X.rep); if ( r == NULL ) r = new_Fix(x->len); uint16 carry = 0; for ( int i=r->siz-1; i >= x->siz; i-- ) r->s[i] = 0; uint32 a; for ( ; i >= 0; i-- ) { a = x->s[i] * ay + carry; r->s[i] = a; carry = a >> 16; } if ( a & 0xffff8000 ) (*Fix_range_error_handler)("multiply by long"); if ( (x0 < 0) ^ (y < 0) ) negate(r,r); return r; } _Fix divide(_Fix x, _Fix y, _Fix q = NULL, _Fix r = NULL) { int xsign = x->s[0] & 0x8000, ysign = y->s[0] & 0x8000; if ( q == NULL ) q = new_Fix(x->len); copy(&_Frep_0,q); if ( r == NULL ) r = new_Fix(x->len + y->len - 1); if ( xsign ) negate(x,r); else copy(x,r); Fix Y(y->len); y = ( ysign ? negate(y,Y.rep) : copy(y,Y.rep) ); if ( !compare(y) ) (*Fix_range_error_handler)("division -- division by zero"); else if ( compare(x,y) >= 0 ) if ( compare(x,y) == 0 && xsign ^ ysign != 0 ) { copy(&_Frep_m1,q); copy(&_Frep_0,r); } else (*Fix_range_error_handler)("division"); else { _Fix t; Fix S(r->len), W(q->len,&_Frep_quotient_bump); for ( int i=1; i < q->len; i++ ) { shift(y,-1,y); subtract(r,y,S.rep); int s_status = compare(S.rep); if ( s_status == 0 ) { t = r, r = S.rep, S.rep = t; break; } else if ( s_status > 0 ) { t = r, r = S.rep, S.rep = t; add(q,W.rep,q); } shift(W.rep,-1,W.rep); } if ( xsign ^ ysign ) negate(q,q); } return q; } _Fix shift(_Fix x, long y, _Fix r = NULL ) { if ( y == 0 ) return x; else if ( r == NULL ) r = new_Fix(x->len); long ay = abs(y), ayh = ay >> 4, ayl = ay & 0x0f; int xl, u, ilow, ihigh; uint16 *rs, *xsl, *xsr; if ( y > 0 ) { rs = r->s; xsl = x->s + ayh; xsr = xsl + 1; xl = ayl; u = 1; ihigh = x->siz - ayh - 1; ilow = 0; } else { rs = &r->s[r->siz - 1]; xsr = &x->s[r->siz - 1] - ayh; xsl = xsr - 1; xl = 16 - ayl; u = -1; ihigh = r->siz - ayh - 1; ilow = ihigh - x->siz; } int xr = 16 - xl; uint16 xrmask = 0xffffL >> xr; for ( int i=0; i < ilow; i++, rs+=u, xsl+=u, xsr+=u ) *rs = 0; for ( ; i < ihigh; i++, rs+=u, xsl+=u, xsr+=u ) *rs = (*xsl << xl) + ((*xsr >> xr) & xrmask); *rs = (y > 0 ? (*xsl << xl) : ((*xsr >> xr) & xrmask)); rs += u; for ( ; i < r->siz; i++, rs+=u ) *rs = 0; return r; } _Fix negate(_Fix x, _Fix r = NULL) { if ( r == NULL ) r = new_Fix(x->len); uint32 carry = 1; for ( int i=r->siz-1; i >= x->siz; i-- ) r->s[i] = 0; for ( ; i >= 0; i-- ) { uint32 a = (uint16 )~x->s[i] + carry; // bug work-around r->s[i] = a; carry = a >> 16; } return r; } // io functions Fix atoF(const char* a, long len = 32) { return Fix(len,atof(a)); } char* Ftoa(Fix& x, int width = 8) { int a, b; char *s = ecvt(value(x), width-2, &a, &b); char *t = new char[strlen(s) + 3]; strcpy(t,"0."); strcat(t,s); return t; } extern Obstack _libgxx_io_ob; extern char* _libgxx_io_oblast; istream& operator >> (istream& s, Fix& y) { int got_one = 0; if (!s.readable()) { s.error(); return s; } if (_libgxx_io_oblast) _libgxx_io_ob.free(_libgxx_io_oblast); char sign = 0, point = 0; char ch; s >> WS; while (s.good()) { s.get(ch); if (ch == '-') { if (sign == 0) { sign = 1; _libgxx_io_ob.grow(ch); } else break; } if (ch == '.') { if (point == 0) { point = 1; _libgxx_io_ob.grow(ch); } else break; } else if (ch >= '0' && ch <= '9') { got_one = 1; _libgxx_io_ob.grow(ch); } else break; } char * p = (char*)(_libgxx_io_ob.finish(0)); _libgxx_io_oblast = p; if (s.good()) s.unget(ch); if (!got_one) s.error(); else y = atoF(p); return s; } void show(Fix& x) { cout << "len = " << x.rep->len << "\n"; cout << "siz = " << x.rep->siz << "\n"; cout << "ref = " << x.rep->ref << "\n"; cout << "man = " << Itoa(mantissa(x)<<16-(x.rep->len & 0x0f ?: 16),16,4*x.rep->siz) << "\n"; cout << "val = " << value(x) << "\n"; cout << "\n"; } // parameter setting operations Fix_peh set_overflow_handler(Fix_peh new_handler) { Fix_peh old_handler = Fix_overflow_handler; Fix_overflow_handler = new_handler; return old_handler; } long Fix_set_default_length(long newlen) { uint16 oldlen = Fix_default_length; if ( newlen < _Fix_min_length || newlen > _Fix_max_length ) (*Fix_error_handler)("illegal length in Fix_set_default_length"); Fix_default_length = newlen; return oldlen; } // overflow handlers void Fix_overflow_saturate(_Fix& r) { if ( (int16 )r->s[0] > 0 ) { r->s[0] = 0x8000; for ( int i=1; i < r->siz; i++ ) r->s[i] = 0; } else { r->s[0] = 0x7fff; for ( int i=1; i < r->siz; i++ ) r->s[i] = 0xffff; mask(r); } } void Fix_overflow_wrap(_Fix& r) {} void Fix_overflow_warning_saturate(_Fix& r) { Fix_overflow_warning(r); Fix_overflow_saturate(r); } void Fix_overflow_warning(_Fix& r) { cerr << "Fix: overflow warning\n"; } void Fix_overflow_error(_Fix& r) { cerr << "Fix: overflow error\n"; abort(); } ---------------------------------------------------------------------------- // // type.h : machine type definitions // #ifndef TYPEH #define TYPEH typedef short int int16; typedef long int int32; typedef unsigned short int uint16; typedef unsigned long int uint32; typedef unsigned int uint; typedef unsigned char byte; #endif ----------------------------------------------------------------------------