VirtualBox

source: vbox/trunk/src/libs/softfloat-3e/testfloat/source/readHex.c@ 106842

Last change on this file since 106842 was 94551, checked in by vboxsync, 3 years ago

libs/softfloat: Copied TestFloat-3e from vendor branch and to testfloat subdir. bugref:9898

  • Property svn:eol-style set to native
File size: 5.2 KB
Line 
1
2/*============================================================================
3
4This C source file is part of TestFloat, Release 3e, a package of programs for
5testing the correctness of floating-point arithmetic complying with the IEEE
6Standard for Floating-Point, by John R. Hauser.
7
8Copyright 2011, 2012, 2013, 2014 The Regents of the University of California.
9All rights reserved.
10
11Redistribution and use in source and binary forms, with or without
12modification, are permitted provided that the following conditions are met:
13
14 1. Redistributions of source code must retain the above copyright notice,
15 this list of conditions, and the following disclaimer.
16
17 2. Redistributions in binary form must reproduce the above copyright notice,
18 this list of conditions, and the following disclaimer in the documentation
19 and/or other materials provided with the distribution.
20
21 3. Neither the name of the University nor the names of its contributors may
22 be used to endorse or promote products derived from this software without
23 specific prior written permission.
24
25THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
26EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
28DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
29DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36=============================================================================*/
37
38#include <stdbool.h>
39#include <stdint.h>
40#include <stdio.h>
41#include "platform.h"
42#include "readHex.h"
43
44bool readHex_bool( bool *aPtr, char sepChar )
45{
46 int i;
47 bool a;
48
49 i = fgetc( stdin );
50 if ( (i == EOF) || (i < '0') || ('1' < i) ) return false;
51 a = i - '0';
52 if ( sepChar ) {
53 i = fgetc( stdin );
54 if ( (sepChar != '\n') || (i != '\r') ) {
55 if ( i != sepChar ) return false;
56 }
57 }
58 *aPtr = a;
59 return true;
60
61}
62
63bool readHex_ui8( uint_least8_t *aPtr, char sepChar )
64{
65 int i;
66 uint_fast8_t a;
67
68 i = fgetc( stdin );
69 if ( i == EOF ) return false;
70 if ( ('0' <= i) && (i <= '9') ) {
71 i -= '0';
72 } else if ( ('A' <= i) && (i <= 'F') ) {
73 i -= 'A' - 10;
74 } else if ( ('a' <= i) && (i <= 'f') ) {
75 i -= 'a' - 10;
76 } else {
77 return false;
78 }
79 a = i<<4;
80 i = fgetc( stdin );
81 if ( i == EOF ) return false;
82 if ( ('0' <= i) && (i <= '9') ) {
83 i -= '0';
84 } else if ( ('A' <= i) && (i <= 'F') ) {
85 i -= 'A' - 10;
86 } else if ( ('a' <= i) && (i <= 'f') ) {
87 i -= 'a' - 10;
88 } else {
89 return false;
90 }
91 a |= i;
92 if ( sepChar ) {
93 i = fgetc( stdin );
94 if ( (sepChar != '\n') || (i != '\r') ) {
95 if ( i != sepChar ) return false;
96 }
97 }
98 *aPtr = a;
99 return true;
100
101}
102
103bool readHex_ui16( uint16_t *aPtr, char sepChar )
104{
105 int i;
106 uint_fast16_t a;
107
108 i = fgetc( stdin );
109 if ( i == EOF ) return false;
110 if ( ('0' <= i) && (i <= '9') ) {
111 i -= '0';
112 } else if ( ('A' <= i) && (i <= 'F') ) {
113 i -= 'A' - 10;
114 } else if ( ('a' <= i) && (i <= 'f') ) {
115 i -= 'a' - 10;
116 } else {
117 return false;
118 }
119 a = (uint_fast16_t) i<<12;
120 i = fgetc( stdin );
121 if ( i == EOF ) return false;
122 if ( ('0' <= i) && (i <= '9') ) {
123 i -= '0';
124 } else if ( ('A' <= i) && (i <= 'F') ) {
125 i -= 'A' - 10;
126 } else if ( ('a' <= i) && (i <= 'f') ) {
127 i -= 'a' - 10;
128 } else {
129 return false;
130 }
131 a |= (uint_fast16_t) i<<8;
132 i = fgetc( stdin );
133 if ( i == EOF ) return false;
134 if ( ('0' <= i) && (i <= '9') ) {
135 i -= '0';
136 } else if ( ('A' <= i) && (i <= 'F') ) {
137 i -= 'A' - 10;
138 } else if ( ('a' <= i) && (i <= 'f') ) {
139 i -= 'a' - 10;
140 } else {
141 return false;
142 }
143 a |= (uint_fast16_t) i<<4;
144 i = fgetc( stdin );
145 if ( i == EOF ) return false;
146 if ( ('0' <= i) && (i <= '9') ) {
147 i -= '0';
148 } else if ( ('A' <= i) && (i <= 'F') ) {
149 i -= 'A' - 10;
150 } else if ( ('a' <= i) && (i <= 'f') ) {
151 i -= 'a' - 10;
152 } else {
153 return false;
154 }
155 a |= i;
156 if ( sepChar ) {
157 i = fgetc( stdin );
158 if ( (sepChar != '\n') || (i != '\r') ) {
159 if ( i != sepChar ) return false;
160 }
161 }
162 *aPtr = a;
163 return true;
164
165}
166
167bool readHex_ui32( uint32_t *aPtr, char sepChar )
168{
169 uint16_t v16, v0;
170
171 if ( ! readHex_ui16( &v16, 0 ) || ! readHex_ui16( &v0, sepChar ) ) {
172 return false;
173 }
174 *aPtr = (uint_fast32_t) v16<<16 | v0;
175 return true;
176
177}
178
179bool readHex_ui64( uint64_t *aPtr, char sepChar )
180{
181 uint32_t v32, v0;
182
183 if ( ! readHex_ui32( &v32, 0 ) || ! readHex_ui32( &v0, sepChar ) ) {
184 return false;
185 }
186 *aPtr = (uint_fast64_t) v32<<32 | v0;
187 return true;
188
189}
190
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette