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