Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An avr assemble fix for Issue #154 #224

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 58 additions & 74 deletions asm_avr.inc
Original file line number Diff line number Diff line change
Expand Up @@ -216,48 +216,40 @@ uECC_VLI_API void uECC_vli_mult(uECC_word_t *result,
wordcount_t num_words) {
/* num_words should already be in r18. */
register wordcount_t r18 __asm__("r18") = num_words;

__asm__ volatile (
"push r18 \n\t"
//L1uTongwei: To fix issue #154, we need to divide the asm code, because the code is too long.
FAST_MULT_ENVIRONMENT("push r18 \n\t");
#if (uECC_MIN_WORDS == 20)
FAST_MULT_ASM_20
"pop r18 \n\t"
#if (uECC_MAX_WORDS > 20)
FAST_MULT_ASM_20_TO_24
#endif
#if (uECC_MAX_WORDS > 24)
FAST_MULT_ASM_24_TO_28
#endif
#if (uECC_MAX_WORDS > 28)
FAST_MULT_ASM_28_TO_32
#endif
FAST_MULT_ENVIRONMENT(FAST_MULT_ASM_20);
FAST_MULT_ENVIRONMENT("pop r18 \n\t");
#if (uECC_MAX_WORDS > 20)
FAST_MULT_ENVIRONMENT(FAST_MULT_ASM_20_TO_24);
#endif
#if (uECC_MAX_WORDS > 24)
FAST_MULT_ENVIRONMENT(FAST_MULT_ASM_24_TO_28);
#endif
#if (uECC_MAX_WORDS > 28)
FAST_MULT_ENVIRONMENT(FAST_MULT_ASM_28_TO_32);
#endif
#elif (uECC_MIN_WORDS == 24)
FAST_MULT_ASM_24
"pop r18 \n\t"
#if (uECC_MAX_WORDS > 24)
FAST_MULT_ASM_24_TO_28
#endif
#if (uECC_MAX_WORDS > 28)
FAST_MULT_ASM_28_TO_32
#endif
FAST_MULT_ENVIRONMENT(FAST_MULT_ASM_24);
FAST_MULT_ENVIRONMENT("pop r18 \n\t");
#if (uECC_MAX_WORDS > 24)
FAST_MULT_ENVIRONMENT(FAST_MULT_ASM_24_TO_28);
#endif
#if (uECC_MAX_WORDS > 28)
FAST_MULT_ENVIRONMENT(FAST_MULT_ASM_28_TO_32);
#endif
#elif (uECC_MIN_WORDS == 28)
FAST_MULT_ASM_28
"pop r18 \n\t"
#if (uECC_MAX_WORDS > 28)
FAST_MULT_ASM_28_TO_32
#endif
FAST_MULT_ENVIRONMENT(FAST_MULT_ASM_28)
FAST_MULT_ENVIRONMENT("pop r18 \n\t");
#if (uECC_MAX_WORDS > 28)
FAST_MULT_ENVIRONMENT(FAST_MULT_ASM_28_TO_32);
#endif
#elif (uECC_MIN_WORDS == 32)
FAST_MULT_ASM_32
"pop r18 \n\t"
FAST_MULT_ENVIRONMENT(FAST_MULT_ASM_32);
FAST_MULT_ENVIRONMENT("pop r18 \n\t");
#endif
"2: \n\t"
"eor r1, r1 \n\t"
: "+x" (left), "+y" (right), "+z" (result)
: "r" (r18)
: "r0", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
"r11", "r12", "r13", "r14", "r15", "r16", "r17", "r19", "r20",
"r21", "r22", "r23", "r24", "r25", "cc"
);
FAST_MULT_ENVIRONMENT("2: \n\t eor r1, r1 \n\t");
}
#define asm_mult 1

Expand All @@ -268,48 +260,40 @@ uECC_VLI_API void uECC_vli_square(uECC_word_t *result,
wordcount_t num_words) {
/* num_words should already be in r20. */
register wordcount_t r20 __asm__("r20") = num_words;

__asm__ volatile (
"push r20 \n\t"
//L1uTongwei: these code are the same as mult.
FAST_SQUARE_ENVIRONMENT("push r20 \n\t");
#if (uECC_MIN_WORDS == 20)
FAST_SQUARE_ASM_20
"pop r20 \n\t"
#if (uECC_MAX_WORDS > 20)
FAST_SQUARE_ASM_20_TO_24
#endif
#if (uECC_MAX_WORDS > 24)
FAST_SQUARE_ASM_24_TO_28
#endif
#if (uECC_MAX_WORDS > 28)
FAST_SQUARE_ASM_28_TO_32
#endif
FAST_SQUARE_ENVIRONMENT(FAST_SQUARE_ASM_20);
FAST_SQUARE_ENVIRONMENT("pop r20 \n\t");
#if (uECC_MAX_WORDS > 20)
FAST_SQUARE_ENVIRONMENT(FAST_SQUARE_ASM_20_TO_24);
#endif
#if (uECC_MAX_WORDS > 24)
FAST_SQUARE_ENVIRONMENT(FAST_SQUARE_ASM_24_TO_28);
#endif
#if (uECC_MAX_WORDS > 28)
FAST_SQUARE_ENVIRONMENT(FAST_SQUARE_ASM_28_TO_32);
#endif
#elif (uECC_MIN_WORDS == 24)
FAST_SQUARE_ASM_24
"pop r20 \n\t"
#if (uECC_MAX_WORDS > 24)
FAST_SQUARE_ASM_24_TO_28
#endif
#if (uECC_MAX_WORDS > 28)
FAST_SQUARE_ASM_28_TO_32
#endif
FAST_SQUARE_ENVIRONMENT(FAST_SQUARE_ASM_24);
FAST_SQUARE_ENVIRONMENT("pop r20 \n\t");
#if (uECC_MAX_WORDS > 24)
FAST_SQUARE_ENVIRONMENT(FAST_SQUARE_ASM_24_TO_28);
#endif
#if (uECC_MAX_WORDS > 28)
FAST_SQUARE_ENVIRONMENT(FAST_SQUARE_ASM_28_TO_32);
#endif
#elif (uECC_MIN_WORDS == 28)
FAST_SQUARE_ASM_28
"pop r20 \n\t"
#if (uECC_MAX_WORDS > 28)
FAST_SQUARE_ASM_28_TO_32
#endif
FAST_SQUARE_ENVIRONMENT(FAST_SQUARE_ASM_28);
FAST_SQUARE_ENVIRONMENT("pop r20 \n\t");
#if (uECC_MAX_WORDS > 28)
FAST_SQUARE_ENVIRONMENT(FAST_SQUARE_ASM_28_TO_32);
#endif
#elif (uECC_MIN_WORDS == 32)
FAST_SQUARE_ASM_32
"pop r20 \n\t"
FAST_SQUARE_ENVIRONMENT(FAST_SQUARE_ASM_32);
FAST_SQUARE_ENVIRONMENT("pop r20 \n\t");
#endif
"2: \n\t"
"eor r1, r1 \n\t"
: "+x" (left), "+z" (result)
: "r" (r20)
: "r0", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
"r11", "r12", "r13", "r14", "r15", "r16", "r17", "r18", "r19",
"r21", "r22", "r23", "r24", "r25", "r28", "r29", "cc"
);
FAST_SQUARE_ENVIRONMENT("2: \n\t eor r1, r1 \n\t");
}
#define asm_square 1
#endif /* uECC_SQUARE_FUNC */
Expand Down
7 changes: 7 additions & 0 deletions asm_avr_mult_square.inc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
#ifndef _UECC_ASM_AVR_MULT_SQUARE_H_
#define _UECC_ASM_AVR_MULT_SQUARE_H_

//L1uTongwei: These environments are comfort for this avr optimize.
//This be adopted by asm_avr.inc to fix the issue #154
#define FAST_MULT_ENVIRONMENT(X) \
__asm__ volatile ( X : "+x" (left), "+y" (right), "+z" (result) : "r" (r18) : "r0", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", "r16", "r17", "r19", "r20", "r21", "r22", "r23", "r24", "r25", "cc");
#define FAST_SQUARE_ENVIRONMENT(X) \
__asm__ volatile ( X : "+x" (left), "+z" (result) : "r" (r20) : "r0", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", "r16", "r17", "r18", "r19", "r21", "r22", "r23", "r24", "r25", "r28", "r29", "cc");

#define FAST_MULT_ASM_20 \
"adiw r30, 10 \n\t" \
"adiw r28, 10 \n\t" \
Expand Down
3 changes: 3 additions & 0 deletions examples/ecc_test/ecc_test.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include <uECC.h>

//L1uTongwei: uncomment this to test optimize.
#define uECC_OPTIMIZATION_LEVEL 4

static int RNG(uint8_t *dest, unsigned size) {
// Use the least-significant bits from the ADC for an unconnected pin (or connected to a source of
// random noise). This can take a long time to generate random data if the result of analogRead(0)
Expand Down