1d719c1c9Smatt/*- 2d719c1c9Smatt * Copyright (c) 2013 The NetBSD Foundation, Inc. 3d719c1c9Smatt * All rights reserved. 4d719c1c9Smatt * 5d719c1c9Smatt * This code is derived from software contributed to The NetBSD Foundation 6d719c1c9Smatt * by Matt Thomas of 3am Software Foundry. 7d719c1c9Smatt * 8d719c1c9Smatt * Redistribution and use in source and binary forms, with or without 9d719c1c9Smatt * modification, are permitted provided that the following conditions 10d719c1c9Smatt * are met: 11d719c1c9Smatt * 1. Redistributions of source code must retain the above copyright 12d719c1c9Smatt * notice, this list of conditions and the following disclaimer. 13d719c1c9Smatt * 2. Redistributions in binary form must reproduce the above copyright 14d719c1c9Smatt * notice, this list of conditions and the following disclaimer in the 15d719c1c9Smatt * documentation and/or other materials provided with the distribution. 16d719c1c9Smatt * 17d719c1c9Smatt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18d719c1c9Smatt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19d719c1c9Smatt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20d719c1c9Smatt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21d719c1c9Smatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22d719c1c9Smatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23d719c1c9Smatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24d719c1c9Smatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25d719c1c9Smatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26d719c1c9Smatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27d719c1c9Smatt * POSSIBILITY OF SUCH DAMAGE. 28d719c1c9Smatt */ 29d719c1c9Smatt#include <machine/asm.h> 30d719c1c9Smatt 31*89c012c5SmattRCSID("$NetBSD: strrchr_naive.S,v 1.4 2013/08/19 17:02:25 matt Exp $") 32d719c1c9Smatt 33d719c1c9Smatt/* LINTSTUB: char * strrchr(const char *, int) */ 34d719c1c9SmattENTRY(strrchr) 352e0525ccSmatt mov r2, r0 /* using r0 as return value */ 3692867fc8Smatt movs r0, #0 /* default to no match */ 3792867fc8Smatt#ifdef __thumb__ 3892867fc8Smatt movs r3, #0xff 3992867fc8Smatt ands r1, r1, r3 /* restrict to a byte value */ 4092867fc8Smatt1: ldrb r3, [r2] /* read a byte */ 4192867fc8Smatt cmp r3, r1 /* does it match? */ 4292867fc8Smatt bne 2f /* no, go and advance */ 4392867fc8Smatt mov r0, r2 /* yes, set return value to point to it */ 4492867fc8Smatt2: adds r2, r2, #1 /* advance to next byte */ 4592867fc8Smatt cmp r3, #0 /* was it a NUL? */ 4692867fc8Smatt bne 1b /* no, get next byte */ 4792867fc8Smatt#else 48d719c1c9Smatt and r1, r1, #0xff /* restrict to a byte value */ 492e0525ccSmatt1: ldrb r3, [r2], #1 /* read a byte */ 50d719c1c9Smatt cmp r3, r1 /* does it match? */ 512e0525ccSmatt subeq r0, r2, #1 /* yes, set return value to point to it */ 522e0525ccSmatt cmp r3, #0 /* was it a NUL? */ 53d719c1c9Smatt bne 1b /* no, get next byte */ 5492867fc8Smatt#endif 55d719c1c9Smatt RET 56d719c1c9SmattEND(strrchr) 57