Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
67
Task/Four-bit-adder/D/four-bit-adder-1.d
Normal file
67
Task/Four-bit-adder/D/four-bit-adder-1.d
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
import std.stdio, std.traits;
|
||||
|
||||
void fourBitsAdder(T)(in T a0, in T a1, in T a2, in T a3,
|
||||
in T b0, in T b1, in T b2, in T b3,
|
||||
out T o0, out T o1,
|
||||
out T o2, out T o3,
|
||||
out T overflow) pure nothrow @nogc {
|
||||
|
||||
// A XOR using only NOT, AND and OR, as task requires.
|
||||
static T xor(in T x, in T y) pure nothrow @nogc {
|
||||
return (~x & y) | (x & ~y);
|
||||
}
|
||||
|
||||
static void halfAdder(in T a, in T b,
|
||||
out T s, out T c) pure nothrow @nogc {
|
||||
s = xor(a, b);
|
||||
// s = a ^ b; // The built-in D xor.
|
||||
c = a & b;
|
||||
}
|
||||
|
||||
static void fullAdder(in T a, in T b, in T ic,
|
||||
out T s, out T oc) pure nothrow @nogc {
|
||||
T ps, pc, tc;
|
||||
|
||||
halfAdder(/*in*/a, b, /*out*/ps, pc);
|
||||
halfAdder(/*in*/ps, ic, /*out*/s, tc);
|
||||
oc = tc | pc;
|
||||
}
|
||||
|
||||
T zero, tc0, tc1, tc2;
|
||||
|
||||
fullAdder(/*in*/a0, b0, zero, /*out*/o0, tc0);
|
||||
fullAdder(/*in*/a1, b1, tc0, /*out*/o1, tc1);
|
||||
fullAdder(/*in*/a2, b2, tc1, /*out*/o2, tc2);
|
||||
fullAdder(/*in*/a3, b3, tc2, /*out*/o3, overflow);
|
||||
}
|
||||
|
||||
void main() {
|
||||
alias T = size_t;
|
||||
static assert(isUnsigned!T);
|
||||
|
||||
enum T one = T.max,
|
||||
zero = T.min,
|
||||
a0 = zero, a1 = one, a2 = zero, a3 = zero,
|
||||
b0 = zero, b1 = one, b2 = one, b3 = one;
|
||||
T s0, s1, s2, s3, overflow;
|
||||
|
||||
fourBitsAdder(/*in*/ a0, a1, a2, a3,
|
||||
/*in*/ b0, b1, b2, b3,
|
||||
/*out*/s0, s1, s2, s3, overflow);
|
||||
|
||||
writefln(" a3 %032b", a3);
|
||||
writefln(" a2 %032b", a2);
|
||||
writefln(" a1 %032b", a1);
|
||||
writefln(" a0 %032b", a0);
|
||||
writefln(" +");
|
||||
writefln(" b3 %032b", b3);
|
||||
writefln(" b2 %032b", b2);
|
||||
writefln(" b1 %032b", b1);
|
||||
writefln(" b0 %032b", b0);
|
||||
writefln(" =");
|
||||
writefln(" s3 %032b", s3);
|
||||
writefln(" s2 %032b", s2);
|
||||
writefln(" s1 %032b", s1);
|
||||
writefln(" s0 %032b", s0);
|
||||
writefln("overflow %032b", overflow);
|
||||
}
|
||||
66
Task/Four-bit-adder/D/four-bit-adder-2.d
Normal file
66
Task/Four-bit-adder/D/four-bit-adder-2.d
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
import std.stdio, std.traits, core.simd;
|
||||
|
||||
void fourBitsAdder(T)(in T a0, in T a1, in T a2, in T a3,
|
||||
in T b0, in T b1, in T b2, in T b3,
|
||||
out T o0, out T o1,
|
||||
out T o2, out T o3,
|
||||
out T overflow) pure nothrow {
|
||||
|
||||
// A XOR using only NOT, AND and OR, as task requires.
|
||||
static T xor(in T x, in T y) pure nothrow {
|
||||
return (~x & y) | (x & ~y);
|
||||
}
|
||||
|
||||
static void halfAdder(in T a, in T b,
|
||||
out T s, out T c) pure nothrow {
|
||||
s = xor(a, b);
|
||||
// s = a ^ b; // The built-in D xor.
|
||||
c = a & b;
|
||||
}
|
||||
|
||||
static void fullAdder(in T a, in T b, in T ic,
|
||||
out T s, out T oc) pure nothrow {
|
||||
T ps, pc, tc;
|
||||
|
||||
halfAdder(/*in*/a, b, /*out*/ps, pc);
|
||||
halfAdder(/*in*/ps, ic, /*out*/s, tc);
|
||||
oc = tc | pc;
|
||||
}
|
||||
|
||||
T zero, tc0, tc1, tc2;
|
||||
|
||||
fullAdder(/*in*/a0, b0, zero, /*out*/o0, tc0);
|
||||
fullAdder(/*in*/a1, b1, tc0, /*out*/o1, tc1);
|
||||
fullAdder(/*in*/a2, b2, tc1, /*out*/o2, tc2);
|
||||
fullAdder(/*in*/a3, b3, tc2, /*out*/o3, overflow);
|
||||
}
|
||||
|
||||
int main() {
|
||||
alias T = ubyte16; // ubyte32 with AVX.
|
||||
immutable T zero;
|
||||
immutable T one = ubyte.max;
|
||||
immutable T a0 = zero, a1 = one, a2 = zero, a3 = zero,
|
||||
b0 = zero, b1 = one, b2 = one, b3 = one;
|
||||
T s0, s1, s2, s3, overflow;
|
||||
|
||||
fourBitsAdder(/*in*/ a0, a1, a2, a3,
|
||||
/*in*/ b0, b1, b2, b3,
|
||||
/*out*/s0, s1, s2, s3, overflow);
|
||||
|
||||
//writefln(" a3 %(%08b%)", a3);
|
||||
writefln(" a3 %(%08b%)", a3.array);
|
||||
writefln(" a2 %(%08b%)", a2.array);
|
||||
writefln(" a1 %(%08b%)", a1.array);
|
||||
writefln(" a0 %(%08b%)", a0.array);
|
||||
writefln(" +");
|
||||
writefln(" b3 %(%08b%)", b3.array);
|
||||
writefln(" b2 %(%08b%)", b2.array);
|
||||
writefln(" b1 %(%08b%)", b1.array);
|
||||
writefln(" b0 %(%08b%)", b0.array);
|
||||
writefln(" =");
|
||||
writefln(" s3 %(%08b%)", s3.array);
|
||||
writefln(" s2 %(%08b%)", s2.array);
|
||||
writefln(" s1 %(%08b%)", s1.array);
|
||||
writefln(" s0 %(%08b%)", s0.array);
|
||||
writefln("overflow %(%08b%)", overflow.array);
|
||||
}
|
||||
39
Task/Four-bit-adder/D/four-bit-adder-3.d
Normal file
39
Task/Four-bit-adder/D/four-bit-adder-3.d
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
fourBitsAdder:
|
||||
pushl %ebp
|
||||
movl %esp, %ebp
|
||||
andl $-32, %esp
|
||||
subl $32, %esp
|
||||
vmovaps 136(%ebp), %ymm4
|
||||
vxorps %ymm3, %ymm4, %ymm5
|
||||
movl 20(%ebp), %ecx
|
||||
vmovaps %ymm5, (%ecx)
|
||||
vandps %ymm3, %ymm4, %ymm3
|
||||
vmovaps 104(%ebp), %ymm4
|
||||
vxorps %ymm2, %ymm4, %ymm5
|
||||
vxorps %ymm3, %ymm5, %ymm6
|
||||
movl 16(%ebp), %ecx
|
||||
vmovaps %ymm6, (%ecx)
|
||||
vandps %ymm3, %ymm5, %ymm3
|
||||
vandps %ymm2, %ymm4, %ymm2
|
||||
vorps %ymm2, %ymm3, %ymm2
|
||||
vmovaps 72(%ebp), %ymm3
|
||||
vxorps %ymm1, %ymm3, %ymm4
|
||||
vxorps %ymm2, %ymm4, %ymm5
|
||||
movl 12(%ebp), %ecx
|
||||
vmovaps %ymm5, (%ecx)
|
||||
vandps %ymm2, %ymm4, %ymm2
|
||||
vandps %ymm1, %ymm3, %ymm1
|
||||
vorps %ymm1, %ymm2, %ymm1
|
||||
vmovaps 40(%ebp), %ymm2
|
||||
vxorps %ymm0, %ymm2, %ymm3
|
||||
vxorps %ymm1, %ymm3, %ymm4
|
||||
movl 8(%ebp), %ecx
|
||||
vmovaps %ymm4, (%ecx)
|
||||
vandps %ymm1, %ymm3, %ymm1
|
||||
vandps %ymm0, %ymm2, %ymm0
|
||||
vorps %ymm0, %ymm1, %ymm0
|
||||
vmovaps %ymm0, (%eax)
|
||||
movl %ebp, %esp
|
||||
popl %ebp
|
||||
vzeroupper
|
||||
ret $160
|
||||
Loading…
Add table
Add a link
Reference in a new issue