github.com/axw/llgo@v0.0.0-20160805011314-95b5fe4dca20/third_party/gofrontend/libgo/go/bytes/indexbyte.c (about) 1 /* indexbyte.c -- implement bytes.IndexByte for Go. 2 3 Copyright 2009 The Go Authors. All rights reserved. 4 Use of this source code is governed by a BSD-style 5 license that can be found in the LICENSE file. */ 6 7 #include <stddef.h> 8 9 #include "runtime.h" 10 #include "array.h" 11 12 /* This is in C so that the compiler can optimize it appropriately. 13 We deliberately don't split the stack in case it does call the 14 library function, which shouldn't need much stack space. */ 15 16 intgo IndexByte (struct __go_open_array, char) 17 __asm__ (GOSYM_PREFIX "bytes.IndexByte") 18 __attribute__ ((no_split_stack)); 19 20 intgo 21 IndexByte (struct __go_open_array s, char b) 22 { 23 char *p; 24 25 p = __builtin_memchr (s.__values, b, s.__count); 26 if (p == NULL) 27 return -1; 28 return p - (char *) s.__values; 29 } 30 31 /* Comparison. */ 32 33 _Bool Equal (struct __go_open_array a, struct __go_open_array b) 34 __asm__ (GOSYM_PREFIX "bytes.Equal") 35 __attribute__ ((no_split_stack)); 36 37 _Bool 38 Equal (struct __go_open_array a, struct __go_open_array b) 39 { 40 if (a.__count != b.__count) 41 return 0; 42 return __builtin_memcmp (a.__values, b.__values, a.__count) == 0; 43 } 44 45 intgo Compare (struct __go_open_array a, struct __go_open_array b) 46 __asm__ (GOSYM_PREFIX "bytes.Compare") 47 __attribute__ ((no_split_stack)); 48 49 intgo 50 Compare (struct __go_open_array a, struct __go_open_array b) 51 { 52 intgo len; 53 54 len = a.__count; 55 if (len > b.__count) 56 len = b.__count; 57 if (len > 0) 58 { 59 intgo ret; 60 61 ret = __builtin_memcmp (a.__values, b.__values, len); 62 if (ret < 0) 63 return -1; 64 else if (ret > 0) 65 return 1; 66 } 67 if (a.__count < b.__count) 68 return -1; 69 else if (a.__count > b.__count) 70 return 1; 71 else 72 return 0; 73 }