[ create a new paste ] login | about

Link: http://codepad.org/YZvm3LB6    [ raw code | output | fork ]

C, pasted on Oct 24:
#include <stdio.h>





struct INT128 {
	unsigned char	i[16];
};





// INT128 ← char[16]
void write_INT128(struct INT128* d, unsigned char* s)
{
	unsigned char* p = d->i;
	int i=16; while(i-->0){ *p++ = *s++; }
}

// char[16] ← INT128
void read_INT128(struct INT128* s, unsigned char* d)
{
	unsigned char* p = s->i;
	int i=16; while(i-->0){ *d++ = *p++; }
}





// a = a + b
void add_INT128(struct INT128* a, struct INT128* b)
{
	unsigned char* ap = a->i;
	unsigned char* bp = b->i;

	unsigned short c = 0;
	int i=16; while(i-->0){
		c = ((unsigned short)*ap) + ((unsigned short)*bp) + (c>>8);
		*ap = (unsigned char)c;

		ap++;
		bp++;
	}
}

// a = b
void mov_INT128(struct INT128* a,struct INT128* b)
{
int i;
       for(i=0;i<16;i++){ a->i[i] = b->i[i];}
}

// a = ~a
void not_INT128(struct INT128* a)
{
int i;
       for(i=0;i<16;i++){ a->i[i] = ~(a->i[i]);}
}

// a = a - b
void sub_INT128(struct INT128* a, struct INT128* b)
{
struct INT128 c;
struct INT128 const1={1};
       mov_INT128(&c,b);
       not_INT128(&c);
       add_INT128(&c,&const1);
       add_INT128(a,&c);
}

// a = a * b
void mul_INT128(struct INT128* a, struct INT128* b)
{
	//
}

// a = a / b
void div_INT128(struct INT128* a, struct INT128* b)
{
	//
}

// a = a >> b
void right_shift_INT128(struct INT128* a, int b)
{
	//
}

// a = a << b
void left_shift_INT128(struct INT128* a, int b)
{
	//
}

// a = a & b
void and_INT128(struct INT128* a, struct INT128* b)
{
	//
}

// a = a | b
void or_INT128(struct INT128* a, struct INT128* b)
{
	//
}

// a = a ^ b
void xor_INT128(struct INT128* a, struct INT128* b)
{
	//
}







void print_0x_INT128(struct INT128* a)
{
	printf("0x");

	unsigned char* p = a->i + (16 - 1);
	int i=16; while(i-->0){ printf("%02X", *p--); }
}





//test
void main()
{
	unsigned char c[16] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160};

	struct INT128 a;	write_INT128(&a,c);
	struct INT128 b;	write_INT128(&b,c);
	
	
	// add test
	printf("add test\n");
	printf("a = ");		print_0x_INT128(&a);	printf("\n");
	printf("b = ");		print_0x_INT128(&b);	printf("\n");
	
	printf("a = a + b\n");
	add_INT128(&a,&b);
	
	printf("a =  ");	print_0x_INT128(&a);	printf("\n");
	
	
	
}


Output:
1
2
3
4
5
add test
a = 0xA0968C82786E645A50463C32281E140A
b = 0xA0968C82786E645A50463C32281E140A
a = a + b
a =  0x412D1904F0DCC8B4A08C7864503C2814


Create a new paste based on this one


Comments: