1
0
mirror of https://github.com/aolofsson/oh.git synced 2025-02-07 06:44:09 +08:00
oh/common/hdl/oh_abs.v
2020-01-28 18:12:57 -05:00

22 lines
859 B
Verilog

//#############################################################################
//# Function: Calculates absolute value of input #
//#############################################################################
//# Author: Andreas Olofsson #
//# License: MIT (see LICENSE file in OH! repository) #
//#############################################################################
module oh_abs #(parameter DW = 2) // data width
(
input [DW-1:0] in, //input operand
output [DW-1:0] out, //out = abs(in) (signed two's complement)
output overflow //high for max negative #
);
assign out[DW-1:0] = in[DW-1] ? ~in[DW-1:0] + 1'b1 :
in[DW-1:0];
assign overflow = in[DW-1] & ~(|in[DW-2:0]);
endmodule // oh_abs