forked from rpavlik/StandardCplusplus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalgorithm.cpp
71 lines (59 loc) · 2.3 KB
/
algorithm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* Copyright (C) 2004 Garrett A. Kajmowicz
This file is part of the uClibc++ Library.
This 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.
This 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 this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <algorithm>
namespace std{
char* __copy(char* first, char* last, char* result) {
#ifdef __AVR__
// This is 1/3 shorter than code produced by GCC
asm (
" movw r30, r24\n" // Move first to index register Z
" movw r26, r20\n" // Move result to index register X
" rjmp forward_loop_middle\n"
"forward_loop_start: ld r18, Z+\n" // Copy data
" st X+, r18\n"
"forward_loop_middle: cp r22, r30\n" // first != last
" cpc r23, r31\n"
" brne forward_loop_start\n"
" movw r24, r30\n"
);
#else
while(first != last){
*(result++) = *(first++);
}
return result;
#endif
}
char* __copy_backward(char* first, char* last, char* result) {
#ifdef __AVR__
// This is 1/3 shorter than code produced by GCC
asm (
" movw r30, r22\n" // Move first to index register Z
" movw r26, r20\n" // Move result to index register X
" rjmp backward_loop_middle\n"
"backward_loop_start: ld r18, -Z\n" // Copy data
" st -X, r18\n"
"backward_loop_middle: cp r24, r30\n" // first != last
" cpc r25, r31\n"
" brne backward_loop_start\n"
" movw r24, r30\n"
);
#else
while(first != last){
*(--result) = *(--last);
}
return result;
#endif
}
}