.
This commit is contained in:
parent
b0dda9aef1
commit
86396fbadb
15
Makefile
Normal file
15
Makefile
Normal file
@ -0,0 +1,15 @@
|
||||
CC?=cc
|
||||
|
||||
all:
|
||||
$(CC) -lm -o sqrt sqrt.c
|
||||
|
||||
check: all
|
||||
./sqrt
|
||||
|
||||
debug:
|
||||
$(CC) -g -lm -o sqrt sqrt.c
|
||||
gdb ./sqrt
|
||||
|
||||
bericht:
|
||||
latexmk -pdf bericht.tex;
|
||||
|
104
sqrt.c
104
sqrt.c
@ -1,52 +1,68 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
* s 1
|
||||
* \ /
|
||||
* +
|
||||
* | 1
|
||||
* \ /
|
||||
* >>
|
||||
* |
|
||||
* s x
|
||||
* \ / \
|
||||
* (/) |\
|
||||
* | | \
|
||||
* \ / \
|
||||
* + \
|
||||
* | 1 |
|
||||
* \ / |
|
||||
* >> |
|
||||
* | /
|
||||
* \ /
|
||||
* -
|
||||
*/
|
||||
typedef uint32_t u32;
|
||||
|
||||
u32 heron_sqrt(u32 s, int* iterations) {
|
||||
void heron_sqrt(u32 *mem) {
|
||||
int i;
|
||||
for (i = 1; i <= mem[0]; i++){
|
||||
u32 s = mem[i];
|
||||
// x_0 = (s + 1) / 2
|
||||
// without increment to avoid overflow for 0xffffffff
|
||||
u32 x = s >> 1;
|
||||
u32 old_x = x;
|
||||
|
||||
*iterations = 0; // statistic
|
||||
|
||||
if (x == 0) return 0;
|
||||
if (x != 0) {
|
||||
while(1) {
|
||||
(*iterations)++; // statistic
|
||||
// x_{n + 1} = (x_n + (s / x_n)) / 2
|
||||
x = (x >> 1) + ((s/x) >> 1);
|
||||
printf("%d\n", x);
|
||||
if (old_x <= x) {
|
||||
return x;
|
||||
mem[i] = x;
|
||||
break;
|
||||
}
|
||||
old_x = x;
|
||||
}
|
||||
}
|
||||
mem[i] = x;
|
||||
}
|
||||
}
|
||||
|
||||
//void heron_sqrt2(u32 *mem) {
|
||||
// int i = 1;
|
||||
// while(i <= mem[0]) {
|
||||
// u32 s = mem[i];
|
||||
// // x_0 = (s + 1) / 2
|
||||
// // without increment to avoid overflow for 0xffffffff
|
||||
// u32 x = s >> 1;
|
||||
// u32 old_x = x;
|
||||
//
|
||||
// if (x != 0) {
|
||||
// while(1) {
|
||||
// // x_{n + 1} = (x_n + (s / x_n)) / 2
|
||||
// //x = (x >> 1) + ((s/x) >> 1);
|
||||
// x = (x / 2) + ((s/x) / 2);
|
||||
// printf("%d\n", x);
|
||||
// if (old_x == x) {
|
||||
// mem[i] = x;
|
||||
// break;
|
||||
// }
|
||||
// old_x = x;
|
||||
// }
|
||||
// }
|
||||
// mem[i] = x;
|
||||
// i++;
|
||||
// }
|
||||
//}
|
||||
|
||||
int main() {
|
||||
u32 data[] = {0, 2, 4,
|
||||
u32 data[] = {0,
|
||||
1,
|
||||
2,
|
||||
4,
|
||||
1<<2,
|
||||
1<<3,
|
||||
1<<4,
|
||||
@ -62,26 +78,24 @@ int main() {
|
||||
1<<24, // 32 - 8 = 24
|
||||
1<<30,
|
||||
0xffffffff};
|
||||
int iter = 0, i = 0;
|
||||
for (i = 0; i < sizeof(data)/sizeof(int); i++) {
|
||||
if (i != 0) printf("------\n");
|
||||
const int length = sizeof(data)/sizeof(u32);
|
||||
const int mem_size = sizeof(data) + sizeof(u32);
|
||||
u32* mem = malloc(mem_size);
|
||||
memcpy(mem, data, mem_size);
|
||||
u32* mem2 = malloc(mem_size);
|
||||
memcpy(mem2, data, mem_size);
|
||||
|
||||
mem[0] = length;
|
||||
heron_sqrt(mem);
|
||||
//heron_sqrt2(mem2);
|
||||
|
||||
int i = 0;
|
||||
for (i = 0; i < length; i++) {
|
||||
printf("s = %u\n", data[i]);
|
||||
|
||||
printf("sqrt(s) = %f\n", sqrt(data[i]));
|
||||
printf("heron_sqrt(s) = %d (%d iterations)\n",
|
||||
heron_sqrt(data[i], &iter), iter);
|
||||
|
||||
// 1/16 = 0.0625
|
||||
u32 s8 = data[i] << 8;
|
||||
printf("heron_sqrt8(s) = %f (%d iterations)%s\n",
|
||||
((double) heron_sqrt(s8, &iter)) / 16,
|
||||
iter, s8 == 0 ? " Out of range!" : "");
|
||||
|
||||
// 1/128 = 0.0078125
|
||||
u32 s14 = data[i] << 14;
|
||||
printf("heron_sqrt14(s) = %f (%d iterations)%s\n",
|
||||
((double) heron_sqrt(data[i]<<14, &iter)) / 128,
|
||||
iter, s14 == 0 ? " Out of range!" : "");
|
||||
printf("heron_sqrt(s) = %d\n", mem[i]);
|
||||
//printf("heron_sqrt2(s) = %d\n", mem2[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user