~ubuntu-branches/ubuntu/maverick/avr-libc/maverick

« back to all changes in this revision

Viewing changes to libc/string/memmem.S

  • Committer: Bazaar Package Importer
  • Author(s): Hakan Ardo
  • Date: 2007-08-09 11:28:01 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070809112801-ps7wognnynio9kz7
Tags: 1:1.4.6-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2007  Dmitry Xmelkov
 
2
   All rights reserved.
 
3
 
 
4
   Redistribution and use in source and binary forms, with or without
 
5
   modification, are permitted provided that the following conditions are met:
 
6
 
 
7
   * Redistributions of source code must retain the above copyright
 
8
     notice, this list of conditions and the following disclaimer.
 
9
   * Redistributions in binary form must reproduce the above copyright
 
10
     notice, this list of conditions and the following disclaimer in
 
11
     the documentation and/or other materials provided with the
 
12
     distribution.
 
13
   * Neither the name of the copyright holders nor the names of
 
14
     contributors may be used to endorse or promote products derived
 
15
     from this software without specific prior written permission.
 
16
 
 
17
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
18
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
19
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
20
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 
21
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
22
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
23
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
24
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
25
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
26
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
27
  POSSIBILITY OF SUCH DAMAGE. */
 
28
 
 
29
/* $Id: memmem.S,v 1.1.2.1 2007/03/31 13:23:16 dmix Exp $ */
 
30
 
 
31
/** \file */
 
32
 
 
33
/** \ingroup avr_string
 
34
    \fn void *memmem(const void *s1, size_t len1, const void *s2, size_t len2)
 
35
 
 
36
    The memmem() function finds the start of the first occurrence of the
 
37
    substring \p s2 of length \p len2 in the memory area \p s1 of length
 
38
    \p len1.
 
39
 
 
40
    \return The memmem() function returns a pointer to the beginning of
 
41
    the substring, or \c NULL if the substring is not found. If \p len2
 
42
    is zero, the function returns \p s1.        */
 
43
 
 
44
/** \ingroup avr_pgmspace
 
45
    \fn void *memmem_P(const void *s1, size_t len1, PGM_VOID_P s2, size_t len2)
 
46
    
 
47
    The memmem_P() function is similar to memmem() except that \p s2 is
 
48
    pointer to a string in program space.       */
 
49
 
 
50
 
 
51
#if !defined(__DOXYGEN__)
 
52
 
 
53
#include "asmdef.h"
 
54
 
 
55
#define s1_hi   r25
 
56
#define s1_lo   r24
 
57
#define len1_hi r23
 
58
#define len1_lo r22
 
59
#define s2_hi   r21
 
60
#define s2_lo   r20
 
61
#define len2_hi r19
 
62
#define len2_lo r18
 
63
 
 
64
#define beg2    r17     /* begin of s2: s2[0]   */
 
65
#define c1      r16     /* char from s1[]       */
 
66
#define c2      r0      /* char from s2[]: tuned for classic lpm instr. */
 
67
 
 
68
#ifdef  Lprogmem
 
69
# define memmem  memmem_P
 
70
# define LOAD    X_lpm
 
71
#else
 
72
# define LOAD    ld
 
73
#endif
 
74
 
 
75
ENTRY memmem
 
76
        cp      len2_lo, __zero_reg__
 
77
        cpc     len2_hi, __zero_reg__
 
78
        breq    .L_ret                  ; s2[] is empty
 
79
        
 
80
        push    beg2
 
81
        push    c1
 
82
 
 
83
        add     len2_lo, s2_lo          ; len2 = &(s2[len2])
 
84
        adc     len2_hi, s2_hi
 
85
        add     len1_lo, s1_lo          ; len1 = &(s1[len1])
 
86
        adc     len1_hi, s1_hi
 
87
 
 
88
        X_movw  ZL, s2_lo
 
89
        LOAD    beg2, Z+                ; beg2 = s2[0]
 
90
        X_movw  s2_lo, ZL               ; save: address of s2[1]
 
91
        
 
92
1:      X_movw  XL, s1_lo               ; goto to begin of s1[]
 
93
 
 
94
2:      cp      XL, len1_lo             ; find first char that is matched
 
95
        cpc     XH, len1_hi
 
96
        brsh    .L_nomatch
 
97
        ld      c1, X+
 
98
        cp      c1, beg2
 
99
        brne    2b
 
100
 
 
101
        X_movw  s1_lo, XL               ; store address
 
102
 
 
103
        X_movw  ZL, s2_lo
 
104
3:      cp      ZL, len2_lo             ; compare strings
 
105
        cpc     ZH, len2_hi
 
106
        brsh    .L_match                ; end of s2[] --> OK
 
107
        cp      XL, len1_lo
 
108
        cpc     XH, len1_hi
 
109
        brsh    .L_nomatch              ; s1[] tail is too short
 
110
        ld      c1, X+
 
111
        LOAD    c2, Z+
 
112
        cp      c1, c2
 
113
        breq    3b
 
114
        rjmp    1b                      ; no equal
 
115
        
 
116
.L_nomatch:
 
117
        ldi     s1_lo, lo8(1)
 
118
        ldi     s1_hi, hi8(1)
 
119
.L_match:
 
120
        sbiw    s1_lo, 1                ; restore after post-increment
 
121
        pop     c1
 
122
        pop     beg2
 
123
.L_ret:
 
124
        ret
 
125
 
 
126
ENDFUNC
 
127
#endif /* not __DOXYGEN__ */