Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
sqrtf.c, vswprintf.c, popen.c
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Jul 28, 2022
1 parent 50a3aaa commit f7e7195
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 11 deletions.
22 changes: 22 additions & 0 deletions c2go.a.pub
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
FILE
_Exit
__DOUBLE_BITS
__FLOAT_BITS
__builtin_inf
__builtin_inff
__builtin_nan
__builtin_nanf
__ctype_get_mb_cur_max
__fpclassify
__fpclassifyf
__fpclassifyl
__isgreater
__isgreaterequal
__isgreaterequalf
__isgreaterequall
__isgreaterf
__isgreaterl
__isless
__islessequal
__islessequalf
__islessequall
__islessf
__islessgreater
__islessgreaterf
__islessgreaterl
__islessl
__isoc_va_list
__isspace
__signbit
__signbitf
__signbitl
a64l
abort
Expand Down
5 changes: 1 addition & 4 deletions c2go.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,14 @@
"./src/misc/syslog.c",
"./src/misc/uname.c",
"./src/misc/realpath.c",
"./src/math/sqrtf.c",
"./src/stdio/puts.c",
"./src/stdio/vswprintf.c",
"./src/stdio/vfscanf.c",
"./src/stdio/vfwscanf.c",
"./src/stdio/vfwprintf.c",
"./src/stdio/freopen.c",
"./src/stdio/ftrylockfile.c",
"./src/stdio/open_memstream.c",
"./src/stdio/open_wmemstream.c",
"./src/stdio/popen.c"
"./src/stdio/open_wmemstream.c"
]
},
"dirs": [
Expand Down
19 changes: 12 additions & 7 deletions c2go_autogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ func Mblen(*int8, uint64) int32 {
func Mbtowc(*uint32, *int8, uint64) int32 {
panic("notimpl")
}
func Sqrtf(float32) float32 {
panic("notimpl")
}
func Vfscanf(*Struct__IO_FILE, *int8, []interface {
}) int32 {
panic("notimpl")
Expand Down Expand Up @@ -112,6 +109,18 @@ func open(*int8, int32, ...interface {
func pipe2(fd *int32, flag int32) int32 {
panic("notimpl")
}
func posix_spawn(*int32, *int8, *_cgoa_19_popen, *_cgoa_18_popen, **int8, **int8) int32 {
panic("notimpl")
}
func posix_spawn_file_actions_adddup2(*_cgoa_19_popen, int32, int32) int32 {
panic("notimpl")
}
func posix_spawn_file_actions_destroy(*_cgoa_19_popen) int32 {
panic("notimpl")
}
func posix_spawn_file_actions_init(*_cgoa_19_popen) int32 {
panic("notimpl")
}
func pthread_setcancelstate(int32, *int32) int32 {
panic("notimpl")
}
Expand Down Expand Up @@ -150,10 +159,6 @@ func vfwscanf(*Struct__IO_FILE, *uint32, []interface {
}) int32 {
panic("notimpl")
}
func vswprintf(*uint32, uint64, *uint32, []interface {
}) int32 {
panic("notimpl")
}
func waitpid(int32, *int32, int32) int32 {
panic("notimpl")
}
Expand Down
17 changes: 17 additions & 0 deletions c2go_header.i.go
Original file line number Diff line number Diff line change
Expand Up @@ -2637,6 +2637,23 @@ func X__isspace(_c int32) int32 {
}()
}

type _cgoa_18_popen struct {
__flags int32
__pgrp int32
__def Struct___sigset_t
__mask Struct___sigset_t
__prio int32
__pol int32
__fn unsafe.Pointer
__pad [56]int8
}
type posix_spawnattr_t = _cgoa_18_popen
type _cgoa_19_popen struct {
__pad0 [2]int32
__actions unsafe.Pointer
__pad [16]int32
}
type posix_spawn_file_actions_t = _cgoa_19_popen
type struct_kstat struct {
st_dev uint64
__st_dev_padding int32
Expand Down
77 changes: 77 additions & 0 deletions popen.c.i.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package libc

import unsafe "unsafe"

func Popen(cmd *int8, mode *int8) *Struct__IO_FILE {
var p [2]int32
var op int32
var e int32
var pid int32
var f *Struct__IO_FILE
var fa _cgoa_19_popen
if int32(*mode) == 'r' {
op = int32(0)
} else if int32(*mode) == 'w' {
op = int32(1)
} else {
*__errno_location() = int32(22)
return (*Struct__IO_FILE)(nil)
}
if pipe2((*int32)(unsafe.Pointer(&p)), int32(524288)) != 0 {
return (*Struct__IO_FILE)(nil)
}
f = Fdopen(*(*int32)(unsafe.Pointer(uintptr(unsafe.Pointer((*int32)(unsafe.Pointer(&p)))) + uintptr(op)*4)), mode)
if !(f != nil) {
__syscall1(int64(6), int64(*(*int32)(unsafe.Pointer(uintptr(unsafe.Pointer((*int32)(unsafe.Pointer(&p)))) + uintptr(int32(0))*4))))
__syscall1(int64(6), int64(*(*int32)(unsafe.Pointer(uintptr(unsafe.Pointer((*int32)(unsafe.Pointer(&p)))) + uintptr(int32(1))*4))))
return (*Struct__IO_FILE)(nil)
}
var __need_unlock int32 = func() int32 {
if f.Lock >= int32(0) {
return __lockfile(f)
} else {
return int32(0)
}
}()
if *(*int32)(unsafe.Pointer(uintptr(unsafe.Pointer((*int32)(unsafe.Pointer(&p)))) + uintptr(int32(1)-op)*4)) == int32(1)-op {
var tmp int32 = fcntl(int32(1)-op, int32(1030), int32(0))
if tmp < int32(0) {
e = *__errno_location()
goto fail
}
__syscall1(int64(6), int64(*(*int32)(unsafe.Pointer(uintptr(unsafe.Pointer((*int32)(unsafe.Pointer(&p)))) + uintptr(int32(1)-op)*4))))
*(*int32)(unsafe.Pointer(uintptr(unsafe.Pointer((*int32)(unsafe.Pointer(&p)))) + uintptr(int32(1)-op)*4)) = tmp
}
e = int32(12)
if !(posix_spawn_file_actions_init(&fa) != 0) {
if !(posix_spawn_file_actions_adddup2(&fa, *(*int32)(unsafe.Pointer(uintptr(unsafe.Pointer((*int32)(unsafe.Pointer(&p)))) + uintptr(int32(1)-op)*4)), int32(1)-op) != 0) {
if !(func() (_cgo_ret int32) {
_cgo_addr := &e
*_cgo_addr = posix_spawn(&pid, (*int8)(unsafe.Pointer(&[8]int8{'/', 'b', 'i', 'n', '/', 's', 'h', '\x00'})), &fa, nil, (**int8)(unsafe.Pointer(&[4]*int8{(*int8)(unsafe.Pointer(&[3]int8{'s', 'h', '\x00'})), (*int8)(unsafe.Pointer(&[3]int8{'-', 'c', '\x00'})), (*int8)(unsafe.Pointer(cmd)), nil})), __environ)
return *_cgo_addr
}() != 0) {
posix_spawn_file_actions_destroy(&fa)
f.Pipe_pid = pid
if !(Strchr(mode, 'e') != nil) {
fcntl(*(*int32)(unsafe.Pointer(uintptr(unsafe.Pointer((*int32)(unsafe.Pointer(&p)))) + uintptr(op)*4)), int32(2), int32(0))
}
__syscall1(int64(6), int64(*(*int32)(unsafe.Pointer(uintptr(unsafe.Pointer((*int32)(unsafe.Pointer(&p)))) + uintptr(int32(1)-op)*4))))
for {
if __need_unlock != 0 {
__unlockfile(f)
}
if true {
break
}
}
return f
}
}
posix_spawn_file_actions_destroy(&fa)
}
fail:
Fclose(f)
__syscall1(int64(6), int64(*(*int32)(unsafe.Pointer(uintptr(unsafe.Pointer((*int32)(unsafe.Pointer(&p)))) + uintptr(int32(1)-op)*4))))
*__errno_location() = e
return (*Struct__IO_FILE)(nil)
}
109 changes: 109 additions & 0 deletions sqrtf.c.i.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package libc

import unsafe "unsafe"

func _cgos_mul32_sqrtf(a uint32, b uint32) uint32 {
return uint32(uint64(a) * uint64(b) >> int32(32))
}
func Sqrtf(x float32) float32 {
var ix uint32
var m uint32
var m1 uint32
var m0 uint32
var even uint32
var ey uint32
ix = *(*uint32)(unsafe.Pointer(&_cgoz_18_sqrtf{x}))
if func() int64 {
if ix-uint32(8388608) >= uint32(2130706432) {
return 1
} else {
return 0
}
}() == int64(0) {
if ix*uint32(2) == uint32(0) {
return x
}
if ix == uint32(2139095040) {
return x
}
if ix > uint32(2139095040) {
return __math_invalidf(x)
}
ix = *(*uint32)(unsafe.Pointer(&_cgoz_19_sqrtf{x * 8388608}))
ix -= uint32(192937984)
}
even = ix & uint32(8388608)
m1 = ix<<int32(8) | uint32(2147483648)
m0 = ix << int32(7) & uint32(2147483647)
m = func() uint32 {
if even != 0 {
return m0
} else {
return m1
}
}()
ey = ix >> int32(1)
ey += uint32(532676608)
ey &= uint32(2139095040)
const _cgos_three_sqrtf uint32 = uint32(3221225472)
var r uint32
var s uint32
var d uint32
var u uint32
var i uint32
i = ix >> int32(17) % uint32(128)
r = uint32(*(*uint16)(unsafe.Pointer(uintptr(unsafe.Pointer((*uint16)(unsafe.Pointer(&__rsqrt_tab)))) + uintptr(i)*2))) << int32(16)
s = _cgos_mul32_sqrtf(m, r)
d = _cgos_mul32_sqrtf(s, r)
u = _cgos_three_sqrtf - d
r = _cgos_mul32_sqrtf(r, u) << int32(1)
s = _cgos_mul32_sqrtf(s, u) << int32(1)
d = _cgos_mul32_sqrtf(s, r)
u = _cgos_three_sqrtf - d
s = _cgos_mul32_sqrtf(s, u)
s = (s - uint32(1)) >> int32(6)
var d0 uint32
var d1 uint32
var d2 uint32
var y float32
var t float32
d0 = m<<int32(16) - s*s
d1 = s - d0
d2 = d1 + s + uint32(1)
s += d1 >> int32(31)
s &= uint32(8388607)
s |= ey
y = *(*float32)(unsafe.Pointer(&_cgoz_20_sqrtf{s}))
if int32(1) != 0 {
var tiny uint32 = uint32(func() int32 {
if func() int64 {
if d2 == uint32(0) {
return 1
} else {
return 0
}
}() == int64(0) {
return int32(0)
} else {
return int32(16777216)
}
}())
tiny |= (d1 ^ d2) & uint32(2147483648)
t = *(*float32)(unsafe.Pointer(&_cgoz_21_sqrtf{tiny}))
y = eval_as_float(y + t)
}
return y
}

type _cgoz_18_sqrtf struct {
_f float32
}
type _cgoz_19_sqrtf struct {
_f float32
}
type _cgoz_20_sqrtf struct {
_i uint32
}
type _cgoz_21_sqrtf struct {
_i uint32
}
70 changes: 70 additions & 0 deletions vswprintf.c.i.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package libc

import unsafe "unsafe"

type _cgos_cookie_vswprintf struct {
ws *uint32
l uint64
}

func _cgos_sw_write_vswprintf(f *Struct__IO_FILE, s *uint8, l uint64) uint64 {
var l0 uint64 = l
var i int32 = int32(0)
var c *_cgos_cookie_vswprintf = (*_cgos_cookie_vswprintf)(f.Cookie)
if uintptr(unsafe.Pointer(s)) != uintptr(unsafe.Pointer((*uint8)(unsafe.Pointer(f.Wbase)))) && _cgos_sw_write_vswprintf(f, f.Wbase, uint64(uintptr(unsafe.Pointer(f.Wpos))-uintptr(unsafe.Pointer(f.Wbase)))) == uint64(18446744073709551615) {
return uint64(18446744073709551615)
}
for c.l != 0 && l != 0 && func() (_cgo_ret int32) {
_cgo_addr := &i
*_cgo_addr = Mbtowc(c.ws, (*int8)(unsafe.Pointer(s)), l)
return *_cgo_addr
}() >= int32(0) {
*(*uintptr)(unsafe.Pointer(&s)) += uintptr(i)
l -= uint64(i)
c.l--
*(*uintptr)(unsafe.Pointer(&c.ws)) += 4
}
*c.ws = uint32(0)
if i < int32(0) {
f.Wpos = func() (_cgo_ret *uint8) {
_cgo_addr := &f.Wbase
*_cgo_addr = func() (_cgo_ret *uint8) {
_cgo_addr := &f.Wend
*_cgo_addr = (*uint8)(nil)
return *_cgo_addr
}()
return *_cgo_addr
}()
f.Flags |= uint32(32)
return uint64(i)
}
f.Wend = (*uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(f.Buf)) + uintptr(f.Buf_size)))
f.Wpos = func() (_cgo_ret *uint8) {
_cgo_addr := &f.Wbase
*_cgo_addr = f.Buf
return *_cgo_addr
}()
return l0
}
func vswprintf(s *uint32, n uint64, fmt *uint32, ap []interface {
}) int32 {
var r int32
var buf [256]uint8
var c _cgos_cookie_vswprintf = _cgos_cookie_vswprintf{s, n - uint64(1)}
var f Struct__IO_FILE = Struct__IO_FILE{0, nil, nil, nil, nil, nil, nil, nil, nil, _cgos_sw_write_vswprintf, nil, (*uint8)(unsafe.Pointer(&buf)), 256, nil, nil, 0, 0, 0, 0, -1, -1, unsafe.Pointer(&c), 0, nil, nil, nil, 0, 0, nil, nil, nil}
if !(n != 0) {
return -1
} else if n > uint64(2147483647) {
*__errno_location() = int32(75)
return -1
}
r = vfwprintf(&f, fmt, ap)
_cgos_sw_write_vswprintf(&f, nil, uint64(0))
return func() int32 {
if uint64(r) >= n {
return -1
} else {
return r
}
}()
}

0 comments on commit f7e7195

Please sign in to comment.