Add Div_Round_Closest() for positive integer types

Change-Id: Ic8d398902947bf18d97c9ab3c6ca6204aa0629f0
Signed-off-by: Nico Huber <nico.huber@secunet.com>
Reviewed-on: https://review.coreboot.org/18356
Reviewed-by: Arthur Heymans <arthur@aheymans.xyz>
Reviewed-by: Paul Menzel <paulepanter@users.sourceforge.net>
Tested-by: Nico Huber <nico.h@gmx.de>
diff --git a/common/Makefile.inc b/common/Makefile.inc
index b8e4f35..e74081d 100644
--- a/common/Makefile.inc
+++ b/common/Makefile.inc
@@ -1,3 +1,4 @@
+hw-y += hw.adb
 hw-y += hw.ads
 hw-y += hw-mmio_range.ads
 hw-y += hw-mmio_regs.adb
diff --git a/common/hw.adb b/common/hw.adb
new file mode 100644
index 0000000..b8f1e52
--- /dev/null
+++ b/common/hw.adb
@@ -0,0 +1,37 @@
+--
+-- Copyright (C) 2017 secunet Security Networks AG
+--
+-- This program is free software; you can redistribute it and/or modify
+-- it under the terms of the GNU General Public License as published by
+-- the Free Software Foundation; either version 2 of the License, or
+-- (at your option) any later version.
+--
+-- This program is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+-- GNU General Public License for more details.
+--
+
+package body HW is
+
+   function Div_Round_Closest (N, M : Pos8) return Int8 is
+   begin
+      return (N + M / 2) / M;
+   end Div_Round_Closest;
+
+   function Div_Round_Closest (N, M : Pos16) return Int16 is
+   begin
+      return (N + M / 2) / M;
+   end Div_Round_Closest;
+
+   function Div_Round_Closest (N, M : Pos32) return Int32 is
+   begin
+      return (N + M / 2) / M;
+   end Div_Round_Closest;
+
+   function Div_Round_Closest (N, M : Pos64) return Int64 is
+   begin
+      return (N + M / 2) / M;
+   end Div_Round_Closest;
+
+end HW;
diff --git a/common/hw.ads b/common/hw.ads
index 9adb794..6537542 100644
--- a/common/hw.ads
+++ b/common/hw.ads
@@ -1,5 +1,5 @@
 --
--- Copyright (C) 2015 secunet Security Networks AG
+-- Copyright (C) 2015-2017 secunet Security Networks AG
 --
 -- This program is free software; you can redistribute it and/or modify
 -- it under the terms of the GNU General Public License as published by
@@ -53,6 +53,30 @@
    subtype Pos32 is Interfaces.Integer_32 range 1 .. Interfaces.Integer_32'Last;
    subtype Pos64 is Interfaces.Integer_64 range 1 .. Interfaces.Integer_64'Last;
 
+   use type Pos8;
+   function Div_Round_Closest (N, M : Pos8) return Int8
+   with
+      Pre => N <= Pos8'Last - M / 2,
+      Post => Div_Round_Closest'Result = (N + M / 2) / M;
+
+   use type Pos16;
+   function Div_Round_Closest (N, M : Pos16) return Int16
+   with
+      Pre => N <= Pos16'Last - M / 2,
+      Post => Div_Round_Closest'Result = (N + M / 2) / M;
+
+   use type Pos32;
+   function Div_Round_Closest (N, M : Pos32) return Int32
+   with
+      Pre => N <= Pos32'Last - M / 2,
+      Post => Div_Round_Closest'Result = (N + M / 2) / M;
+
+   use type Pos64;
+   function Div_Round_Closest (N, M : Pos64) return Int64
+   with
+      Pre => N <= Pos64'Last - M / 2,
+      Post => Div_Round_Closest'Result = (N + M / 2) / M;
+
    subtype Buffer_Range is Natural range 0 .. Natural'Last - 1;
    type Buffer is array (Buffer_Range range <>) of Byte;