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