xref: /titanic_50/usr/src/lib/libtnf/scalar.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  *	Copyright (c) 1994, by Sun Microsytems, Inc.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #pragma	ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #include "libtnf.h"
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #define	CHECK_SCALAR(datum)	check_scalar(datum)
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #define	DATUM_NATIVE(x)		DATUM_TNF(datum)->file_native
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate /*
39*7c478bd9Sstevel@tonic-gate  *
40*7c478bd9Sstevel@tonic-gate  */
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate static void		check_scalar(tnf_datum_t);
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate static tnf_uint64_t	get_uint64(TNF *tnf, caddr_t val);
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate /*
47*7c478bd9Sstevel@tonic-gate  *
48*7c478bd9Sstevel@tonic-gate  */
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate static void
check_scalar(tnf_datum_t datum)51*7c478bd9Sstevel@tonic-gate check_scalar(tnf_datum_t datum)
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	CHECK_DATUM(datum);
54*7c478bd9Sstevel@tonic-gate 	if (!INFO_SCALAR(DATUM_INFO(datum)))
55*7c478bd9Sstevel@tonic-gate 		_tnf_error(DATUM_TNF(datum), TNF_ERR_TYPEMISMATCH);
56*7c478bd9Sstevel@tonic-gate 	/* XXX Need to check for exact scalar type match as well */
57*7c478bd9Sstevel@tonic-gate }
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate /*
60*7c478bd9Sstevel@tonic-gate  * Exported scalar operations
61*7c478bd9Sstevel@tonic-gate  */
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate /* No swapping required: */
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate char
tnf_get_char(tnf_datum_t datum)66*7c478bd9Sstevel@tonic-gate tnf_get_char(tnf_datum_t datum)
67*7c478bd9Sstevel@tonic-gate {
68*7c478bd9Sstevel@tonic-gate 	CHECK_SCALAR(datum);
69*7c478bd9Sstevel@tonic-gate 	return (*(char *)DATUM_VAL(datum));
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate tnf_int8_t
tnf_get_int8(tnf_datum_t datum)73*7c478bd9Sstevel@tonic-gate tnf_get_int8(tnf_datum_t datum)
74*7c478bd9Sstevel@tonic-gate {
75*7c478bd9Sstevel@tonic-gate 	CHECK_SCALAR(datum);
76*7c478bd9Sstevel@tonic-gate 	return (*(tnf_int8_t *)DATUM_VAL(datum));
77*7c478bd9Sstevel@tonic-gate }
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate tnf_int16_t
tnf_get_int16(tnf_datum_t datum)80*7c478bd9Sstevel@tonic-gate tnf_get_int16(tnf_datum_t datum)
81*7c478bd9Sstevel@tonic-gate {
82*7c478bd9Sstevel@tonic-gate 	tnf_int16_t	val;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	CHECK_SCALAR(datum);
85*7c478bd9Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
86*7c478bd9Sstevel@tonic-gate 	val = *(tnf_int16_t *)DATUM_VAL(datum);
87*7c478bd9Sstevel@tonic-gate 	return (DATUM_NATIVE(datum) ? val : _tnf_swap16(val));
88*7c478bd9Sstevel@tonic-gate }
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate /* 32-bit integers: */
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate tnf_int32_t
tnf_get_int32(tnf_datum_t datum)93*7c478bd9Sstevel@tonic-gate tnf_get_int32(tnf_datum_t datum)
94*7c478bd9Sstevel@tonic-gate {
95*7c478bd9Sstevel@tonic-gate 	CHECK_SCALAR(datum);
96*7c478bd9Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
97*7c478bd9Sstevel@tonic-gate 	return (_GET_INT32(DATUM_TNF(datum), DATUM_VAL(datum)));
98*7c478bd9Sstevel@tonic-gate }
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate /* 64-bit integers: */
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate static tnf_uint64_t
get_uint64(TNF * tnf,caddr_t val)103*7c478bd9Sstevel@tonic-gate get_uint64(TNF *tnf, caddr_t val)
104*7c478bd9Sstevel@tonic-gate {
105*7c478bd9Sstevel@tonic-gate 	tnf_uint32_t	hi32, lo32; /* XXX both assumed unsigned */
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	/* XXX Can't rely on address alignment */
108*7c478bd9Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
109*7c478bd9Sstevel@tonic-gate 	hi32 = *(tnf_uint32_t *)val;
110*7c478bd9Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
111*7c478bd9Sstevel@tonic-gate 	lo32 = *(tnf_uint32_t *)(val + sizeof (tnf_uint32_t));
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate #ifdef _LONG_LONG_HTOL
114*7c478bd9Sstevel@tonic-gate 	/* eg. sparc */
115*7c478bd9Sstevel@tonic-gate 	if (tnf->file_native)
116*7c478bd9Sstevel@tonic-gate 		return ((((tnf_uint64_t)hi32) << 32)
117*7c478bd9Sstevel@tonic-gate 			+ (tnf_uint64_t)lo32);
118*7c478bd9Sstevel@tonic-gate 	else
119*7c478bd9Sstevel@tonic-gate 		/* XXX Assume words are swapped as well: */
120*7c478bd9Sstevel@tonic-gate 		return ((((tnf_uint64_t)_tnf_swap32(lo32)) << 32)
121*7c478bd9Sstevel@tonic-gate 			+ (tnf_uint64_t)_tnf_swap32(hi32));
122*7c478bd9Sstevel@tonic-gate #else
123*7c478bd9Sstevel@tonic-gate 	/* eg. i386 */
124*7c478bd9Sstevel@tonic-gate 	if (tnf->file_native)
125*7c478bd9Sstevel@tonic-gate 		return ((((tnf_uint64_t)lo32) << 32)
126*7c478bd9Sstevel@tonic-gate 			+ (tnf_uint64_t)hi32);
127*7c478bd9Sstevel@tonic-gate 	else
128*7c478bd9Sstevel@tonic-gate 		/* XXX Assume words are swapped as well: */
129*7c478bd9Sstevel@tonic-gate 		return ((((tnf_uint64_t)_tnf_swap32(hi32)) << 32)
130*7c478bd9Sstevel@tonic-gate 			+ (tnf_uint64_t)_tnf_swap32(lo32));
131*7c478bd9Sstevel@tonic-gate #endif
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate tnf_int64_t
tnf_get_int64(tnf_datum_t datum)135*7c478bd9Sstevel@tonic-gate tnf_get_int64(tnf_datum_t datum)
136*7c478bd9Sstevel@tonic-gate {
137*7c478bd9Sstevel@tonic-gate 	CHECK_SCALAR(datum);
138*7c478bd9Sstevel@tonic-gate 	return (get_uint64(DATUM_TNF(datum), DATUM_VAL(datum)));
139*7c478bd9Sstevel@tonic-gate }
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate /* floats: */
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate tnf_float32_t
tnf_get_float32(tnf_datum_t datum)144*7c478bd9Sstevel@tonic-gate tnf_get_float32(tnf_datum_t datum)
145*7c478bd9Sstevel@tonic-gate {
146*7c478bd9Sstevel@tonic-gate 	union {
147*7c478bd9Sstevel@tonic-gate 		tnf_uint32_t	i32;
148*7c478bd9Sstevel@tonic-gate 		tnf_float32_t	f32;
149*7c478bd9Sstevel@tonic-gate 	} u;
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	CHECK_SCALAR(datum);
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
154*7c478bd9Sstevel@tonic-gate 	u.i32 = _GET_UINT32(DATUM_TNF(datum), DATUM_VAL(datum)); /* XXX */
155*7c478bd9Sstevel@tonic-gate 	return (u.f32);
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate tnf_float64_t
tnf_get_float64(tnf_datum_t datum)159*7c478bd9Sstevel@tonic-gate tnf_get_float64(tnf_datum_t datum)
160*7c478bd9Sstevel@tonic-gate {
161*7c478bd9Sstevel@tonic-gate 	union {
162*7c478bd9Sstevel@tonic-gate 		tnf_uint64_t	i64;
163*7c478bd9Sstevel@tonic-gate 		tnf_float64_t	f64;
164*7c478bd9Sstevel@tonic-gate 	} u;
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	CHECK_SCALAR(datum);
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	u.i64 = get_uint64(DATUM_TNF(datum), DATUM_VAL(datum)); /* XXX */
169*7c478bd9Sstevel@tonic-gate 	return (u.f64);
170*7c478bd9Sstevel@tonic-gate }
171