1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // constant conversions
6
7 package const1
8
9 import "math"
10
11 const(
12 mi = ^int(0)
13 mu = ^uint(0)
14 mp = ^uintptr(0)
15
16 logSizeofInt = uint(mi>>8&1 + mi>>16&1 + mi>>32&1)
17 logSizeofUint = uint(mu>>8&1 + mu>>16&1 + mu>>32&1)
18 logSizeofUintptr = uint(mp>>8&1 + mp>>16&1 + mp>>32&1)
19 )
20
21 const (
22 minInt8 = -1<<(8<<iota - 1)
23 minInt16
24 minInt32
25 minInt64
26 minInt = -1<<(8<<logSizeofInt - 1)
27 )
28
29 const (
30 maxInt8 = 1<<(8<<iota - 1) - 1
31 maxInt16
32 maxInt32
33 maxInt64
34 maxInt = 1<<(8<<logSizeofInt - 1) - 1
35 )
36
37 const (
38 maxUint8 = 1<<(8<<iota) - 1
39 maxUint16
40 maxUint32
41 maxUint64
42 maxUint = 1<<(8<<logSizeofUint) - 1
43 maxUintptr = 1<<(8<<logSizeofUintptr) - 1
44 )
45
46 const (
47 smallestFloat32 = 1.0 / (1<<(127 - 1 + 23))
48 // TODO(gri) The compiler limits integers to 512 bit and thus
49 // we cannot compute the value (1<<(1023 - 1 + 52))
50 // without overflow. For now we match the compiler.
51 // See also issue #44057.
52 // smallestFloat64 = 1.0 / (1<<(1023 - 1 + 52))
53 smallestFloat64 = math.SmallestNonzeroFloat64
54 )
55
56 const (
57 _ = assert(smallestFloat32 > 0)
58 _ = assert(smallestFloat64 > 0)
59 )
60
61 const (
62 maxFloat32 = 1<<127 * (1<<24 - 1) / (1.0<<23)
63 // TODO(gri) The compiler limits integers to 512 bit and thus
64 // we cannot compute the value 1<<1023
65 // without overflow. For now we match the compiler.
66 // See also issue #44057.
67 // maxFloat64 = 1<<1023 * (1<<53 - 1) / (1.0<<52)
68 maxFloat64 = math.MaxFloat64
69 )
70
71 const (
72 _ int8 = minInt8 /* ERROR "overflows" */ - 1
73 _ int8 = minInt8
74 _ int8 = maxInt8
75 _ int8 = maxInt8 /* ERROR "overflows" */ + 1
76 _ int8 = smallestFloat64 /* ERROR "truncated" */
77
78 _ = int8(minInt8 /* ERROR "cannot convert" */ - 1)
79 _ = int8(minInt8)
80 _ = int8(maxInt8)
81 _ = int8(maxInt8 /* ERROR "cannot convert" */ + 1)
82 _ = int8(smallestFloat64 /* ERROR "cannot convert" */)
83 )
84
85 const (
86 _ int16 = minInt16 /* ERROR "overflows" */ - 1
87 _ int16 = minInt16
88 _ int16 = maxInt16
89 _ int16 = maxInt16 /* ERROR "overflows" */ + 1
90 _ int16 = smallestFloat64 /* ERROR "truncated" */
91
92 _ = int16(minInt16 /* ERROR "cannot convert" */ - 1)
93 _ = int16(minInt16)
94 _ = int16(maxInt16)
95 _ = int16(maxInt16 /* ERROR "cannot convert" */ + 1)
96 _ = int16(smallestFloat64 /* ERROR "cannot convert" */)
97 )
98
99 const (
100 _ int32 = minInt32 /* ERROR "overflows" */ - 1
101 _ int32 = minInt32
102 _ int32 = maxInt32
103 _ int32 = maxInt32 /* ERROR "overflows" */ + 1
104 _ int32 = smallestFloat64 /* ERROR "truncated" */
105
106 _ = int32(minInt32 /* ERROR "cannot convert" */ - 1)
107 _ = int32(minInt32)
108 _ = int32(maxInt32)
109 _ = int32(maxInt32 /* ERROR "cannot convert" */ + 1)
110 _ = int32(smallestFloat64 /* ERROR "cannot convert" */)
111 )
112
113 const (
114 _ int64 = minInt64 /* ERROR "overflows" */ - 1
115 _ int64 = minInt64
116 _ int64 = maxInt64
117 _ int64 = maxInt64 /* ERROR "overflows" */ + 1
118 _ int64 = smallestFloat64 /* ERROR "truncated" */
119
120 _ = int64(minInt64 /* ERROR "cannot convert" */ - 1)
121 _ = int64(minInt64)
122 _ = int64(maxInt64)
123 _ = int64(maxInt64 /* ERROR "cannot convert" */ + 1)
124 _ = int64(smallestFloat64 /* ERROR "cannot convert" */)
125 )
126
127 const (
128 _ int = minInt /* ERROR "overflows" */ - 1
129 _ int = minInt
130 _ int = maxInt
131 _ int = maxInt /* ERROR "overflows" */ + 1
132 _ int = smallestFloat64 /* ERROR "truncated" */
133
134 _ = int(minInt /* ERROR "cannot convert" */ - 1)
135 _ = int(minInt)
136 _ = int(maxInt)
137 _ = int(maxInt /* ERROR "cannot convert" */ + 1)
138 _ = int(smallestFloat64 /* ERROR "cannot convert" */)
139 )
140
141 const (
142 _ uint8 = 0 /* ERROR "overflows" */ - 1
143 _ uint8 = 0
144 _ uint8 = maxUint8
145 _ uint8 = maxUint8 /* ERROR "overflows" */ + 1
146 _ uint8 = smallestFloat64 /* ERROR "truncated" */
147
148 _ = uint8(0 /* ERROR "cannot convert" */ - 1)
149 _ = uint8(0)
150 _ = uint8(maxUint8)
151 _ = uint8(maxUint8 /* ERROR "cannot convert" */ + 1)
152 _ = uint8(smallestFloat64 /* ERROR "cannot convert" */)
153 )
154
155 const (
156 _ uint16 = 0 /* ERROR "overflows" */ - 1
157 _ uint16 = 0
158 _ uint16 = maxUint16
159 _ uint16 = maxUint16 /* ERROR "overflows" */ + 1
160 _ uint16 = smallestFloat64 /* ERROR "truncated" */
161
162 _ = uint16(0 /* ERROR "cannot convert" */ - 1)
163 _ = uint16(0)
164 _ = uint16(maxUint16)
165 _ = uint16(maxUint16 /* ERROR "cannot convert" */ + 1)
166 _ = uint16(smallestFloat64 /* ERROR "cannot convert" */)
167 )
168
169 const (
170 _ uint32 = 0 /* ERROR "overflows" */ - 1
171 _ uint32 = 0
172 _ uint32 = maxUint32
173 _ uint32 = maxUint32 /* ERROR "overflows" */ + 1
174 _ uint32 = smallestFloat64 /* ERROR "truncated" */
175
176 _ = uint32(0 /* ERROR "cannot convert" */ - 1)
177 _ = uint32(0)
178 _ = uint32(maxUint32)
179 _ = uint32(maxUint32 /* ERROR "cannot convert" */ + 1)
180 _ = uint32(smallestFloat64 /* ERROR "cannot convert" */)
181 )
182
183 const (
184 _ uint64 = 0 /* ERROR "overflows" */ - 1
185 _ uint64 = 0
186 _ uint64 = maxUint64
187 _ uint64 = maxUint64 /* ERROR "overflows" */ + 1
188 _ uint64 = smallestFloat64 /* ERROR "truncated" */
189
190 _ = uint64(0 /* ERROR "cannot convert" */ - 1)
191 _ = uint64(0)
192 _ = uint64(maxUint64)
193 _ = uint64(maxUint64 /* ERROR "cannot convert" */ + 1)
194 _ = uint64(smallestFloat64 /* ERROR "cannot convert" */)
195 )
196
197 const (
198 _ uint = 0 /* ERROR "overflows" */ - 1
199 _ uint = 0
200 _ uint = maxUint
201 _ uint = maxUint /* ERROR "overflows" */ + 1
202 _ uint = smallestFloat64 /* ERROR "truncated" */
203
204 _ = uint(0 /* ERROR "cannot convert" */ - 1)
205 _ = uint(0)
206 _ = uint(maxUint)
207 _ = uint(maxUint /* ERROR "cannot convert" */ + 1)
208 _ = uint(smallestFloat64 /* ERROR "cannot convert" */)
209 )
210
211 const (
212 _ uintptr = 0 /* ERROR "overflows" */ - 1
213 _ uintptr = 0
214 _ uintptr = maxUintptr
215 _ uintptr = maxUintptr /* ERROR "overflows" */ + 1
216 _ uintptr = smallestFloat64 /* ERROR "truncated" */
217
218 _ = uintptr(0 /* ERROR "cannot convert" */ - 1)
219 _ = uintptr(0)
220 _ = uintptr(maxUintptr)
221 _ = uintptr(maxUintptr /* ERROR "cannot convert" */ + 1)
222 _ = uintptr(smallestFloat64 /* ERROR "cannot convert" */)
223 )
224
225 const (
226 _ float32 = minInt64
227 _ float64 = minInt64
228 _ complex64 = minInt64
229 _ complex128 = minInt64
230
231 _ = float32(minInt64)
232 _ = float64(minInt64)
233 _ = complex64(minInt64)
234 _ = complex128(minInt64)
235 )
236
237 const (
238 _ float32 = maxUint64
239 _ float64 = maxUint64
240 _ complex64 = maxUint64
241 _ complex128 = maxUint64
242
243 _ = float32(maxUint64)
244 _ = float64(maxUint64)
245 _ = complex64(maxUint64)
246 _ = complex128(maxUint64)
247 )
248
249 // TODO(gri) find smaller deltas below
250
251 const delta32 = maxFloat32/(1 << 23)
252
253 const (
254 _ float32 = - /* ERROR "overflow" */ (maxFloat32 + delta32)
255 _ float32 = -maxFloat32
256 _ float32 = maxFloat32
257 _ float32 = maxFloat32 /* ERROR "overflow" */ + delta32
258
259 _ = float32(- /* ERROR "cannot convert" */ (maxFloat32 + delta32))
260 _ = float32(-maxFloat32)
261 _ = float32(maxFloat32)
262 _ = float32(maxFloat32 /* ERROR "cannot convert" */ + delta32)
263
264 _ = assert(float32(smallestFloat32) == smallestFloat32)
265 _ = assert(float32(smallestFloat32/2) == 0)
266 _ = assert(float32(smallestFloat64) == 0)
267 _ = assert(float32(smallestFloat64/2) == 0)
268 )
269
270 const delta64 = maxFloat64/(1 << 52)
271
272 const (
273 _ float64 = - /* ERROR "overflow" */ (maxFloat64 + delta64)
274 _ float64 = -maxFloat64
275 _ float64 = maxFloat64
276 _ float64 = maxFloat64 /* ERROR "overflow" */ + delta64
277
278 _ = float64(- /* ERROR "cannot convert" */ (maxFloat64 + delta64))
279 _ = float64(-maxFloat64)
280 _ = float64(maxFloat64)
281 _ = float64(maxFloat64 /* ERROR "cannot convert" */ + delta64)
282
283 _ = assert(float64(smallestFloat32) == smallestFloat32)
284 _ = assert(float64(smallestFloat32/2) == smallestFloat32/2)
285 _ = assert(float64(smallestFloat64) == smallestFloat64)
286 _ = assert(float64(smallestFloat64/2) == 0)
287 )
288
289 const (
290 _ complex64 = - /* ERROR "overflow" */ (maxFloat32 + delta32)
291 _ complex64 = -maxFloat32
292 _ complex64 = maxFloat32
293 _ complex64 = maxFloat32 /* ERROR "overflow" */ + delta32
294
295 _ = complex64(- /* ERROR "cannot convert" */ (maxFloat32 + delta32))
296 _ = complex64(-maxFloat32)
297 _ = complex64(maxFloat32)
298 _ = complex64(maxFloat32 /* ERROR "cannot convert" */ + delta32)
299 )
300
301 const (
302 _ complex128 = - /* ERROR "overflow" */ (maxFloat64 + delta64)
303 _ complex128 = -maxFloat64
304 _ complex128 = maxFloat64
305 _ complex128 = maxFloat64 /* ERROR "overflow" */ + delta64
306
307 _ = complex128(- /* ERROR "cannot convert" */ (maxFloat64 + delta64))
308 _ = complex128(-maxFloat64)
309 _ = complex128(maxFloat64)
310 _ = complex128(maxFloat64 /* ERROR "cannot convert" */ + delta64)
311 )
312
313 // Initialization of typed constant and conversion are the same:
314 const (
315 f32 = 1 + smallestFloat32
316 x32 float32 = f32
317 y32 = float32(f32)
318 _ = assert(x32 - y32 == 0)
319 )
320
321 const (
322 f64 = 1 + smallestFloat64
323 x64 float64 = f64
324 y64 = float64(f64)
325 _ = assert(x64 - y64 == 0)
326 )
327
328 const (
329 _ = int8(-1) << 7
330 _ = int8 /* ERROR "overflows" */ (-1) << 8
331
332 _ = uint32(1) << 31
333 _ = uint32 /* ERROR "overflows" */ (1) << 32
334 )
335
View as plain text