From b49df0526518869457098ae6f54c72874cca7e78 Mon Sep 17 00:00:00 2001
From: bay-max1 <34803732+bay-max1@users.noreply.github.com>
Date: Sat, 23 Dec 2017 22:37:37 -0800
Subject: [PATCH] Fix for uselongdouble problem in irange2cidrlist()

Implement log2 sub routine directly, to avoid precision problems with floor()
problems with perls built with uselongdouble defined.
Credit: xenu, on IRC, for _log2 sub routine.
---
 lib/Net/Netmask.pm | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/lib/Net/Netmask.pm b/lib/Net/Netmask.pm
index b26a1f7..8237566 100644
--- a/lib/Net/Netmask.pm
+++ b/lib/Net/Netmask.pm
@@ -464,7 +464,7 @@ sub irange2cidrlist
 	my @result;
 	while ($end >= $start) {
 		my $maxsize = imaxblock($start, 32);
-		my $maxdiff = 32 - floor(log($end - $start + 1)/log(2));
+		my $maxdiff = 32 - _log2($end - $start + 1);
 		$maxsize = $maxdiff if $maxsize < $maxdiff;
 		push (@result, bless {
 			'IBASE' => $start,
@@ -617,6 +617,18 @@ sub split
 				( 0 .. ( $parts - 1 ) );
 }
 
+# Implement log2 sub routine directly, to avoid precision problems with floor() 
+# problems with perls built with uselongdouble defined.
+# Credit: xenu, on IRC
+sub _log2 {
+   my $n = shift;
+   
+   my $ret = 0;
+   $ret++ while ($n >>= 1);
+   
+   return $ret;
+}
+
 BEGIN {
 	for (my $i = 0; $i <= 32; $i++) {
 		$imask[$i] = imask($i);