8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libtnf / scalar.c
blob8d37f690a156e8ed4c79cc0b35f17f70cfee995b
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright (c) 1994, by Sun Microsytems, Inc.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include "libtnf.h"
34 #define CHECK_SCALAR(datum) check_scalar(datum)
36 #define DATUM_NATIVE(x) DATUM_TNF(datum)->file_native
42 static void check_scalar(tnf_datum_t);
44 static tnf_uint64_t get_uint64(TNF *tnf, caddr_t val);
50 static void
51 check_scalar(tnf_datum_t datum)
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 */
60 * Exported scalar operations
63 /* No swapping required: */
65 char
66 tnf_get_char(tnf_datum_t datum)
68 CHECK_SCALAR(datum);
69 return (*(char *)DATUM_VAL(datum));
72 tnf_int8_t
73 tnf_get_int8(tnf_datum_t datum)
75 CHECK_SCALAR(datum);
76 return (*(tnf_int8_t *)DATUM_VAL(datum));
79 tnf_int16_t
80 tnf_get_int16(tnf_datum_t datum)
82 tnf_int16_t val;
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));
90 /* 32-bit integers: */
92 tnf_int32_t
93 tnf_get_int32(tnf_datum_t datum)
95 CHECK_SCALAR(datum);
96 /* LINTED pointer cast may result in improper alignment */
97 return (_GET_INT32(DATUM_TNF(datum), DATUM_VAL(datum)));
100 /* 64-bit integers: */
102 static tnf_uint64_t
103 get_uint64(TNF *tnf, caddr_t val)
105 tnf_uint32_t hi32, lo32; /* XXX both assumed unsigned */
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));
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
134 tnf_int64_t
135 tnf_get_int64(tnf_datum_t datum)
137 CHECK_SCALAR(datum);
138 return (get_uint64(DATUM_TNF(datum), DATUM_VAL(datum)));
141 /* floats: */
143 tnf_float32_t
144 tnf_get_float32(tnf_datum_t datum)
146 union {
147 tnf_uint32_t i32;
148 tnf_float32_t f32;
149 } u;
151 CHECK_SCALAR(datum);
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);
158 tnf_float64_t
159 tnf_get_float64(tnf_datum_t datum)
161 union {
162 tnf_uint64_t i64;
163 tnf_float64_t f64;
164 } u;
166 CHECK_SCALAR(datum);
168 u.i64 = get_uint64(DATUM_TNF(datum), DATUM_VAL(datum)); /* XXX */
169 return (u.f64);