diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Master/Public-Key-Algorithmen/PKA-Prakt1/src/main.c | |
| download | Studium-master.tar.gz Studium-master.tar.bz2 | |
Diffstat (limited to 'Master/Public-Key-Algorithmen/PKA-Prakt1/src/main.c')
| -rw-r--r-- | Master/Public-Key-Algorithmen/PKA-Prakt1/src/main.c | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/Master/Public-Key-Algorithmen/PKA-Prakt1/src/main.c b/Master/Public-Key-Algorithmen/PKA-Prakt1/src/main.c new file mode 100644 index 0000000..2987966 --- /dev/null +++ b/Master/Public-Key-Algorithmen/PKA-Prakt1/src/main.c @@ -0,0 +1,88 @@ +/* + * main.c + * + * Created on: 23.04.2010 + * Author: sven + */ +#include <stdlib.h> +#include <stdint.h> +#include <stdio.h> +#include <time.h> +#include <sys/time.h> + +#define HI(x) (x >> 16) +#define LO(x) (x & 0x0000FFFF) + +void mulc(uint32_t *u, uint32_t *v, uint32_t a, uint32_t b) +{ + uint32_t t; + t = LO(a) * LO(b); + *v = LO(t); + t = HI(t) + HI(a) * LO(b); + *u = HI(t); + t = LO(t) + LO(a) * HI(b); + *v |= LO(t) << 16; + *u += HI(t) + HI(a) * HI(b); +} +void mula(uint32_t *u, uint32_t *v, uint32_t a, uint32_t b) +{ + asm + ( + "mov %2,%%eax\n" + "mul %3\n" + "mov %%edx, %0\n" + "mov %%eax, %1" + :"=r"(*u),"=r"(*v) + :"r"(a),"r"(b) + ); +} +void initRandomizer() +{ + srand(time(NULL)); +} +uint32_t getRandomUint32() +{ + uint32_t res = rand(); + if(res % 2) + { + return (res | 1<<31); + } + return res; +} +int main(int argc, char* argv[]) +{ + initRandomizer(); + uint32_t a = getRandomUint32(); + uint32_t b = getRandomUint32(); + uint32_t u,v; + uint32_t counter; + uint32_t outerCnt; + const uint32_t NUM_INNER_LOOPS = 1000000; + const uint32_t NUM_OUTER_LOOPS = 1000; + struct timeval startc; + struct timeval endc; + struct timeval starta; + struct timeval enda; + long diffCsum = 0,diffAsum = 0; + for(outerCnt=0;outerCnt<NUM_OUTER_LOOPS;outerCnt++) { + gettimeofday(&startc,0); + for (counter=0; counter < NUM_INNER_LOOPS; counter++) { + mulc(&u,&v,a,b); + } + gettimeofday(&endc,0); + diffCsum += (endc.tv_sec*1000000 + endc.tv_usec) - (startc.tv_sec*1000000 + startc.tv_usec); + printf("C %u * %u = %u.%u\n",a,b,u,v); + u=0; + v=0; + gettimeofday(&starta,0); + for (counter=0; counter < NUM_INNER_LOOPS; counter++) { + mula(&u,&v,a,b); + } + gettimeofday(&enda,0); + diffAsum += (enda.tv_sec*1000000 + enda.tv_usec) - (starta.tv_sec*1000000 + starta.tv_usec); + printf("ASM %u * %u = %u.%u\n",a,b,u,v); + } + printf("C: total %ld usec, avg %ld usec\n",diffCsum,(diffCsum / NUM_OUTER_LOOPS)); + printf("ASM: total %ld usec, avg %ld usec\n",diffAsum,(diffAsum / NUM_OUTER_LOOPS)); + return EXIT_SUCCESS; +} |
