-
Notifications
You must be signed in to change notification settings - Fork 38
/
cstd.go
739 lines (636 loc) · 14.8 KB
/
cstd.go
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
package program
import (
"fmt"
"strings"
)
type stdFunction struct {
cFunc string
includeHeader string
functionBody string
dependPackages []string
dependFuncStd []string
}
func init() {
source := `
//---
// fmax returns the larger of its arguments: either x or y.
// c function : double fmax(double , double )
// dep pkg :
// dep func :
func fmax(x, y float64) float64 {
if x < y {
return y
}
return x
}
//---
// fmaxl returns the larger of its arguments: either x or y.
// c function : long double fmaxl(long double , long double )
// dep pkg :
// dep func :
func fmaxl(x, y float64) float64 {
if x < y {
return y
}
return x
}
//---
// cbrt compute cube root
// c function : float cbrtf(float)
// dep pkg : math
// dep func :
func cbrtf(x float32) float32 {
return float32(math.Cbrt(float64(x)))
}
//---
// BoolToInt converts boolean value to an int, which is a common operation in C.
// 0 and 1 represent false and true respectively.
// c function : int BoolToInt(int)
// dep pkg :
// dep func :
func BoolToInt(x bool) int32 {
if x {
return 1
}
return 0
}
//---
// fma returns x*y+z.
// c function : double fma(double, double, double)
// dep pkg :
// dep func :
func fma(x, y, z float64) float64 {
return x*y + z
}
//---
// fmal returns x*y+z.
// c function : long double fmal(long double, long double, long double)
// dep pkg :
// dep func :
func fmal(x, y, z float64) float64 {
return x*y + z
}
//---
// fmaf returns x*y+z.
// c function : float fmaf(float, float, float)
// dep pkg :
// dep func :
func fmaf(x, y, z float32) float32 {
return x*y + z
}
//---
// realloc is function from stdlib.h.
// c function : void * realloc(void* , size_t )
// dep pkg : reflect
// dep func : memcpy
func realloc(ptr interface{}, size uint32) interface{} {
if ptr == nil {
return make([]byte, size)
}
elemType := reflect.TypeOf(ptr).Elem()
ptrNew := reflect.MakeSlice(reflect.SliceOf(elemType), int(size), int(size)).Interface()
// copy elements
memcpy(ptrNew, ptr, size)
return ptrNew
}
//---
// memcpy is function from string.h.
// c function : void * memcpy( void * , const void * , size_t )
// dep pkg : reflect
// dep func :
func memcpy(dst, src interface{}, size uint32) interface{} {
switch reflect.TypeOf(src).Kind() {
case reflect.Slice:
s := reflect.ValueOf(src)
d := reflect.ValueOf(dst)
if s.Len() == 0 {
return dst
}
if s.Len() > 0 {
size /= uint32(int(s.Index(0).Type().Size()))
}
var val reflect.Value
for i := 0; i < int(size); i++ {
if i < s.Len() {
val = s.Index(i)
}
d.Index(i).Set(val)
}
}
return dst
}
//---
// __assert_fail from assert.h
// c function : bool __assert_fail(const char*, const char*, unsigned int, const char*)
// dep pkg : fmt os github.com/Konstantin8105/c4go/noarch
// dep func :
func __assert_fail(
expression, filePath []byte,
lineNumber uint32,
functionName []byte,
) bool {
fmt.Fprintf(
os.Stderr,
"a.out: %s:%d: %s: Assertion %s%s' failed.\n",
noarch.CStringToString(filePath),
lineNumber,
noarch.CStringToString(functionName),
string(byte(96)),
noarch.CStringToString(expression),
)
os.Exit(134)
return true
}
//---
// tolower from ctype.h
// c function : int tolower(int)
// dep pkg : unicode
// dep func :
func tolower (_c int32) int32 {
return int32(unicode.ToLower(rune(_c)))
}
//---
// toupper from ctype.h
// c function : int toupper(int)
// dep pkg : unicode
// dep func :
func toupper(_c int32) int32 {
return int32(unicode.ToUpper(rune(_c)))
}
//---
// __isnanf from math.h
// c function : int __isnanf(float)
// dep pkg : math
// dep func : BoolToInt
func __isnanf(x float32) int32 {
return BoolToInt(math.IsNaN(float64(x)))
}
//---
// __isinff from math.h
// c function : int __isinff(float)
// dep pkg : math
// dep func : BoolToInt
func __isinff(x float32) int32 {
return BoolToInt(math.IsInf(float64(x), 0))
}
//---
// __nanf from math.h
// c function : double __nanf(const char *)
// dep pkg : math
// dep func :
func __nanf(_ []byte) float64 {
return math.NaN()
}
//---
// __inff from math.h
// c function : float __inff()
// dep pkg : math
// dep func :
func __inff() float32 {
return float32(math.Inf(0))
}
//---
// __isinf from math.h
// c function : int __isinf(double)
// dep pkg : math
// dep func : BoolToInt
func __isinf(x float64) int32 {
return BoolToInt(math.IsInf(x, 0))
}
//---
// __builtin_isinf_sign from math.h
// c function : int __builtin_isinf_sign(float)
// dep pkg : math
// dep func : BoolToInt
func __builtin_isinf_sign(x float32) int32 {
return BoolToInt(math.IsInf(float64(x), 0))
}
//---
// __isinfl from math.h
// c function : int __isinfl(long double)
// dep pkg : math
// dep func : BoolToInt
func __isinfl(x float64) int32 {
return BoolToInt(math.IsInf(x, 0))
}
//---
// __signbit from math.h
// c function : int __signbit(double)
// dep pkg : math
// dep func : BoolToInt
func __signbit(x float64) int32 {
return BoolToInt(math.Signbit(x))
}
//---
// __signbitd from math.h
// c function : int __signbitd(double)
// dep pkg : math
// dep func : BoolToInt
func __signbitd(x float64) int32 {
return BoolToInt(math.Signbit(x))
}
//---
// __signbitl from math.h
// c function : int __signbitl(long double)
// dep pkg : math
// dep func : BoolToInt
func __signbitl(x float64) int32 {
return BoolToInt(math.Signbit(x))
}
//---
// __signbitf ...
// c function : int __signbitf(float)
// dep pkg : math
// dep func : BoolToInt
func __signbitf(x float32) int32 {
return BoolToInt(math.Signbit(float64(x)))
}
//---
// __isnanl from math.h
// c function : int __isnanl(long double)
// dep pkg : math
// dep func : BoolToInt
func __isnanl(x float64) int32 {
return BoolToInt(math.IsNaN(x))
}
//---
// __isnan from math.h
// c function : int __isnan(double)
// dep pkg : math
// dep func : BoolToInt
func __isnan(x float64) int32 {
return BoolToInt(math.IsNaN(x))
}
//---
// sinhf from math.h
// c function : float sinhf(float)
// dep pkg : math
// dep func :
// sinhf compute hyperbolic sine
func sinhf(a float32) float32 {
return float32(math.Sinh(float64(a)))
}
//---
// coshf from math.h
// c function : float coshf(float)
// dep pkg : math
// dep func :
// coshf compute hyperbolic cose
func coshf(a float32) float32 {
return float32(math.Cosh(float64(a)))
}
//---
// tanhf from math.h
// c function : float tanhf(float)
// dep pkg : math
// dep func :
// tanhf compute hyperbolic tan
func tanhf(a float32) float32 {
return float32(math.Tanh(float64(a)))
}
//---
// hypotf from math.h
// c function : float hypotf(float)
// dep pkg : math
// dep func :
// hypotf compute the square root of the sum of the squares of x and y
func hypotf(x, y float32) float32 {
return float32(math.Hypot(float64(x), float64(y)))
}
//---
// copysignf from math.h
// c function : float copysignf(float, float)
// dep pkg : math
// dep func :
// copysignf copies sign of y to absolute value of x
func copysignf(x float32, y float32) float32 {
return float32(math.Copysign(float64(x), float64(y)))
}
//---
// expf from math.h
// c function : float expf(float )
// dep pkg : math
// dep func :
// expf : finds e^x
func expf(x float32) float32 {
return float32(math.Exp(float64(x)))
}
//---
// erff from math.h
// c function : float erff(float )
// dep pkg : math
// dep func :
// erff : finds error function value of x
func erff(x float32) float32 {
return float32(math.Erf(float64(x)))
}
//---
// erfcf from math.h
// c function : float erfcf(float )
// dep pkg : math
// dep func :
// erfcf : finds error function value of x
func erfcf(x float32) float32 {
return float32(math.Erfc(float64(x)))
}
//---
// lround from math.h
// c function : long int lround(double )
// dep pkg :
// dep func : lroundl
func lround(x float32) int32 {
return lroundl(float64(x))
}
//---
// lroundf from math.h
// c function : long int lroundf(float )
// dep pkg :
// dep func : lroundl
func lroundf(x float32) int32 {
return lroundl(float64(x))
}
//---
// lroundl from math.h
// c function : long int lroundl(long double )
// dep pkg : math
// dep func :
func lroundl(x float64) int32 {
return int32(math.Round(x))
}
//---
// fmin from math.h
// c function : double fmin(double , double )
// dep pkg :
// dep func :
// fmin returns the smaller of its arguments: either x or y.
func fmin(x, y float64) float64 {
if x < y {
return x
}
return y
}
//---
// fminl from math.h
// c function : double fminl(long double , long double )
// dep pkg :
// dep func :
// fmin returns the smaller of its arguments: either x or y.
func fminl(x, y float64) float64 {
if x < y {
return x
}
return y
}
//---
// fminf from math.h
// c function : float fminf(float , float )
// dep pkg :
// dep func :
// fminf returns the smaller of its arguments: either x or y.
func fminf(x, y float32) float32 {
if x < y {
return x
}
return y
}
//---
// fmaxf from math.h
// c function : float fmaxf(float , float )
// dep pkg :
// dep func :
// fmaxf returns the larger of its arguments: either x or y.
func fmaxf(x, y float32) float32 {
if x < y {
return y
}
return x
}
//---
// fdiml from math.h
// c function : long double fdiml(long double,long double)
// dep pkg :
// dep func :
// fdiml returns the positive difference between x and y.
func fdiml(x, y float64) float64 {
if x > y {
return x - y
}
return 0
}
//---
// fdim from math.h
// c function : double fdim(double, double)
// dep pkg :
// dep func :
// fdim returns the positive difference between x and y.
func fdim(x, y float64) float64 {
if x > y {
return x - y
}
return 0
}
//---
// fdimf from math.h
// c function : float fdimf(float, float)
// dep pkg :
// dep func :
// fdimf returns the positive difference between x and y.
func fdimf(x, y float32) float32 {
if x > y {
return x - y
}
return 0
}
//---
// log2f from math.h
// c function : float log2f(float)
// dep pkg : math
// dep func :
// log2f returns the binary (base-2) logarithm of x.
func log2f(x float32) float32 {
return float32(math.Log2(float64(x)))
}
//---
// log1pf from math.h
// c function : float log1pf(float)
// dep pkg : math
// dep func :
// log1pf compute ln(1+arg)
func log1pf(arg float32) float32 {
return float32(math.Log1p(float64(arg)))
}
//---
// llround from math.h
// c function : long long int llround(double )
// dep pkg :
// dep func : llroundl
func llround(x float32) int64 {
return llroundl(float64(x))
}
//---
// llroundf from math.h
// c function : long long int llroundf(float )
// dep pkg :
// dep func : llroundl
func llroundf(x float32) int64 {
return llroundl(float64(x))
}
//---
// llroundl from math.h
// c function : long long int llroundl(long double )
// dep pkg : math
// dep func :
func llroundl(x float64) int64 {
return int64(math.Round(x))
}
//---
// expm1f from math.h
// c function : float expm1f(float)
// dep pkg : math
// dep func :
// expm1f returns e raised to the power x minus one: e^x-1
func expm1f(x float32) float32 {
return float32(math.Expm1(float64(x)))
}
//---
// exp2f from math.h
// c function : float exp2f(float)
// dep pkg : math
// dep func :
// exp2f Returns the base-2 exponential function of x, which is 2 raised
// to the power x: 2^x
func exp2f(x float32) float32 {
return float32(math.Exp2(float64(x)))
}
//---
// __ctype_b_loc from ctype.h
// c function : const unsigned short int** __ctype_b_loc()
// dep pkg : unicode
// dep func :
func __ctype_b_loc() [][]uint16 {
var characterTable []uint16
for i := 0; i < 255; i++ {
var c uint16
// Each of the bitwise expressions below were copied from the enum
// values, like _ISupper, etc.
if unicode.IsUpper(rune(i)) {
c |= ((1 << (0)) << 8)
}
if unicode.IsLower(rune(i)) {
c |= ((1 << (1)) << 8)
}
if unicode.IsLetter(rune(i)) {
c |= ((1 << (2)) << 8)
}
if unicode.IsDigit(rune(i)) {
c |= ((1 << (3)) << 8)
}
if unicode.IsDigit(rune(i)) ||
(i >= 'a' && i <= 'f') ||
(i >= 'A' && i <= 'F') {
// IsXDigit. This is the same implementation as the Mac version.
// There may be a better way to do this.
c |= ((1 << (4)) << 8)
}
if unicode.IsSpace(rune(i)) {
c |= ((1 << (5)) << 8)
}
if unicode.IsPrint(rune(i)) {
c |= ((1 << (6)) << 8)
}
// The IsSpace check is required because Go treats spaces as graphic
// characters, which C does not.
if unicode.IsGraphic(rune(i)) && !unicode.IsSpace(rune(i)) {
c |= ((1 << (7)) << 8)
}
// http://www.cplusplus.com/reference/cctype/isblank/
// The standard "C" locale considers blank characters the tab
// character ('\t') and the space character (' ').
if i == int('\t') || i == int(' ') {
c |= ((1 << (8)) >> 8)
}
if unicode.IsControl(rune(i)) {
c |= ((1 << (9)) >> 8)
}
if unicode.IsPunct(rune(i)) {
c |= ((1 << (10)) >> 8)
}
if unicode.IsLetter(rune(i)) || unicode.IsDigit(rune(i)) {
c |= ((1 << (11)) >> 8)
}
// Yes, I know this is a hideously slow way to do it but I just want to
// test if this works right now.
characterTable = append(characterTable, c)
}
return [][]uint16{characterTable}
}
`
// split source by parts
var (
splitter = "//---"
cFunc = "c function :"
depPkg = "dep pkg :"
depFunc = "dep func :"
)
cs := strings.Split(source, splitter)
// Examples:
// __signbitf ...
// __inline_signbitf ...
// __builtin_signbitf ...
to := []string{"__inline_", "__builtin_"}
for i, size := 0, len(cs); i < size; i++ {
if !strings.Contains(cs[i], "__") {
continue
}
for j := range to {
part := strings.Replace(cs[i], "__", to[j], -1)
cs = append(cs, part)
}
}
for _, c := range cs {
if strings.TrimSpace(c) == "" {
continue
}
var s stdFunction
s.functionBody = c
lines := strings.Split(c, "\n")
var foundCfunc, foundDepPkg, foundDepFunc bool
for _, line := range lines {
if index := strings.Index(line, cFunc); index > 0 {
line = line[index+len(cFunc):]
s.cFunc = strings.TrimSpace(line)
if line == "" {
panic(fmt.Errorf("no function name : %v", lines))
}
foundCfunc = true
}
if index := strings.Index(line, depPkg); index > 0 {
line = line[index+len(depPkg):]
pkgs := strings.Split(line, " ")
for _, pkg := range pkgs {
if pkg == "" || strings.TrimSpace(pkg) == "" {
continue
}
s.dependPackages = append(s.dependPackages, pkg)
}
foundDepPkg = true
}
if index := strings.Index(line, depFunc); index > 0 {
line = line[index+len(depFunc):]
funcs := strings.Split(line, " ")
for _, f := range funcs {
if f == "" || strings.TrimSpace(f) == "" {
continue
}
s.dependFuncStd = append(s.dependFuncStd, f)
}
foundDepFunc = true
}
}
if !foundCfunc || !foundDepPkg || !foundDepFunc {
panic(c)
}
std = append(std, s)
}
}
var std []stdFunction