blob: a4de4602538cef7783067c0392ecfd95fc0f6eb8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
package runtime
import (
"internal/task"
"unsafe"
)
type stringer interface {
String() string
}
// Lock to make sure print calls do not interleave.
// This is a no-op lock on systems that do not have parallelism.
var printLock task.PMutex
func printlock() {
printLock.Lock()
}
func printunlock() {
printLock.Unlock()
}
//go:nobounds
func printstring(s string) {
for i := 0; i < len(s); i++ {
putchar(s[i])
}
}
func printuint8(n uint8) {
if TargetBits >= 32 {
printuint32(uint32(n))
} else {
prevdigits := n / 10
if prevdigits != 0 {
printuint8(prevdigits)
}
putchar(byte((n % 10) + '0'))
}
}
func printint8(n int8) {
if TargetBits >= 32 {
printint32(int32(n))
} else {
if n < 0 {
putchar('-')
n = -n
}
printuint8(uint8(n))
}
}
func printuintptr(n uintptr) {
switch unsafe.Sizeof(n) {
case 2:
printuint16(uint16(n))
case 4:
printuint32(uint32(n))
case 8:
printuint64(uint64(n))
}
}
func printuint16(n uint16) {
printuint32(uint32(n))
}
func printint16(n int16) {
printint32(int32(n))
}
func printuint32(n uint32) {
printuint64(uint64(n))
}
func printint32(n int32) {
// Print integer in signed big-endian base-10 notation, for humans to
// read.
if n < 0 {
putchar('-')
n = -n
}
printuint32(uint32(n))
}
//go:nobounds
func printuint64(n uint64) {
digits := [20]byte{} // enough to hold (2^64)-1
// Fill in all 10 digits.
firstdigit := 19 // digit index that isn't zero (by default, the last to handle '0' correctly)
for i := 19; i >= 0; i-- {
digit := byte(n%10 + '0')
digits[i] = digit
if digit != '0' {
firstdigit = i
}
n /= 10
}
// Print digits without the leading zeroes.
for i := firstdigit; i < 20; i++ {
putchar(digits[i])
}
}
func printint64(n int64) {
if n < 0 {
putchar('-')
n = -n
}
printuint64(uint64(n))
}
// printfloat32() was copied from the relevant source in the original Go
// implementation and modified to work with float32 instead of float64. It is
// copyright by the Go authors, licensed under the same BSD 3-clause license.
// See https://golang.org/LICENSE for details.
//
// It is a near-duplicate of printfloat64. This is done so that printing a
// float32 value doesn't involve float64 routines, which can be unexpected and a
// problem sometimes. It comes with a possible code size reduction if both
// printfloat32 and printfloat64 are used, which seems uncommon.
//
// Source:
// https://github.com/golang/go/blob/master/src/runtime/print.go
func printfloat32(v float32) {
switch {
case v != v:
printstring("NaN")
return
case v+v == v && v > 0:
printstring("+Inf")
return
case v+v == v && v < 0:
printstring("-Inf")
return
}
const n = 7 // digits printed
var buf [n + 7]byte
buf[0] = '+'
e := 0 // exp
if v == 0 {
if 1/v < 0 {
buf[0] = '-'
}
} else {
if v < 0 {
v = -v
buf[0] = '-'
}
// normalize
for v >= 10 {
e++
v /= 10
}
for v < 1 {
e--
v *= 10
}
// round
h := float32(5.0)
for i := 0; i < n; i++ {
h /= 10
}
v += h
if v >= 10 {
e++
v /= 10
}
}
// format +d.dddd+edd
for i := 0; i < n; i++ {
s := int(v)
buf[i+2] = byte(s + '0')
v -= float32(s)
v *= 10
}
buf[1] = buf[2]
buf[2] = '.'
buf[n+2] = 'e'
buf[n+3] = '+'
if e < 0 {
e = -e
buf[n+3] = '-'
}
buf[n+4] = byte(e/100) + '0'
buf[n+5] = byte(e/10)%10 + '0'
buf[n+6] = byte(e%10) + '0'
for _, c := range buf {
putchar(c)
}
}
// printfloat64() was copied from the relevant source in the original Go
// implementation. It is copyright by the Go authors, licensed under the same
// BSD 3-clause license. See https://golang.org/LICENSE for details.
//
// Source:
// https://github.com/golang/go/blob/master/src/runtime/print.go
func printfloat64(v float64) {
switch {
case v != v:
printstring("NaN")
return
case v+v == v && v > 0:
printstring("+Inf")
return
case v+v == v && v < 0:
printstring("-Inf")
return
}
const n = 7 // digits printed
var buf [n + 7]byte
buf[0] = '+'
e := 0 // exp
if v == 0 {
if 1/v < 0 {
buf[0] = '-'
}
} else {
if v < 0 {
v = -v
buf[0] = '-'
}
// normalize
for v >= 10 {
e++
v /= 10
}
for v < 1 {
e--
v *= 10
}
// round
h := 5.0
for i := 0; i < n; i++ {
h /= 10
}
v += h
if v >= 10 {
e++
v /= 10
}
}
// format +d.dddd+edd
for i := 0; i < n; i++ {
s := int(v)
buf[i+2] = byte(s + '0')
v -= float64(s)
v *= 10
}
buf[1] = buf[2]
buf[2] = '.'
buf[n+2] = 'e'
buf[n+3] = '+'
if e < 0 {
e = -e
buf[n+3] = '-'
}
buf[n+4] = byte(e/100) + '0'
buf[n+5] = byte(e/10)%10 + '0'
buf[n+6] = byte(e%10) + '0'
for _, c := range buf {
putchar(c)
}
}
func printcomplex64(c complex64) {
putchar('(')
printfloat32(real(c))
printfloat32(imag(c))
printstring("i)")
}
func printcomplex128(c complex128) {
putchar('(')
printfloat64(real(c))
printfloat64(imag(c))
printstring("i)")
}
func printspace() {
putchar(' ')
}
func printnl() {
if baremetal {
putchar('\r')
}
putchar('\n')
}
func printitf(msg interface{}) {
switch msg := msg.(type) {
case bool:
printbool(msg)
case int:
switch unsafe.Sizeof(msg) {
case 8:
printint64(int64(msg))
case 4:
printint32(int32(msg))
}
case int8:
printint8(msg)
case int16:
printint16(msg)
case int32:
printint32(msg)
case int64:
printint64(msg)
case uint:
switch unsafe.Sizeof(msg) {
case 8:
printuint64(uint64(msg))
case 4:
printuint32(uint32(msg))
}
case uint8:
printuint8(msg)
case uint16:
printuint16(msg)
case uint32:
printuint32(msg)
case uint64:
printuint64(msg)
case uintptr:
printuintptr(msg)
case float32:
printfloat32(msg)
case float64:
printfloat64(msg)
case complex64:
printcomplex64(msg)
case complex128:
printcomplex128(msg)
case string:
printstring(msg)
case error:
printstring(msg.Error())
case stringer:
printstring(msg.String())
default:
// cast to underlying type
itf := *(*_interface)(unsafe.Pointer(&msg))
putchar('(')
printuintptr(uintptr(itf.typecode))
putchar(':')
printptr(uintptr(itf.value))
putchar(')')
}
}
func printmap(m *hashmap) {
printstring("map[")
if m == nil {
printstring("nil")
} else {
switch unsafe.Sizeof(m.count) {
case 8:
printuint64(uint64(m.count))
case 4:
printuint32(uint32(m.count))
case 2:
printuint16(uint16(m.count))
}
}
putchar(']')
}
func printptr(ptr uintptr) {
if ptr == 0 {
printstring("nil")
return
}
putchar('0')
putchar('x')
for i := 0; i < int(unsafe.Sizeof(ptr))*2; i++ {
nibble := byte(ptr >> (unsafe.Sizeof(ptr)*8 - 4))
if nibble < 10 {
putchar(nibble + '0')
} else {
putchar(nibble - 10 + 'a')
}
ptr <<= 4
}
}
func printbool(b bool) {
if b {
printstring("true")
} else {
printstring("false")
}
}
func printslice(ptr, len_, cap_ uintptr) {
putchar('[')
printuintptr(len_)
putchar('/')
printuintptr(cap_)
putchar(']')
printptr(ptr)
}
|