1 /*
2  * Copyright (c) 2012
3  *      Inferno Nettverk A/S, Norway.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. The above copyright notice, this list of conditions and the following
9  *    disclaimer must appear in all copies of the software, derivative works
10  *    or modified versions, and any portions thereof, aswell as in all
11  *    supporting documentation.
12  * 2. All advertising materials mentioning features or use of this software
13  *    must display the following acknowledgement:
14  *      This product includes software developed by
15  *      Inferno Nettverk A/S, Norway.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * Inferno Nettverk A/S requests users of this software to return to
31  *
32  *  Software Distribution Coordinator  or  sdc@inet.no
33  *  Inferno Nettverk A/S
34  *  Oslo Research Park
35  *  Gaustadall�en 21
36  *  NO-0349 Oslo
37  *  Norway
38  *
39  * any improvements or extensions that they make and grant Inferno Nettverk A/S
40  * the rights to redistribute these changes.
41  *
42  */
43 
44 #include "common.h"
45 
46 static const char rcsid[] =
47 "$Id: hw.c,v 1.12 2012/12/31 17:09:51 karls Exp $";
48 
49 
50 static void
51 minmaxvalueoftype(const size_t typelen, long long *min, long long *max);
52 
53 static void
54 uminmaxvalueoftype(const size_t typelen, unsigned long long *min,
55                    unsigned long long *max);
56 
57 
58 void
runenvcheck(void)59 runenvcheck(void)
60 {
61    const char *function = "runenvcheck()";
62    struct {
63       size_t expectedsize;
64       size_t actualsize;
65       int    issigned;
66       size_t bitlength;
67    } checkv[] = {      /* expected. */   /* is. */       /* signed? */ /* len */
68                      { BYTEWIDTH_8BITS,  sizeof(sbits_8),   1,            8  },
69                      { BYTEWIDTH_8BITS,  sizeof(ubits_8),   0,            8  },
70                      { BYTEWIDTH_16BITS, sizeof(sbits_16),  1,            16 },
71                      { BYTEWIDTH_16BITS, sizeof(ubits_16),  0,            16 },
72                      { BYTEWIDTH_32BITS, sizeof(sbits_32),  1,            32 },
73                      { BYTEWIDTH_32BITS, sizeof(ubits_32),  0,            32 }
74                 };
75    size_t i;
76 
77    for (i = 0; i < ELEMENTS(checkv); ++i)
78       if (checkv[i].expectedsize != checkv[i].actualsize)
79          serrx("%s: expected size of %s %lu bit type to be %lu (based on "
80                "pre-compiletime check), but now it is %lu.  "
81                "Perhaps we were ./configured on a different CPU/platform "
82                "from what we were later compiled on?",
83                function,
84                checkv[i].issigned ? "signed" : "unsigned",
85                (unsigned long)checkv[i].bitlength,
86                (unsigned long)checkv[i].expectedsize,
87                (unsigned long)checkv[i].actualsize);
88 }
89 
90 long long
minvalueoftype(typelen)91 minvalueoftype(typelen)
92    const size_t typelen;
93 {
94    long long min, max;
95 
96    minmaxvalueoftype(typelen, &min, &max);
97    return min;
98 }
99 
100 long long
maxvalueoftype(typelen)101 maxvalueoftype(typelen)
102    const size_t typelen;
103 {
104    long long min, max;
105 
106    minmaxvalueoftype(typelen, &min, &max);
107 
108    return max;
109 }
110 
111 unsigned long long
uminvalueoftype(typelen)112 uminvalueoftype(typelen)
113    const size_t typelen;
114 {
115    unsigned long long min, max;
116 
117    uminmaxvalueoftype(typelen, &min, &max);
118    return min;
119 }
120 
121 unsigned long long
umaxvalueoftype(typelen)122 umaxvalueoftype(typelen)
123    const size_t typelen;
124 {
125    unsigned long long min, max;
126 
127    uminmaxvalueoftype(typelen, &min, &max);
128    return max;
129 }
130 
131 static void
minmaxvalueoftype(typelen,min,max)132 minmaxvalueoftype(typelen, min, max)
133    const size_t typelen;
134    long long *min;
135    long long *max;
136 {
137    const char *function = "minmaxvalueoftype()";
138 
139    switch (typelen) {
140       case sizeof(int8_t):
141          *min = INT8_MIN;
142          *max = INT8_MAX;
143          break;
144 
145       case sizeof(int16_t):
146          *min = INT16_MIN;
147          *max = INT16_MAX;
148          break;
149 
150       case sizeof(int32_t):
151          *min = INT32_MIN;
152          *max = INT32_MAX;
153          break;
154 
155       case sizeof(int64_t):
156          *min = INT64_MIN;
157          *max = INT64_MAX;
158          break;
159 
160       default:
161          swarnx("%s: unsupported typelength %lu",
162                 function, (unsigned long)typelen);
163 
164          SERRX(0);
165    }
166 }
167 
168 static void
uminmaxvalueoftype(typelen,min,max)169 uminmaxvalueoftype(typelen, min, max)
170    const size_t typelen;
171    unsigned long long *min;
172    unsigned long long *max;
173 {
174    const char *function = "minmaxvalueoftype()";
175 
176    *min = 0;
177 
178    switch (typelen) {
179       case sizeof(uint8_t):
180          *max = UINT8_MAX;
181          break;
182 
183       case sizeof(uint16_t):
184          *max = UINT16_MAX;
185          break;
186 
187       case sizeof(uint32_t):
188          *max = UINT32_MAX;
189          break;
190 
191       case sizeof(uint64_t):
192          *max = UINT64_MAX;
193          break;
194 
195       default:
196          swarnx("%s: unsupported typelength %lu",
197                 function, (unsigned long)typelen);
198 
199          SERRX(0);
200    }
201 }
202