summaryrefslogtreecommitdiff
path: root/ports/sysdeps/tile/tilegx
diff options
context:
space:
mode:
Diffstat (limited to 'ports/sysdeps/tile/tilegx')
-rw-r--r--ports/sysdeps/tile/tilegx/bits/atomic.h49
-rw-r--r--ports/sysdeps/tile/tilegx/bits/wordsize.h8
-rw-r--r--ports/sysdeps/tile/tilegx/memchr.c73
-rw-r--r--ports/sysdeps/tile/tilegx/memcpy.c206
-rw-r--r--ports/sysdeps/tile/tilegx/memset.c151
-rw-r--r--ports/sysdeps/tile/tilegx/memusage.h30
-rw-r--r--ports/sysdeps/tile/tilegx/rawmemchr.c45
-rw-r--r--ports/sysdeps/tile/tilegx/strchr.c67
-rw-r--r--ports/sysdeps/tile/tilegx/strchrnul.c64
-rw-r--r--ports/sysdeps/tile/tilegx/string-endian.h45
-rw-r--r--ports/sysdeps/tile/tilegx/strlen.c39
-rw-r--r--ports/sysdeps/tile/tilegx/strrchr.c68
-rw-r--r--ports/sysdeps/tile/tilegx/tilegx32/Implies3
-rw-r--r--ports/sysdeps/tile/tilegx/tilegx64/Implies3
14 files changed, 851 insertions, 0 deletions
diff --git a/ports/sysdeps/tile/tilegx/bits/atomic.h b/ports/sysdeps/tile/tilegx/bits/atomic.h
new file mode 100644
index 0000000000..270955ac00
--- /dev/null
+++ b/ports/sysdeps/tile/tilegx/bits/atomic.h
@@ -0,0 +1,49 @@
+/* Copyright (C) 2011 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library. If not, see
+ <http://www.gnu.org/licenses/>. */
+
+#ifndef _BITS_ATOMIC_H
+#define _BITS_ATOMIC_H 1
+
+#include <arch/spr_def.h>
+
+/* Pick appropriate 8- or 4-byte instruction. */
+#define __atomic_update(mem, v, op) \
+ ((__typeof (*(mem))) (__typeof (*(mem) - *(mem))) \
+ ((sizeof (*(mem)) == 8) ? \
+ __insn_##op ((void *) (mem), (int64_t) (__typeof((v) - (v))) (v)) : \
+ (sizeof (*(mem)) == 4) ? \
+ __insn_##op##4 ((void *) (mem), (int32_t) (__typeof ((v) - (v))) (v)) : \
+ __atomic_error_bad_argument_size()))
+
+#define atomic_compare_and_exchange_val_acq(mem, n, o) \
+ ({ __insn_mtspr (SPR_CMPEXCH_VALUE, (int64_t) (__typeof ((o) - (o))) (o)); \
+ __atomic_update (mem, n, cmpexch); })
+#define atomic_exchange_acq(mem, newvalue) \
+ __atomic_update (mem, newvalue, exch)
+#define atomic_exchange_and_add(mem, value) \
+ __atomic_update (mem, value, fetchadd)
+#define atomic_and_val(mem, mask) \
+ __atomic_update (mem, mask, fetchand)
+#define atomic_or_val(mem, mask) \
+ __atomic_update (mem, mask, fetchor)
+#define atomic_decrement_if_positive(mem) \
+ __atomic_update (mem, -1, fetchaddgez)
+
+#include <sysdeps/tile/bits/atomic.h>
+
+#endif /* bits/atomic.h */
diff --git a/ports/sysdeps/tile/tilegx/bits/wordsize.h b/ports/sysdeps/tile/tilegx/bits/wordsize.h
new file mode 100644
index 0000000000..5d4e4b4470
--- /dev/null
+++ b/ports/sysdeps/tile/tilegx/bits/wordsize.h
@@ -0,0 +1,8 @@
+/* Determine the wordsize from the preprocessor defines. */
+
+#ifdef __LP64__
+# define __WORDSIZE 64
+# define __WORDSIZE_TIME64_COMPAT32 1
+#else
+# define __WORDSIZE 32
+#endif
diff --git a/ports/sysdeps/tile/tilegx/memchr.c b/ports/sysdeps/tile/tilegx/memchr.c
new file mode 100644
index 0000000000..32f24ec43e
--- /dev/null
+++ b/ports/sysdeps/tile/tilegx/memchr.c
@@ -0,0 +1,73 @@
+/* Copyright (C) 2011-2012 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library. If not, see
+ <http://www.gnu.org/licenses/>. */
+
+#include <string.h>
+#include <stdint.h>
+#include "string-endian.h"
+
+void *
+__memchr (const void *s, int c, size_t n)
+{
+ const uint64_t *last_word_ptr;
+ const uint64_t *p;
+ const char *last_byte_ptr;
+ uintptr_t s_int;
+ uint64_t goal, before_mask, v, bits;
+ char *ret;
+
+ if (__builtin_expect (n == 0, 0))
+ {
+ /* Don't dereference any memory if the array is empty. */
+ return NULL;
+ }
+
+ /* Get an aligned pointer. */
+ s_int = (uintptr_t) s;
+ p = (const uint64_t *) (s_int & -8);
+
+ /* Create eight copies of the byte for which we are looking. */
+ goal = copy_byte(c);
+
+ /* Read the first word, but munge it so that bytes before the array
+ will not match goal. */
+ before_mask = MASK (s_int);
+ v = (*p | before_mask) ^ (goal & before_mask);
+
+ /* Compute the address of the last byte. */
+ last_byte_ptr = (const char *) s + n - 1;
+
+ /* Compute the address of the word containing the last byte. */
+ last_word_ptr = (const uint64_t *) ((uintptr_t) last_byte_ptr & -8);
+
+ while ((bits = __insn_v1cmpeq (v, goal)) == 0)
+ {
+ if (__builtin_expect (p == last_word_ptr, 0))
+ {
+ /* We already read the last word in the array, so give up. */
+ return NULL;
+ }
+ v = *++p;
+ }
+
+ /* We found a match, but it might be in a byte past the end
+ of the array. */
+ ret = ((char *) p) + (CFZ (bits) >> 3);
+ return (ret <= last_byte_ptr) ? ret : NULL;
+}
+weak_alias (__memchr, memchr)
+libc_hidden_builtin_def (memchr)
diff --git a/ports/sysdeps/tile/tilegx/memcpy.c b/ports/sysdeps/tile/tilegx/memcpy.c
new file mode 100644
index 0000000000..dd6e30dd60
--- /dev/null
+++ b/ports/sysdeps/tile/tilegx/memcpy.c
@@ -0,0 +1,206 @@
+/* Copyright (C) 2011-2012 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library. If not, see
+ <http://www.gnu.org/licenses/>. */
+
+#include <string.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <arch/chip.h>
+
+/* Must be 8 bytes in size. */
+#define word_t uint64_t
+
+/* How many cache lines ahead should we prefetch? */
+#define PREFETCH_LINES_AHEAD 3
+
+void *
+__memcpy (void *__restrict dstv, const void *__restrict srcv, size_t n)
+{
+ char *__restrict dst1 = (char *) dstv;
+ const char *__restrict src1 = (const char *) srcv;
+ const char *__restrict src1_end;
+ const char *__restrict prefetch;
+ word_t *__restrict dst8; /* 8-byte pointer to destination memory. */
+ word_t final; /* Final bytes to write to trailing word, if any */
+ long i;
+
+ if (n < 16)
+ {
+ for (; n; n--)
+ *dst1++ = *src1++;
+ return dstv;
+ }
+
+ /* Locate the end of source memory we will copy. Don't prefetch
+ past this. */
+ src1_end = src1 + n - 1;
+
+ /* Prefetch ahead a few cache lines, but not past the end. */
+ prefetch = src1;
+ for (i = 0; i < PREFETCH_LINES_AHEAD; i++)
+ {
+ __insn_prefetch (prefetch);
+ prefetch += CHIP_L2_LINE_SIZE ();
+ prefetch = (prefetch > src1_end) ? prefetch : src1;
+ }
+
+ /* Copy bytes until dst is word-aligned. */
+ for (; (uintptr_t) dst1 & (sizeof (word_t) - 1); n--)
+ *dst1++ = *src1++;
+
+ /* 8-byte pointer to destination memory. */
+ dst8 = (word_t *) dst1;
+
+ if (__builtin_expect ((uintptr_t) src1 & (sizeof (word_t) - 1), 0))
+ {
+ /* Misaligned copy. Copy 8 bytes at a time, but don't bother
+ with other fanciness.
+ TODO: Consider prefetching and using wh64 as well. */
+
+ /* Create an aligned src8. */
+ const word_t *__restrict src8 =
+ (const word_t *) ((uintptr_t) src1 & -sizeof (word_t));
+ word_t b;
+
+ word_t a = *src8++;
+ for (; n >= sizeof (word_t); n -= sizeof (word_t))
+ {
+ b = *src8++;
+ a = __insn_dblalign (a, b, src1);
+ *dst8++ = a;
+ a = b;
+ }
+
+ if (n == 0)
+ return dstv;
+
+ b = ((const char *) src8 <= src1_end) ? *src8 : 0;
+
+ /* Final source bytes to write to trailing partial word, if any. */
+ final = __insn_dblalign (a, b, src1);
+ }
+ else
+ {
+ /* Aligned copy. */
+
+ const word_t *__restrict src8 = (const word_t *) src1;
+
+ /* src8 and dst8 are both word-aligned. */
+ if (n >= CHIP_L2_LINE_SIZE ())
+ {
+ /* Copy until 'dst' is cache-line-aligned. */
+ for (; (uintptr_t) dst8 & (CHIP_L2_LINE_SIZE () - 1);
+ n -= sizeof (word_t))
+ *dst8++ = *src8++;
+
+ /* If copying to self, return. The test is cheap enough
+ that we do it despite the fact that the memcpy() contract
+ doesn't require us to support overlapping dst and src.
+ This is the most common case of overlap, and any close
+ overlap will cause corruption due to the wh64 below.
+ This case is particularly important since the compiler
+ will emit memcpy() calls for aggregate copies even if it
+ can't prove that src != dst. */
+ if (__builtin_expect (dst8 == src8, 0))
+ return dstv;
+
+ for (; n >= CHIP_L2_LINE_SIZE ();)
+ {
+ __insn_wh64 (dst8);
+
+ /* Prefetch and advance to next line to prefetch, but
+ don't go past the end. */
+ __insn_prefetch (prefetch);
+ prefetch += CHIP_L2_LINE_SIZE ();
+ prefetch = (prefetch > src1_end) ? prefetch :
+ (const char *) src8;
+
+ /* Copy an entire cache line. Manually unrolled to
+ avoid idiosyncracies of compiler unrolling. */
+#define COPY_WORD(offset) ({ dst8[offset] = src8[offset]; n -= 8; })
+ COPY_WORD (0);
+ COPY_WORD (1);
+ COPY_WORD (2);
+ COPY_WORD (3);
+ COPY_WORD (4);
+ COPY_WORD (5);
+ COPY_WORD (6);
+ COPY_WORD (7);
+#if CHIP_L2_LINE_SIZE() != 64
+# error "Fix code that assumes particular L2 cache line size."
+#endif
+
+ dst8 += CHIP_L2_LINE_SIZE () / sizeof (word_t);
+ src8 += CHIP_L2_LINE_SIZE () / sizeof (word_t);
+ }
+ }
+
+ for (; n >= sizeof (word_t); n -= sizeof (word_t))
+ *dst8++ = *src8++;
+
+ if (__builtin_expect (n == 0, 1))
+ return dstv;
+
+ final = *src8;
+ }
+
+ /* n != 0 if we get here. Write out any trailing bytes. */
+ dst1 = (char *) dst8;
+#ifndef __BIG_ENDIAN__
+ if (n & 4)
+ {
+ *(uint32_t *) dst1 = final;
+ dst1 += 4;
+ final >>= 32;
+ n &= 3;
+ }
+ if (n & 2)
+ {
+ *(uint16_t *) dst1 = final;
+ dst1 += 2;
+ final >>= 16;
+ n &= 1;
+ }
+ if (n)
+ *(uint8_t *) dst1 = final;
+#else
+ if (n & 4)
+ {
+ *(uint32_t *) dst1 = final >> 32;
+ dst1 += 4;
+ }
+ else
+ {
+ final >>= 32;
+ }
+ if (n & 2)
+ {
+ *(uint16_t *) dst1 = final >> 16;
+ dst1 += 2;
+ }
+ else
+ {
+ final >>= 16;
+ }
+ if (n & 1)
+ *(uint8_t *) dst1 = final >> 8;
+#endif
+
+ return dstv;
+}
+weak_alias (__memcpy, memcpy)
+libc_hidden_builtin_def (memcpy)
diff --git a/ports/sysdeps/tile/tilegx/memset.c b/ports/sysdeps/tile/tilegx/memset.c
new file mode 100644
index 0000000000..dad3eadc5a
--- /dev/null
+++ b/ports/sysdeps/tile/tilegx/memset.c
@@ -0,0 +1,151 @@
+/* Copyright (C) 2011-2012 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library. If not, see
+ <http://www.gnu.org/licenses/>. */
+
+#include <arch/chip.h>
+#include <string.h>
+#include <stdint.h>
+#include "string-endian.h"
+
+void *
+__memset (void *s, int c, size_t n)
+{
+ uint64_t *out64;
+ int n64, to_align64;
+ uint64_t v64;
+ uint8_t *out8 = s;
+
+ /* Experimentation shows that a trivial tight loop is a win up until
+ around a size of 20, where writing a word at a time starts to win. */
+#define BYTE_CUTOFF 20
+
+#if BYTE_CUTOFF < 7
+ /* This must be at least at least this big, or some code later
+ on doesn't work. */
+# error "BYTE_CUTOFF is too small."
+#endif
+
+ if (n < BYTE_CUTOFF)
+ {
+ /* Strangely, this turns out to be the tightest way to write
+ this loop. */
+ if (n != 0)
+ {
+ do
+ {
+ /* Strangely, combining these into one line performs worse. */
+ *out8 = c;
+ out8++;
+ }
+ while (--n != 0);
+ }
+
+ return s;
+ }
+
+ /* Align 'out8'. We know n >= 7 so this won't write past the end. */
+ while (((uintptr_t) out8 & 7) != 0)
+ {
+ *out8++ = c;
+ --n;
+ }
+
+ /* Align 'n'. */
+ while (n & 7)
+ out8[--n] = c;
+
+ out64 = (uint64_t *) out8;
+ n64 = n >> 3;
+
+ /* Tile input byte out to 64 bits. */
+ v64 = copy_byte(c);
+
+ /* This must be at least 8 or the following loop doesn't work. */
+#define CACHE_LINE_SIZE_IN_DOUBLEWORDS (CHIP_L2_LINE_SIZE() / 8)
+
+ /* Determine how many words we need to emit before the 'out32'
+ pointer becomes aligned modulo the cache line size. */
+ to_align64 = (-((uintptr_t) out64 >> 3)) &
+ (CACHE_LINE_SIZE_IN_DOUBLEWORDS - 1);
+
+ /* Only bother aligning and using wh64 if there is at least
+ one full cache line to process. This check also prevents
+ overrunning the end of the buffer with alignment words. */
+ if (to_align64 <= n64 - CACHE_LINE_SIZE_IN_DOUBLEWORDS)
+ {
+ int lines_left;
+
+ /* Align out64 mod the cache line size so we can use wh64. */
+ n64 -= to_align64;
+ for (; to_align64 != 0; to_align64--)
+ {
+ *out64 = v64;
+ out64++;
+ }
+
+ /* Use unsigned divide to turn this into a right shift. */
+ lines_left = (unsigned) n64 / CACHE_LINE_SIZE_IN_DOUBLEWORDS;
+
+ do
+ {
+ /* Only wh64 a few lines at a time, so we don't exceed the
+ maximum number of victim lines. */
+ int x = ((lines_left < CHIP_MAX_OUTSTANDING_VICTIMS ()) ? lines_left
+ : CHIP_MAX_OUTSTANDING_VICTIMS ());
+ uint64_t *wh = out64;
+ int i = x;
+ int j;
+
+ lines_left -= x;
+
+ do
+ {
+ __insn_wh64 (wh);
+ wh += CACHE_LINE_SIZE_IN_DOUBLEWORDS;
+ }
+ while (--i);
+
+ for (j = x * (CACHE_LINE_SIZE_IN_DOUBLEWORDS / 4); j != 0; j--)
+ {
+ *out64++ = v64;
+ *out64++ = v64;
+ *out64++ = v64;
+ *out64++ = v64;
+ }
+ }
+ while (lines_left != 0);
+
+ /* We processed all full lines above, so only this many
+ words remain to be processed. */
+ n64 &= CACHE_LINE_SIZE_IN_DOUBLEWORDS - 1;
+ }
+
+ /* Now handle any leftover values. */
+ if (n64 != 0)
+ {
+ do
+ {
+ *out64 = v64;
+ out64++;
+ }
+ while (--n64 != 0);
+ }
+
+ return s;
+}
+weak_alias (__memset, memset)
+libc_hidden_builtin_def (memset)
diff --git a/ports/sysdeps/tile/tilegx/memusage.h b/ports/sysdeps/tile/tilegx/memusage.h
new file mode 100644
index 0000000000..0ac8ad8644
--- /dev/null
+++ b/ports/sysdeps/tile/tilegx/memusage.h
@@ -0,0 +1,30 @@
+/* Copyright (C) 2011 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library. If not, see
+ <http://www.gnu.org/licenses/>. */
+
+#include <arch/spr_def.h>
+
+#define GETSP() ({ register uintptr_t stack_ptr asm ("sp"); stack_ptr; })
+
+#define GETTIME(low,high) \
+ { \
+ uint64_t cycles = __insn_mfspr (SPR_CYCLE); \
+ low = cycles & 0xffffffff; \
+ high = cycles >> 32; \
+ }
+
+#include <sysdeps/generic/memusage.h>
diff --git a/ports/sysdeps/tile/tilegx/rawmemchr.c b/ports/sysdeps/tile/tilegx/rawmemchr.c
new file mode 100644
index 0000000000..70b5928ebc
--- /dev/null
+++ b/ports/sysdeps/tile/tilegx/rawmemchr.c
@@ -0,0 +1,45 @@
+/* Copyright (C) 2011-2012 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library. If not, see
+ <http://www.gnu.org/licenses/>. */
+
+#include <string.h>
+#include <stdint.h>
+#include "string-endian.h"
+
+void *
+__rawmemchr (const void *s, int c)
+{
+ /* Get an aligned pointer. */
+ const uintptr_t s_int = (uintptr_t) s;
+ const uint64_t *p = (const uint64_t *) (s_int & -8);
+
+ /* Create eight copies of the byte for which we are looking. */
+ const uint64_t goal = copy_byte(c);
+
+ /* Read the first word, but munge it so that bytes before the array
+ will not match goal. */
+ const uint64_t before_mask = MASK (s_int);
+ uint64_t v = (*p | before_mask) ^ (goal & before_mask);
+
+ uint64_t bits;
+ while ((bits = __insn_v1cmpeq (v, goal)) == 0)
+ v = *++p;
+
+ return ((char *) p) + (CFZ (bits) >> 3);
+}
+libc_hidden_def (__rawmemchr)
+weak_alias (__rawmemchr, rawmemchr)
diff --git a/ports/sysdeps/tile/tilegx/strchr.c b/ports/sysdeps/tile/tilegx/strchr.c
new file mode 100644
index 0000000000..c6a741b37d
--- /dev/null
+++ b/ports/sysdeps/tile/tilegx/strchr.c
@@ -0,0 +1,67 @@
+/* Copyright (C) 2011-2012 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library. If not, see
+ <http://www.gnu.org/licenses/>. */
+
+#include <string.h>
+#include <stdint.h>
+#include "string-endian.h"
+
+#undef strchr
+
+char *
+strchr (const char *s, int c)
+{
+ int z, g;
+
+ /* Get an aligned pointer. */
+ const uintptr_t s_int = (uintptr_t) s;
+ const uint64_t *p = (const uint64_t *) (s_int & -8);
+
+ /* Create eight copies of the byte for which we are looking. */
+ const uint64_t goal = copy_byte(c);
+
+ /* Read the first aligned word, but force bytes before the string to
+ match neither zero nor goal (we make sure the high bit of each byte
+ is 1, and the low 7 bits are all the opposite of the goal byte). */
+ const uint64_t before_mask = MASK (s_int);
+ uint64_t v = (*p | before_mask) ^ (goal & __insn_v1shrui (before_mask, 1));
+
+ uint64_t zero_matches, goal_matches;
+ while (1)
+ {
+ /* Look for a terminating '\0'. */
+ zero_matches = __insn_v1cmpeqi (v, 0);
+
+ /* Look for the goal byte. */
+ goal_matches = __insn_v1cmpeq (v, goal);
+
+ if (__builtin_expect ((zero_matches | goal_matches) != 0, 0))
+ break;
+
+ v = *++p;
+ }
+
+ z = CFZ (zero_matches);
+ g = CFZ (goal_matches);
+
+ /* If we found c before '\0' we got a match. Note that if c == '\0'
+ then g == z, and we correctly return the address of the '\0'
+ rather than NULL. */
+ return (g <= z) ? ((char *) p) + (g >> 3) : NULL;
+}
+weak_alias (strchr, index)
+libc_hidden_builtin_def (strchr)
diff --git a/ports/sysdeps/tile/tilegx/strchrnul.c b/ports/sysdeps/tile/tilegx/strchrnul.c
new file mode 100644
index 0000000000..4251598dca
--- /dev/null
+++ b/ports/sysdeps/tile/tilegx/strchrnul.c
@@ -0,0 +1,64 @@
+/* Copyright (C) 2011-2012 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library. If not, see
+ <http://www.gnu.org/licenses/>. */
+
+#include <string.h>
+#include <stdint.h>
+#include "string-endian.h"
+
+char *
+__strchrnul (const char *s, int c)
+{
+ int z, g;
+
+ /* Get an aligned pointer. */
+ const uintptr_t s_int = (uintptr_t) s;
+ const uint64_t *p = (const uint64_t *) (s_int & -8);
+
+ /* Create eight copies of the byte for which we are looking. */
+ const uint64_t goal = copy_byte(c);
+
+ /* Read the first aligned word, but force bytes before the string to
+ match neither zero nor goal (we make sure the high bit of each byte
+ is 1, and the low 7 bits are all the opposite of the goal byte). */
+ const uint64_t before_mask = MASK (s_int);
+ uint64_t v = (*p | before_mask) ^ (goal & __insn_v1shrui (before_mask, 1));
+
+ uint64_t zero_matches, goal_matches;
+ while (1)
+ {
+ /* Look for a terminating '\0'. */
+ zero_matches = __insn_v1cmpeqi (v, 0);
+
+ /* Look for the goal byte. */
+ goal_matches = __insn_v1cmpeq (v, goal);
+
+ if (__builtin_expect ((zero_matches | goal_matches) != 0, 0))
+ break;
+
+ v = *++p;
+ }
+
+ z = CFZ (zero_matches);
+ g = CFZ (goal_matches);
+
+ /* Return a pointer to the NUL or goal, whichever is first. */
+ if (z < g)
+ g = z;
+ return ((char *) p) + (g >> 3);
+}
+weak_alias (__strchrnul, strchrnul)
diff --git a/ports/sysdeps/tile/tilegx/string-endian.h b/ports/sysdeps/tile/tilegx/string-endian.h
new file mode 100644
index 0000000000..c2e40ecd4f
--- /dev/null
+++ b/ports/sysdeps/tile/tilegx/string-endian.h
@@ -0,0 +1,45 @@
+/* Copyright (C) 2011-2012 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library. If not, see
+ <http://www.gnu.org/licenses/>. */
+
+/* Provide a mask based on the pointer alignment that
+ sets up non-zero bytes before the beginning of the string.
+ The MASK expression works because shift counts are taken mod 64.
+ Also, specify how to count "first" and "last" bits
+ when the bits have been read as a word. */
+
+#ifndef __BIG_ENDIAN__
+#define MASK(x) (__insn_shl(1ULL, (x << 3)) - 1)
+#define NULMASK(x) ((2ULL << x) - 1)
+#define CFZ(x) __insn_ctz(x)
+#define REVCZ(x) __insn_clz(x)
+#else
+#define MASK(x) (__insn_shl(-2LL, ((-x << 3) - 1)))
+#define NULMASK(x) (-2LL << (63 - x))
+#define CFZ(x) __insn_clz(x)
+#define REVCZ(x) __insn_ctz(x)
+#endif
+
+/* Create eight copies of the byte in a uint64_t. */
+static inline uint64_t copy_byte(uint8_t byte)
+{
+ uint64_t word = byte;
+ word = __insn_bfins(word, word, 8, 15);
+ word = __insn_bfins(word, word, 16, 31);
+ word = __insn_bfins(word, word, 32, 63);
+ return word;
+}
diff --git a/ports/sysdeps/tile/tilegx/strlen.c b/ports/sysdeps/tile/tilegx/strlen.c
new file mode 100644
index 0000000000..dda9dff1c9
--- /dev/null
+++ b/ports/sysdeps/tile/tilegx/strlen.c
@@ -0,0 +1,39 @@
+/* Copyright (C) 2011 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library. If not, see
+ <http://www.gnu.org/licenses/>. */
+
+#include <string.h>
+#include <stdint.h>
+#include "string-endian.h"
+
+size_t
+strlen (const char *s)
+{
+ /* Get an aligned pointer. */
+ const uintptr_t s_int = (uintptr_t) s;
+ const uint64_t *p = (const uint64_t *) (s_int & -8);
+
+ /* Read and MASK the first word. */
+ uint64_t v = *p | MASK (s_int);
+
+ uint64_t bits;
+ while ((bits = __insn_v1cmpeqi (v, 0)) == 0)
+ v = *++p;
+
+ return ((const char *) p) + (CFZ (bits) >> 3) - s;
+}
+libc_hidden_builtin_def (strlen)
diff --git a/ports/sysdeps/tile/tilegx/strrchr.c b/ports/sysdeps/tile/tilegx/strrchr.c
new file mode 100644
index 0000000000..b6e43a6832
--- /dev/null
+++ b/ports/sysdeps/tile/tilegx/strrchr.c
@@ -0,0 +1,68 @@
+/* Copyright (C) 2011-2012 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library. If not, see
+ <http://www.gnu.org/licenses/>. */
+
+#include <string.h>
+#include <stdint.h>
+#include "string-endian.h"
+
+char *
+strrchr (const char *s, int c)
+{
+ /* Get an aligned pointer. */
+ const uintptr_t s_int = (uintptr_t) s;
+ const uint64_t *p = (const uint64_t *) (s_int & -8);
+
+ /* Create eight copies of the byte for which we are looking. */
+ const uint64_t goal = copy_byte(c);
+
+ /* Read the first aligned word, but force bytes before the string to
+ match neither zero nor goal (we make sure the high bit of each byte
+ is 1, and the low 7 bits are all the opposite of the goal byte). */
+ const uint64_t before_mask = MASK (s_int);
+ uint64_t v = (*p | before_mask) ^ (goal & __insn_v1shrui (before_mask, 1));
+ const char *found = NULL;
+ uint64_t zero_matches, goal_matches;
+ while (1)
+ {
+ /* Look for a terminating '\0'. */
+ zero_matches = __insn_v1cmpeqi (v, 0);
+
+ /* Look for the goal byte. */
+ goal_matches = __insn_v1cmpeq (v, goal);
+
+ /* If we found the goal, record the last offset. */
+ if (__builtin_expect (goal_matches != 0, 0))
+ {
+ if (__builtin_expect (zero_matches != 0, 0))
+ {
+ /* Clear any goal after the first zero. */
+ int first_nul = CFZ (zero_matches);
+ goal_matches &= NULMASK (first_nul);
+ }
+ if (__builtin_expect (goal_matches != 0, 1))
+ found = ((char *) p) + 7 - (REVCZ (goal_matches) >> 3);
+ }
+
+ if (__builtin_expect (zero_matches != 0, 0))
+ return (char *) found;
+
+ v = *++p;
+ }
+}
+weak_alias (strrchr, rindex)
+libc_hidden_builtin_def (strrchr)
diff --git a/ports/sysdeps/tile/tilegx/tilegx32/Implies b/ports/sysdeps/tile/tilegx/tilegx32/Implies
new file mode 100644
index 0000000000..993b7f4cd1
--- /dev/null
+++ b/ports/sysdeps/tile/tilegx/tilegx32/Implies
@@ -0,0 +1,3 @@
+tile/tilegx
+tile
+wordsize-32
diff --git a/ports/sysdeps/tile/tilegx/tilegx64/Implies b/ports/sysdeps/tile/tilegx/tilegx64/Implies
new file mode 100644
index 0000000000..eb0686e0e6
--- /dev/null
+++ b/ports/sysdeps/tile/tilegx/tilegx64/Implies
@@ -0,0 +1,3 @@
+tile/tilegx
+tile
+wordsize-64