/*
 *
 *	$Header: /usr/u/wjr/include/RCS/misc.h,v 1.22 1992/05/13 00:16:16 wjr Exp $
 *
 * Copyright (c) 1990, 1991 Cornell University.  All Rights Reserved.
 *
 * Copyright (c) 1991 Xerox Corporation.  All Rights Reserved.
 *
 * Use, reproduction and preparation of derivative works of this software is
 * permitted.  Any copy of this software or of any derivative work must
 * include both the above copyright notices of Cornell University and Xerox
 * Corporation and this paragraph.  This software is made available AS IS, and
 * XEROX CORPORATION DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
 * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE, AND NOTWITHSTANDING ANY OTHER PROVISION CONTAINED
 * HEREIN, ANY LIABILITY FOR DAMAGES RESULTING FROM THE SOFTWARE OR ITS USE IS
 * EXPRESSLY DISCLAIMED, WHETHER ARISING IN CONTRACT, TORT (INCLUDING
 * NEGLIGENCE) OR STRICT LIABILITY, EVEN IF XEROX CORPORATION IS ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGES.
 */
/*
 *
 * misc.h - miscellaneous types and declarations
 *
 */

#ifndef WJR_MISC_H
#define	WJR_MISC_H



#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include "panic.h"
#include "dbug.h"

typedef unsigned char uchar;
#if 0
typedef unsigned long ulong;
#endif
/* typedef unsigned short ushort; */
/* typedef unsigned int  uint; */      /* now in gcc-lib/sparc/2.2.2/include/types.h */

/* rgb and hsv structures... */
typedef struct {uchar r, g, b;} RGB;
typedef struct {float r, g, b;} RGBFloat;
typedef struct {float h, s, v;} HSV;


typedef char boolean;

#undef  TRUE
#define TRUE	((boolean) 1)

#undef  FALSE
#define FALSE	((boolean) 0)


#ifndef __GNUC__

#ifdef	cstar                /* Use the C* extensions: >? (MAX) and <? (MIN) operators */

#ifndef	MAX
#define	MAX(x, y) ((x) >? (y))
#endif

#ifndef	MIN
#define	MIN(x, y) ((x) <? (y))
#endif

#ifndef	ABS
#define	ABS(x)	((x) >? (-(x)))
#endif

#else                                        /* not gcc and not cstar */

#ifndef MAX
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#endif

#ifndef MIN
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#endif

#ifndef ABS
#define ABS(x) (((x) < 0) ? -(x) : (x))
#endif

#endif

                                             /* not gcc => kill inline */
#define inline 

#else                                        /* using gcc */

/* GCC allows new scopes inside expressions; use to get better MAX and MIN */

#define MAX(a, b)   ({ typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; })
#define MIN(a, b)   ({ typeof (a) _a = (a); typeof (b) _b = (b); _a < _b ? _a : _b; })
#define ABS(a)      ({ typeof (a) _a = (a); _a < (typeof (a)) 0 ? -_a : _a; })
#define SQUARE(x)   ({ typeof (x) _x = (x); _x*_x; })
#define SWAP(a, b)  ({ typeof (a) _tmp = (a); (a) = (b); (b) = _tmp; })
#define ROUND(a)    ({ typeof (a) __a = (a);                                           \
                       (int) ((__a > 0) ? (__a + 0.5) : (__a - 0.5)); })

#define FLOAT_NAN     ({ float f; (*((long *) &f) = 0x7fffffff); f;})
#define DOUBLE_NAN    ({ double d; (*((long *) &d) = 0x7fffffff);                      \
                         (*(((long *) &d) + 1) = 0xffffffff); d;})

#endif


#define BOUND(x, l, h)   (MIN(MAX((x), (l)), (h)))


/*  I (gregk) like to use lower case too ... unfortunately there are some */
/* existing functions in the library by these names.  */
#if 0
#define max(a, b)      MAX(a, b)
#define min(a, b)      MIN(a, b)
#define abs(a)         ABS(a)
#define bound(a, b)    BOUND(a, b)
#define square(a)      SQUARE(a)
#define swap(a, b)     SWAP(a, b)
#define true           TRUE
#define false          FALSE
#endif


/* these make more sense (gregk) */

#define import         extern
#define export         
#define local          static


/* This is what malloc returns on error, and is also a null pointer */
#ifndef NULL
#define NULL    0
#endif



/* include declarations for some library routines that never seem to get declared */
/* Unfortunately those gcc people can't seem to get their acts together on what the */
/* proper prototypes should be and even whether some of these should have the proper */
/* argument types declared...  so a macro tells what file to include */
/* should we use gcc's __VERSION__ macro?? */
/* #define  MISC_H_PROTOTYPE_FILE "misc-h-prototypes-gcc-" __VERSION__ ".h" */

#ifndef  MISC_H_PROTOTYPE_FILE
#define  MISC_H_PROTOTYPE_FILE "misc-h-prototypes.h"
#endif

#include MISC_H_PROTOTYPE_FILE

#endif
