1 /*
2  * vp_util.c
3  *
4  * Miscellaneous utility routines.
5  *
6  * Copyright (c) 1994 The Board of Trustees of The Leland Stanford
7  * Junior University.  All rights reserved.
8  *
9  * Permission to use, copy, modify and distribute this software and its
10  * documentation for any purpose is hereby granted without fee, provided
11  * that the above copyright notice and this permission notice appear in
12  * all copies of this software and that you do not sell the software.
13  * Commercial licensing is available by contacting the author.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  * Author:
20  *    Phil Lacroute
21  *    Computer Systems Laboratory
22  *    Electrical Engineering Dept.
23  *    Stanford University
24  */
25 
26 /*
27  * $Date: 1994/12/30 23:52:38 $
28  * $Revision: 1.27 $
29  */
30 
31 #include "vp_global.h"
32 
33 /*
34  * vpRamp
35  *
36  * Fill a float array with a ramp (a piecewise linear function).
37  */
38 
39 vpResult
vpRamp(dst,stride,num_points,ramp_x,ramp_y)40 vpRamp(dst, stride, num_points, ramp_x, ramp_y)
41 float *dst;	/* array to store ramp into */
42 int stride;	/* stride (in bytes) for dst */
43 int num_points;	/* number of linear segment endpoints */
44 int *ramp_x;	/* x coordinates of the endpoints (and indexes into dst) */
45 float *ramp_y;	/* y coordinates of the endpoints */
46 {
47     int i, x;
48     int lastx, nextx;
49     double lasty, nexty, dydx, y;
50 
51     if (num_points < 1)
52 	return(VPERROR_BAD_VALUE);
53     for (i = 1; i < num_points; i++)
54 	if (ramp_x[i] <= ramp_x[i-1])
55 	    return(VPERROR_BAD_VALUE);
56     dst[*ramp_x * stride/sizeof(float)] = *ramp_y;
57     if (num_points == 1)
58 	return(VP_OK);
59     for (i = 1; i < num_points; i++) {
60 	lastx = ramp_x[i-1];
61 	lasty = ramp_y[i-1];
62 	nextx = ramp_x[i];
63 	nexty = ramp_y[i];
64 	dydx = (double)(nexty - lasty) / (double)(nextx - lastx);
65 	for (x = lastx+1, y = lasty+dydx; x < nextx; x++, y += dydx)
66 	    dst[x * stride/sizeof(float)] = y;
67 	dst[x * stride/sizeof(float)] = nexty;
68     }
69     return(VP_OK);
70 }
71 
72 /*
73  * VPBug
74  *
75  * Print an error message and exit.  The argument is a printf-style format
76  * string followed by parameters.
77  */
78 
79 #ifdef ANSI_C
80 void
VPBug(char * fmt,...)81 VPBug(char *fmt, ...)
82 #else
83 void
84 VPBug(fmt, va_alist)
85 char *fmt;
86 va_dcl
87 #endif
88 {
89     va_list args;
90     extern void exit ANSI_ARGS((int status));
91 
92     fprintf(stderr, "BUG: ");
93 #ifdef ANSI_C
94     va_start(args, fmt);
95 #else
96     va_start(args);
97 #endif
98 #ifdef HAVE_VPRINTF
99     vfprintf(stderr, fmt, args);
100 #else
101 #ifdef HAVE_DOPRNT
102     _doprnt(fmt, args, stderr);
103 #else
104     fprintf(stderr, "unrecoverable error");
105 #endif
106 #endif
107     va_end(args);
108     fprintf(stderr, "\n");
109     exit(1);
110 }
111 
112 #ifdef DEBUG
113 /*
114  * VPDebug
115  *
116  * Print a debugging message.
117  *    VPDebug(debug_code, format, parameters, ...)
118  */
119 
120 #ifdef ANSI_C
121 void
VPDebug(vpContext * vpc,int debug_code,char * fmt,...)122 VPDebug(vpContext *vpc, int debug_code, char *fmt, ...)
123 #else
124 void
125 VPDebug(vpc, debug_code, fmt, va_alist)
126 vpContext *vpc;
127 int debug_code;
128 char *fmt;
129 va_dcl
130 #endif
131 {
132     va_list args;
133 
134     if (vpc->debug_enable[debug_code]) {
135 #ifdef ANSI_C
136 	va_start(args, fmt);
137 #else
138 	va_start(args);
139 #endif
140 #ifdef HAVE_VPRINTF
141 	vfprintf(stdout, fmt, args);
142 #else
143 #ifdef HAVE_DOPRNT
144 	_doprnt(fmt, args, stdout);
145 #else
146 	printf("unrecoverable error");
147 #endif
148 #endif
149 	va_end(args);
150     }
151 }
152 #endif /* DEBUG */
153