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
|
/*
* main.c
*
* Created on: 23.04.2010
* Author: eisenhauer
*/
#include "stdlib.h"
#include "unistd.h"
#include "stdio.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"
: "=c"(*u),"=d"(*v)
: "a"(a),"b"(b)
);
}
int main(int args, char* argv[])
{
uint32_t resLow;
uint32_t resHigh;
uint32_t a = 1261938865;
uint32_t b = 1446688886;
mulc(&resHigh,&resLow,a,b);
printf("%u * %u = %u %u\n",a,b,resHigh,resLow);
resHigh = 0;
resLow = 0;
printf("%u %u\n",resHigh,resLow);
mula(&resHigh,&resLow,a,b);
printf("%u * %u = %u %u\n",a,b,resHigh,resLow);
return EXIT_SUCCESS;
}
|