lwip-sys with changes
Fork of lwip-sys by
arch/checksum.c@10:09b0951b1899, 2013-09-10 (annotated)
- Committer:
- bogdanm
- Date:
- Tue Sep 10 15:14:42 2013 +0300
- Revision:
- 10:09b0951b1899
Sync with git revision 171dda705c947bf910926a0b73d6a4797802554d
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 10:09b0951b1899 | 1 | /* Copyright (C) 2013 - Adam Green (https://github.com/adamgreen) |
bogdanm | 10:09b0951b1899 | 2 | |
bogdanm | 10:09b0951b1899 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 10:09b0951b1899 | 4 | you may not use this file except in compliance with the License. |
bogdanm | 10:09b0951b1899 | 5 | You may obtain a copy of the License at |
bogdanm | 10:09b0951b1899 | 6 | |
bogdanm | 10:09b0951b1899 | 7 | http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 10:09b0951b1899 | 8 | |
bogdanm | 10:09b0951b1899 | 9 | Unless required by applicable law or agreed to in writing, software |
bogdanm | 10:09b0951b1899 | 10 | distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 10:09b0951b1899 | 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 10:09b0951b1899 | 12 | See the License for the specific language governing permissions and |
bogdanm | 10:09b0951b1899 | 13 | limitations under the License. |
bogdanm | 10:09b0951b1899 | 14 | */ |
bogdanm | 10:09b0951b1899 | 15 | #if defined(TOOLCHAIN_GCC) && defined(__thumb2__) |
bogdanm | 10:09b0951b1899 | 16 | |
bogdanm | 10:09b0951b1899 | 17 | |
bogdanm | 10:09b0951b1899 | 18 | /* This is a hand written Thumb-2 assembly language version of the |
bogdanm | 10:09b0951b1899 | 19 | algorithm 3 version of lwip_standard_chksum in lwIP's inet_chksum.c. It |
bogdanm | 10:09b0951b1899 | 20 | performs the checksumming 32-bits at a time and even unrolls the loop to |
bogdanm | 10:09b0951b1899 | 21 | perform two of these 32-bit adds per loop iteration. |
bogdanm | 10:09b0951b1899 | 22 | |
bogdanm | 10:09b0951b1899 | 23 | Returns: |
bogdanm | 10:09b0951b1899 | 24 | 16-bit 1's complement summation (not inversed). |
bogdanm | 10:09b0951b1899 | 25 | |
bogdanm | 10:09b0951b1899 | 26 | NOTE: This function does return a uint16_t from the assembly language code |
bogdanm | 10:09b0951b1899 | 27 | but is marked as void so that GCC doesn't issue warning because it |
bogdanm | 10:09b0951b1899 | 28 | doesn't know about this low level return. |
bogdanm | 10:09b0951b1899 | 29 | */ |
bogdanm | 10:09b0951b1899 | 30 | __attribute__((naked)) void /*uint16_t*/ thumb2_checksum(const void* pData, int length) |
bogdanm | 10:09b0951b1899 | 31 | { |
bogdanm | 10:09b0951b1899 | 32 | __asm ( |
bogdanm | 10:09b0951b1899 | 33 | ".syntax unified\n" |
bogdanm | 10:09b0951b1899 | 34 | ".thumb\n" |
bogdanm | 10:09b0951b1899 | 35 | |
bogdanm | 10:09b0951b1899 | 36 | // Push non-volatile registers we use on stack. Push link register too to |
bogdanm | 10:09b0951b1899 | 37 | // keep stack 8-byte aligned and allow single pop to restore and return. |
bogdanm | 10:09b0951b1899 | 38 | " push {r4, lr}\n" |
bogdanm | 10:09b0951b1899 | 39 | // Initialize sum, r2, to 0. |
bogdanm | 10:09b0951b1899 | 40 | " movs r2, #0\n" |
bogdanm | 10:09b0951b1899 | 41 | // Remember whether pData was at odd address in r3. This is used later to |
bogdanm | 10:09b0951b1899 | 42 | // know if it needs to swap the result since the summation will be done at |
bogdanm | 10:09b0951b1899 | 43 | // an offset of 1, rather than 0. |
bogdanm | 10:09b0951b1899 | 44 | " ands r3, r0, #1\n" |
bogdanm | 10:09b0951b1899 | 45 | // Need to 2-byte align? If not skip ahead. |
bogdanm | 10:09b0951b1899 | 46 | " beq 1$\n" |
bogdanm | 10:09b0951b1899 | 47 | // We can return if there are no bytes to sum. |
bogdanm | 10:09b0951b1899 | 48 | " cbz r1, 9$\n" |
bogdanm | 10:09b0951b1899 | 49 | |
bogdanm | 10:09b0951b1899 | 50 | // 2-byte align. |
bogdanm | 10:09b0951b1899 | 51 | // Place the first data byte in odd summation location since it needs to be |
bogdanm | 10:09b0951b1899 | 52 | // swapped later. It's ok to overwrite r2 here as it only had a value of 0 |
bogdanm | 10:09b0951b1899 | 53 | // up until now. Advance r0 pointer and decrement r1 length as we go. |
bogdanm | 10:09b0951b1899 | 54 | " ldrb r2, [r0], #1\n" |
bogdanm | 10:09b0951b1899 | 55 | " lsls r2, r2, #8\n" |
bogdanm | 10:09b0951b1899 | 56 | " subs r1, r1, #1\n" |
bogdanm | 10:09b0951b1899 | 57 | |
bogdanm | 10:09b0951b1899 | 58 | // Need to 4-byte align? If not skip ahead. |
bogdanm | 10:09b0951b1899 | 59 | "1$:\n" |
bogdanm | 10:09b0951b1899 | 60 | " ands r4, r0, #3\n" |
bogdanm | 10:09b0951b1899 | 61 | " beq 2$\n" |
bogdanm | 10:09b0951b1899 | 62 | // Have more than 1 byte left to align? If not skip ahead to take care of |
bogdanm | 10:09b0951b1899 | 63 | // trailing byte. |
bogdanm | 10:09b0951b1899 | 64 | " cmp r1, #2\n" |
bogdanm | 10:09b0951b1899 | 65 | " blt 7$\n" |
bogdanm | 10:09b0951b1899 | 66 | |
bogdanm | 10:09b0951b1899 | 67 | // 4-byte align. |
bogdanm | 10:09b0951b1899 | 68 | " ldrh r4, [r0], #2\n" |
bogdanm | 10:09b0951b1899 | 69 | " adds r2, r2, r4\n" |
bogdanm | 10:09b0951b1899 | 70 | " subs r1, r1, #2\n" |
bogdanm | 10:09b0951b1899 | 71 | |
bogdanm | 10:09b0951b1899 | 72 | // Main summing loop which sums up data 2 words at a time. |
bogdanm | 10:09b0951b1899 | 73 | // Make sure that we have more than 7 bytes left to sum. |
bogdanm | 10:09b0951b1899 | 74 | "2$:\n" |
bogdanm | 10:09b0951b1899 | 75 | " cmp r1, #8\n" |
bogdanm | 10:09b0951b1899 | 76 | " blt 3$\n" |
bogdanm | 10:09b0951b1899 | 77 | // Sum next two words. Applying previous upper 16-bit carry to |
bogdanm | 10:09b0951b1899 | 78 | // lower 16-bits. |
bogdanm | 10:09b0951b1899 | 79 | " ldr r4, [r0], #4\n" |
bogdanm | 10:09b0951b1899 | 80 | " adds r2, r4\n" |
bogdanm | 10:09b0951b1899 | 81 | " adc r2, r2, #0\n" |
bogdanm | 10:09b0951b1899 | 82 | " ldr r4, [r0], #4\n" |
bogdanm | 10:09b0951b1899 | 83 | " adds r2, r4\n" |
bogdanm | 10:09b0951b1899 | 84 | " adc r2, r2, #0\n" |
bogdanm | 10:09b0951b1899 | 85 | " subs r1, r1, #8\n" |
bogdanm | 10:09b0951b1899 | 86 | " b 2$\n" |
bogdanm | 10:09b0951b1899 | 87 | |
bogdanm | 10:09b0951b1899 | 88 | // Sum up any remaining half-words. |
bogdanm | 10:09b0951b1899 | 89 | "3$:\n" |
bogdanm | 10:09b0951b1899 | 90 | // Make sure that we have more than 1 byte left to sum. |
bogdanm | 10:09b0951b1899 | 91 | " cmp r1, #2\n" |
bogdanm | 10:09b0951b1899 | 92 | " blt 7$\n" |
bogdanm | 10:09b0951b1899 | 93 | // Sum up next half word, continue to apply carry. |
bogdanm | 10:09b0951b1899 | 94 | " ldrh r4, [r0], #2\n" |
bogdanm | 10:09b0951b1899 | 95 | " adds r2, r4\n" |
bogdanm | 10:09b0951b1899 | 96 | " adc r2, r2, #0\n" |
bogdanm | 10:09b0951b1899 | 97 | " subs r1, r1, #2\n" |
bogdanm | 10:09b0951b1899 | 98 | " b 3$\n" |
bogdanm | 10:09b0951b1899 | 99 | |
bogdanm | 10:09b0951b1899 | 100 | // Handle trailing byte, if it exists |
bogdanm | 10:09b0951b1899 | 101 | "7$:\n" |
bogdanm | 10:09b0951b1899 | 102 | " cbz r1, 8$\n" |
bogdanm | 10:09b0951b1899 | 103 | " ldrb r4, [r0]\n" |
bogdanm | 10:09b0951b1899 | 104 | " adds r2, r4\n" |
bogdanm | 10:09b0951b1899 | 105 | " adc r2, r2, #0\n" |
bogdanm | 10:09b0951b1899 | 106 | |
bogdanm | 10:09b0951b1899 | 107 | // Fold 32-bit checksum into 16-bit checksum. |
bogdanm | 10:09b0951b1899 | 108 | "8$:\n" |
bogdanm | 10:09b0951b1899 | 109 | " ubfx r4, r2, #16, #16\n" |
bogdanm | 10:09b0951b1899 | 110 | " ubfx r2, r2, #0, #16\n" |
bogdanm | 10:09b0951b1899 | 111 | " adds r2, r4\n" |
bogdanm | 10:09b0951b1899 | 112 | " ubfx r4, r2, #16, #16\n" |
bogdanm | 10:09b0951b1899 | 113 | " ubfx r2, r2, #0, #16\n" |
bogdanm | 10:09b0951b1899 | 114 | " adds r2, r4\n" |
bogdanm | 10:09b0951b1899 | 115 | |
bogdanm | 10:09b0951b1899 | 116 | // Swap bytes if started at odd address |
bogdanm | 10:09b0951b1899 | 117 | " cbz r3, 9$\n" |
bogdanm | 10:09b0951b1899 | 118 | " rev16 r2, r2\n" |
bogdanm | 10:09b0951b1899 | 119 | |
bogdanm | 10:09b0951b1899 | 120 | // Return final sum. |
bogdanm | 10:09b0951b1899 | 121 | "9$: mov r0, r2\n" |
bogdanm | 10:09b0951b1899 | 122 | " pop {r4, pc}\n" |
bogdanm | 10:09b0951b1899 | 123 | ); |
bogdanm | 10:09b0951b1899 | 124 | } |
bogdanm | 10:09b0951b1899 | 125 | |
bogdanm | 10:09b0951b1899 | 126 | #endif |