Skip to content

Commit

Permalink
Add __divdi3(), remove __udivdi3() kernel dependency
Browse files Browse the repository at this point in the history
Up until now no SPL consumer attempted to perform signed 64-bit
division so there was no need to support this.  That has now
changed so I adding 64-bit division support for 32-bit platforms.
The signed implementation is based on the unsigned version.

Since the have been several bug reports in the past concerning
correct 64-bit division on 32-bit platforms I added some long
over due regression tests.  Much to my surprise the unsigned
64-bit division regression tests failed.

This was surprising because __udivdi3() was implemented by simply
calling div64_u64() which is provided by the kernel.  This meant
that the linux kernels 64-bit division algorithm on 32-bit platforms
was flawed.  After some investigation this turned out to be exactly
the case.

Because of this I was forced to abandon the kernel helper and
instead to fully implement 64-bit division in the spl.  There are
several published implementation out there on how to do this
properly and I settled on one proposed in the book Hacker's Delight.
Their proposed algoritm is freely available without restriction
and I have just modified it to be linux kernel friendly.

The update implementation now passed all the unsigned and signed
regression tests.  This should be functional, but not fast, which is
good enough for out purposes.  If you want fast too I'd strongly
suggest you upgrade to a 64-bit platform.  I have also reported the
kernel bug and we'll see if we can't get it fixed up stream.
  • Loading branch information
behlendorf committed Jul 13, 2010
1 parent d466208 commit a4bfd8e
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 221 deletions.
28 changes: 0 additions & 28 deletions config/spl-build.m4
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ AC_DEFUN([SPL_AC_CONFIG_KERNEL], [
SPL_AC_INODE_I_MUTEX
SPL_AC_MUTEX_OWNER
SPL_AC_MUTEX_LOCK_NESTED
SPL_AC_DIV64_64
SPL_AC_DIV64_U64
SPL_AC_3ARGS_ON_EACH_CPU
SPL_AC_KALLSYMS_LOOKUP_NAME
SPL_AC_GET_VMALLOC_INFO
Expand Down Expand Up @@ -950,32 +948,6 @@ AC_DEFUN([SPL_AC_MUTEX_LOCK_NESTED], [
])
])

dnl #
dnl # 2.6.22 API change,
dnl # First introduced 'div64_64()' in lib/div64.c
dnl
AC_DEFUN([SPL_AC_DIV64_64], [
SPL_CHECK_SYMBOL_EXPORT(
[div64_64],
[],
[AC_DEFINE(HAVE_DIV64_64, 1,
[div64_64() is available])],
[])
])

dnl #
dnl # 2.6.26 API change,
dnl # Renamed 'div64_64()' to 'div64_u64' in lib/div64.c
dnl #
AC_DEFUN([SPL_AC_DIV64_U64], [
SPL_CHECK_SYMBOL_EXPORT(
[div64_u64],
[],
[AC_DEFINE(HAVE_DIV64_U64, 1,
[div64_u64() is available])],
[])
])

dnl #
dnl # 2.6.27 API change,
dnl # on_each_cpu() uses 3 args, no 'retry' argument
Expand Down
164 changes: 0 additions & 164 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -13406,88 +13406,6 @@ fi



{ $as_echo "$as_me:$LINENO: checking whether symbol div64_64 is exported" >&5
$as_echo_n "checking whether symbol div64_64 is exported... " >&6; }
grep -q -E '[[:space:]]div64_64[[:space:]]' \
$LINUX_OBJ/Module*.symvers 2>/dev/null
rc=$?
if test $rc -ne 0; then
export=0
for file in ; do
grep -q -E "EXPORT_SYMBOL.*(div64_64)" \
"$LINUX_OBJ/$file" 2>/dev/null
rc=$?
if test $rc -eq 0; then
export=1
break;
fi
done
if test $export -eq 0; then
{ $as_echo "$as_me:$LINENO: result: no" >&5
$as_echo "no" >&6; }

else
{ $as_echo "$as_me:$LINENO: result: yes" >&5
$as_echo "yes" >&6; }

cat >>confdefs.h <<\_ACEOF
#define HAVE_DIV64_64 1
_ACEOF

fi
else
{ $as_echo "$as_me:$LINENO: result: yes" >&5
$as_echo "yes" >&6; }

cat >>confdefs.h <<\_ACEOF
#define HAVE_DIV64_64 1
_ACEOF

fi



{ $as_echo "$as_me:$LINENO: checking whether symbol div64_u64 is exported" >&5
$as_echo_n "checking whether symbol div64_u64 is exported... " >&6; }
grep -q -E '[[:space:]]div64_u64[[:space:]]' \
$LINUX_OBJ/Module*.symvers 2>/dev/null
rc=$?
if test $rc -ne 0; then
export=0
for file in ; do
grep -q -E "EXPORT_SYMBOL.*(div64_u64)" \
"$LINUX_OBJ/$file" 2>/dev/null
rc=$?
if test $rc -eq 0; then
export=1
break;
fi
done
if test $export -eq 0; then
{ $as_echo "$as_me:$LINENO: result: no" >&5
$as_echo "no" >&6; }

else
{ $as_echo "$as_me:$LINENO: result: yes" >&5
$as_echo "yes" >&6; }

cat >>confdefs.h <<\_ACEOF
#define HAVE_DIV64_U64 1
_ACEOF

fi
else
{ $as_echo "$as_me:$LINENO: result: yes" >&5
$as_echo "yes" >&6; }

cat >>confdefs.h <<\_ACEOF
#define HAVE_DIV64_U64 1
_ACEOF

fi



{ $as_echo "$as_me:$LINENO: checking whether on_each_cpu() wants 3 args" >&5
$as_echo_n "checking whether on_each_cpu() wants 3 args... " >&6; }

Expand Down Expand Up @@ -16932,88 +16850,6 @@ fi



{ $as_echo "$as_me:$LINENO: checking whether symbol div64_64 is exported" >&5
$as_echo_n "checking whether symbol div64_64 is exported... " >&6; }
grep -q -E '[[:space:]]div64_64[[:space:]]' \
$LINUX_OBJ/Module*.symvers 2>/dev/null
rc=$?
if test $rc -ne 0; then
export=0
for file in ; do
grep -q -E "EXPORT_SYMBOL.*(div64_64)" \
"$LINUX_OBJ/$file" 2>/dev/null
rc=$?
if test $rc -eq 0; then
export=1
break;
fi
done
if test $export -eq 0; then
{ $as_echo "$as_me:$LINENO: result: no" >&5
$as_echo "no" >&6; }

else
{ $as_echo "$as_me:$LINENO: result: yes" >&5
$as_echo "yes" >&6; }

cat >>confdefs.h <<\_ACEOF
#define HAVE_DIV64_64 1
_ACEOF

fi
else
{ $as_echo "$as_me:$LINENO: result: yes" >&5
$as_echo "yes" >&6; }

cat >>confdefs.h <<\_ACEOF
#define HAVE_DIV64_64 1
_ACEOF

fi



{ $as_echo "$as_me:$LINENO: checking whether symbol div64_u64 is exported" >&5
$as_echo_n "checking whether symbol div64_u64 is exported... " >&6; }
grep -q -E '[[:space:]]div64_u64[[:space:]]' \
$LINUX_OBJ/Module*.symvers 2>/dev/null
rc=$?
if test $rc -ne 0; then
export=0
for file in ; do
grep -q -E "EXPORT_SYMBOL.*(div64_u64)" \
"$LINUX_OBJ/$file" 2>/dev/null
rc=$?
if test $rc -eq 0; then
export=1
break;
fi
done
if test $export -eq 0; then
{ $as_echo "$as_me:$LINENO: result: no" >&5
$as_echo "no" >&6; }

else
{ $as_echo "$as_me:$LINENO: result: yes" >&5
$as_echo "yes" >&6; }

cat >>confdefs.h <<\_ACEOF
#define HAVE_DIV64_U64 1
_ACEOF

fi
else
{ $as_echo "$as_me:$LINENO: result: yes" >&5
$as_echo "yes" >&6; }

cat >>confdefs.h <<\_ACEOF
#define HAVE_DIV64_U64 1
_ACEOF

fi



{ $as_echo "$as_me:$LINENO: checking whether on_each_cpu() wants 3 args" >&5
$as_echo_n "checking whether on_each_cpu() wants 3 args... " >&6; }

Expand Down
32 changes: 32 additions & 0 deletions include/linux/math64_compat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*****************************************************************************\
* Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
* Copyright (C) 2007 The Regents of the University of California.
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
* Written by Brian Behlendorf <behlendorf1@llnl.gov>.
* UCRL-CODE-235197
*
* This file is part of the SPL, Solaris Porting Layer.
* For details, see <http://github.com/behlendorf/spl/>.
*
* The SPL 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.
*
* The SPL 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.
*
* You should have received a copy of the GNU General Public License along
* with the SPL. If not, see <http://www.gnu.org/licenses/>.
\*****************************************************************************/

#ifndef _SPL_MATH64_COMPAT_H
#define _SPL_MATH64_COMPAT_H

#ifndef abs64
#define abs64(x) ({ uint64_t t = (x) >> 63; ((x) ^ t) - t; })
#endif

#endif /* _SPL_MATH64_COMPAT_H */
1 change: 1 addition & 0 deletions include/sys/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <linux/module_compat.h>
#include <linux/sysctl_compat.h>
#include <linux/proc_compat.h>
#include <linux/math64_compat.h>

#ifndef HAVE_UINTPTR_T
typedef unsigned long uintptr_t;
Expand Down
Loading

0 comments on commit a4bfd8e

Please sign in to comment.