On Wed, 9 Dec 2015 18:35:13 +0300
Vasiliy Tolstov <
v.to...@selfip.ru> wrote:
> In C i can calloc zero buffer and doing memcmp (that optimized for
> hardware) How can i do this in go (not use CGO) ? For simplify buffer
> is 4K ?
Write a no-brainer doing memcmp(), compile it with `gcc -O2 -static`
then call `objdump -D` on the resulting binary and search the result
for "memcmp". You can then snatch the generated assembly code and
massage it to make it consumable by the Go assembler (observe that Go
calling conventions are different from C's).
On my platform (amd64) gcc 4.9.2 (libc6 2.19), the memcmp code switches
on CPU features and branches off into SSE2, SSE4 etc parts.
But if you inspect that code, you'll find that it's not magic -- in
the sense it does not somehow map to a single CPU instruction, -- and
it's quite complicated. To me, it appears to be way more complicated
than what Go 1.5.2 generates for the range loop of
b := make([]byte, 4096)
for _, c := range b {
if c != 0 {
break
}
}
(call `go build -gcflags='-S' ...` to inspect it).
Not sure how the speeds of execution differ though but I'd not expect
them to be dramatically different for 4k-worth buffers.
4G would really be more interesting.