|
| 1 | +/************************************************************************* |
| 2 | + * |
| 3 | + * This file is part of ACT standard library |
| 4 | + * |
| 5 | + * Copyright (c) 2025 Jakob Jordan |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * |
| 19 | + ************************************************************************** |
| 20 | + */ |
| 21 | + |
| 22 | +namespace math { |
| 23 | + |
| 24 | +export |
| 25 | +template<pint N> |
| 26 | +defproc ripple_carry_adder(chan?(int<N>) a_in, b_in; chan!(int<N>) s_out) { |
| 27 | + int<N> a, b; |
| 28 | + int<1> c[N]; |
| 29 | + int<1> s[N]; |
| 30 | + int<N> st; |
| 31 | + chp { |
| 32 | + *[ a_in?a, b_in?b; |
| 33 | + s[0] := (a{0} ^ b{0}), |
| 34 | + c[0] := a{0} & b{0}; |
| 35 | + (;i : N-1 : s[i + 1] := (a{i + 1} ^ b{i + 1} ^ c[i]), |
| 36 | + c[i + 1] := (a{i + 1} & b{i + 1} | ((a{i + 1} ^ b{i + 1}) & c[i]))); |
| 37 | + st := (| i : N : s[i] << i); |
| 38 | + s_out!st |
| 39 | + ] |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +export |
| 44 | +template<pint N> |
| 45 | +defproc carry_save_adder(chan?(int<N>) a_in, b_in, c_in; chan!(int<N>) s_out, d_out) { |
| 46 | + int<N> a, b, c; |
| 47 | + int<1> s[N], d[N]; |
| 48 | + int<N> st, dt; |
| 49 | + |
| 50 | + chp { |
| 51 | + *[ a_in?a, b_in?b, c_in?c; |
| 52 | + (, i : N : s[i] := (a{i} ^ b{i} ^ c{i}), d[i] := (a{i} & b{i} | ((a{i} ^ b{i}) & c{i}))); |
| 53 | + st := (| i : N : s[i] << i), |
| 54 | + dt := (| i : N : d[i] << i); |
| 55 | + s_out!st, d_out!(dt << 1) |
| 56 | + ] |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +} |
0 commit comments