diff options
Diffstat (limited to 'libgo/go/internal/bytealg/bytealg.c')
-rw-r--r-- | libgo/go/internal/bytealg/bytealg.c | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/libgo/go/internal/bytealg/bytealg.c b/libgo/go/internal/bytealg/bytealg.c new file mode 100644 index 00000000000..39c060faf0b --- /dev/null +++ b/libgo/go/internal/bytealg/bytealg.c @@ -0,0 +1,116 @@ +/* bytealg.c -- gccgo implementations of bytealg functions + + Copyright 2018 The Go Authors. All rights reserved. + Use of this source code is governed by a BSD-style + license that can be found in the LICENSE file. */ + +#include <stddef.h> +#include <string.h> + +#include "runtime.h" +#include "array.h" + +intgo Compare(struct __go_open_array, struct __go_open_array) + __asm__(GOSYM_PREFIX "internal_bytealg.Compare") + __attribute__((no_split_stack)); + +intgo Compare(struct __go_open_array a, struct __go_open_array b) +{ + intgo l; + + l = a.__count; + if (b.__count < l) { + l = b.__count; + } + if (l > 0 && a.__values != b.__values) { + int i; + + i = __builtin_memcmp(a.__values, b.__values, (size_t)(l)); + if (i != 0) { + return i; + } + } + if (a.__count < b.__count) { + return -1; + } + if (a.__count > b.__count) { + return +1; + } + return 0; +} + +_Bool Equal(struct __go_open_array, struct __go_open_array) + __asm__(GOSYM_PREFIX "internal_bytealg.Equal") + __attribute__((no_split_stack)); + +_Bool Equal(struct __go_open_array a, struct __go_open_array b) +{ + intgo l; + + l = a.__count; + if (l != b.__count) { + return 0; + } + return __builtin_memcmp(a.__values, b.__values, l) == 0; +} + +intgo IndexByte(struct __go_open_array, byte) + __asm__(GOSYM_PREFIX "internal_bytealg.IndexByte") + __attribute__((no_split_stack)); + +intgo IndexByte(struct __go_open_array b, byte c) +{ + const byte *p; + + p = __builtin_memchr(b.__values, c, (size_t)(b.__count)); + if (p == nil) { + return -1; + } + return p - (byte *)(b.__values); +} + + +intgo IndexByteString(String, byte) + __asm__(GOSYM_PREFIX "internal_bytealg.IndexByteString") + __attribute__((no_split_stack)); + +intgo IndexByteString(String s, byte c) +{ + const byte *p; + + p = __builtin_memchr(s.str, c, (size_t)(s.len)); + if (p == nil) { + return -1; + } + return p - s.str; +} + +intgo Index(struct __go_open_array, struct __go_open_array) + __asm__(GOSYM_PREFIX "internal_bytealg.Index") + __attribute__((no_split_stack)); + +intgo Index(struct __go_open_array a, struct __go_open_array b) +{ + const byte* p; + + p = memmem(a.__values, a.__count, b.__values, b.__count); + if (p == nil) { + return -1; + } + return p - (const byte*)(a.__values); +} + +intgo IndexString(String, String) + __asm__(GOSYM_PREFIX "internal_bytealg.IndexString") + __attribute__((no_split_stack)); + +intgo IndexString(String a, String b) +{ + const byte* p; + + p = memmem(a.str, a.len, b.str, b.len); + if (p == nil) { + return -1; + } + return p - a.str; +} |