2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A .file "strrchr.s"
2N/A
2N/A/
2N/A/ strrchr(sp, c)
2N/A/
2N/A/ Returns the pointer in sp at which the character c last
2N/A/ appears; NULL if no found
2N/A/
2N/A/ Fast assembly language version of the following C-program strrchr
2N/A/ which represents the `standard' for the C-library.
2N/A/
2N/A/ char *
2N/A/ strrchr(const char *sp, int c)
2N/A/ {
2N/A/ char *r = NULL;
2N/A/
2N/A/ do {
2N/A/ if (*sp == (char)c)
2N/A/ r = (char *)sp;
2N/A/ } while (*sp++);
2N/A/
2N/A/ return (r);
2N/A/ }
2N/A/
2N/A
2N/A#include "SYS.h"
2N/A
2N/A ENTRY(strrchr)
2N/A pushl %edi / save register variable
2N/A movl 8(%esp), %eax / %eax = string address
2N/A movb 12(%esp), %cl / %cl = char sought
2N/A movl $0, %edi / %edi = NULL (current occurrence)
2N/A
2N/A testl $3, %eax / if %eax not word aligned
2N/A jnz .L1 / goto .L1
2N/A .align 4
2N/A.L3:
2N/A movl (%eax), %edx / move 1 word from (%eax) to %edx
2N/A cmpb %cl, %dl / if the fist byte is not %cl
2N/A jne .L4 / goto .L4
2N/A movl %eax, %edi / save this address to %edi
2N/A.L4:
2N/A cmpb $0, %dl / if a null termination
2N/A je .L8 / goto .L8
2N/A
2N/A cmpb %cl, %dh / if the second byte is not %cl
2N/A jne .L5 / goto .L5
2N/A leal 1(%eax), %edi / save this address to %edi
2N/A.L5:
2N/A cmpb $0, %dh / if a null termination
2N/A je .L8 / goto .L8
2N/A
2N/A shrl $16, %edx / right shift 16-bit
2N/A cmpb %cl, %dl / if the third byte is not %cl
2N/A jne .L6 / goto .L6
2N/A leal 2(%eax), %edi / save this address to %edi
2N/A.L6:
2N/A cmpb $0, %dl / if a null termination
2N/A je .L8 / goto .L8
2N/A
2N/A cmpb %cl, %dh / if the fourth byte is not %cl
2N/A jne .L7 / goto .L7
2N/A leal 3(%eax), %edi / save this address to %edi
2N/A.L7:
2N/A cmpb $0, %dh / if a null termination
2N/A je .L8 / goto .L8
2N/A
2N/A addl $4, %eax / next word
2N/A jmp .L3 / goto .L3
2N/A .align 4
2N/A.L1:
2N/A movb (%eax), %dl / move 1 byte from (%eax) to %dl
2N/A cmpb %cl, %dl / if %dl is not %cl
2N/A jne .L2 / goto .L2
2N/A movl %eax, %edi / save this address to %edi
2N/A.L2:
2N/A cmpb $0, %dl / if a null termination
2N/A je .L8 / goto .L8
2N/A
2N/A incl %eax / next byte
2N/A testl $3, %eax / if %eax not word aligned
2N/A jnz .L1 / goto .L1
2N/A jmp .L3 / goto .L3 (word aligned)
2N/A .align 4
2N/A.L8:
2N/A movl %edi, %eax / %edi points to the last occurrence or NULL
2N/A popl %edi / restore register variable
2N/A ret
2N/A SET_SIZE(strrchr)