如何在Linux内核中除以两个64位数字? [英] How to divide two 64-bit numbers in Linux Kernel?

查看:107
本文介绍了如何在Linux内核中除以两个64位数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些将四舍五入的代码示范(C语法):

Some code that rounds up the division to demonstrate (C-syntax):

#define SINT64 long long int
#define SINT32 long int

SINT64 divRound(SINT64 dividend, SINT64 divisor)
{
  SINT32 quotient1 = dividend / divisor;

  SINT32 modResult = dividend % divisor;
  SINT32 multResult = modResult * 2;
  SINT32 quotient2 = multResult / divisor;

  SINT64 result = quotient1 + quotient2;

  return ( result );
}

现在,如果这是用户空间,我们甚至可能不会注意到我们的编译器正在为那些运算符生成代码(例如,divdi3()用于除法).很有可能我们甚至不知道与libgcc链接.问题在于内核空间不同(例如,否libgcc).该怎么办?

Now, if this were User-space we probably wouldn't even notice that our compiler is generating code for those operators (e.g. divdi3() for division). Chances are we link with libgcc without even knowing it. The problem is that Kernel-space is different (e.g. no libgcc). What to do?

爬行Google一段时间,请注意,几乎所有人都在处理未签名的变体:

Crawl Google for a while, notice that pretty much everyone addresses the unsigned variant:

#define UINT64 long long int
#define UINT32 long int

UINT64 divRound(UINT64 dividend, UINT64 divisor)
{
  UINT32 quotient1 = dividend / divisor;

  UINT32 modResult = dividend % divisor;
  UINT32 multResult = modResult * 2;
  UINT32 quotient2 = multResult / divisor;

  UINT64 result = quotient1 + quotient2;

  return ( result );
}

我知道如何解决此问题:在 asm/div64.h 中用do_div()覆盖udivdi3()umoddi3().做对了吗?错误的.签名与未签名是不同的,sdivdi3()不能简单地调用udivdi3(),由于某种原因,它们是独立的函数.

I know how to fix this one: Override udivdi3() and umoddi3() with do_div() from asm/div64.h. Done right? Wrong. Signed is not the same as unsigned, sdivdi3() does not simply call udivdi3(), they are separate functions for a reason.

您解决了这个问题吗?您是否知道可以帮助我做到这一点的图书馆?我真的很困惑,所以无论您在这里看到什么,我现在都不知道都是有用的.

Have you solved this problem? Do you know of a library that will help me do this? I'm really stuck so whatever you might see here that I just don't right now would be really helpful.

谢谢, 乍得

推荐答案

此功能在/linux/lib/div64.c 早于内核v2.6.22.

This functionality is introduced in /linux/lib/div64.c as early as kernel v2.6.22.

这篇关于如何在Linux内核中除以两个64位数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆