阅读:4802回复:11
浮点运算
请问如何能在WINDOWS过滤文件驱动中实现浮点运算.谢谢.
|
|
沙发#
发布于:2007-07-11 11:42
在内核驱动中使用浮点运算需要用到 KeSaveFloatingPointState 和 KeRestoreFloatingPointState 保存FPU状态,参考一下DDK文档吧。
KFLOATING_SAVE saveData; NTSTATUS status; double floatValue; status = KeSaveFloatingPointState(&saveData); if (NT_SUCCESS(status)) { floatValue = 1.0; KeRestoreFloatingPointState(&saveData); } |
|
|
板凳#
发布于:2007-07-11 15:11
谢谢,我按照这种用法,在编译时出现如下错误报告:
error LNK2001: unresolved external symbol __fltused : error LNK1120: 1 unresolved externals 如何解决?万分感谢. |
|
地板#
发布于:2007-07-12 10:42
我没试过,能否将你的代码贴一点出来
|
|
|
地下室#
发布于:2007-07-12 10:54
在你的代码里面加一行:
ULONG _fltused; 这个方法来源于: http://www.osronline.com/lists_archive/ntdev/thread9344.html __fltused is a data symbol reference generated by the compiler when it sees floating point math being used in a source file. There are no generated code references to this symbol. The point of the symbol is to insure that the linker will pull in the necessary floating point support libraries needed to make the code work. It is possible you may have additional unresolved library calls like _ftol, which are actual references that will have to be resolved somehow. __fltused can be resolved by declaring a DWORD in your data with this name. Loren |
|
|
5楼#
发布于:2007-07-12 11:01
还有一篇:
来源:http://www.leftycp.com/archiver/?tid-4152.html ------------------------------------------------------ 关于浮点运算: 用KeSaveFloatingPointState和KeRestoreFloatingPointState 不过我一直没有机会用 直到前几天,我用上了。我发现直接用这两函数,会报错,说不能link 外部的__fltused。 同事加了个全局的ulong __fltused = 1;错误没有了,可是结果并不正确。sprintf("%3f",x)不对,x是个float。 后来发现,加上msvcrt.lib就好了,也不用定义全局的变量。 Walt Oney有更全面的解释,哪天贴上来 ------------------------------------------------------- 把Walt Oney关于浮点运算的一个帖子贴上来吧 From: Walter Oney Subject: Re: Using Floating variable in a WDM driver Date: 1999/09/22 Message-ID: <37E8B83D.AFA107ED@oneysoft.com>#1/1 Content-Transfer-Encoding: 7bit References: <7sa8kg$elc$1@news.entreprises.cegetel.fr> Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@rcn.com X-Trace: GjGUm2k/h6dchDSW3dAtWL+k38GPA70bhhY4oqkqhG8= Organization: Walter Oney Software Mime-Version: 1.0 Reply-To: waltoney@oneysoft.com NNTP-Posting-Date: 22 Sep 1999 11:08:34 GMT Newsgroups: comp.os.ms-windows.programmer.drivers,comp.os.ms-windows.programmer.nt.kernel-mode David BOUYEURE wrote: > I want to use afloating variable in my driver (98 WDM). > I have this error at compile time : > "unresolved external symbol __fltused" > I guess I have to link with a special lib, but I haven't found wich one. The problem is worse than just finding the right library, unfortunately. The C compiler's floating point support assumes that it will be operating in a an application environment where you can initialize the coprocessor, install some exception handlers, and then blast away. It also assumes that the operating system will take care of saving and restoring each thread's coprocessor context as required by all the thread context switches that occur from then on. These assumptions aren't usually true in a driver. Furthermore, the runtime library support for coprocessor exceptions can't work because there's a whole bunch of missing infrastructure. What you basically need to do is write your code in such a way that you initialize the coprocessor each time you want to use it (don't forget KeSaveFloatingPointState and KeRestoreFloatingPointState). Set things up so that the coprocessor will never generate an exception, too. Then you can simply define the symbol __fltused somewhere to satisfy the linker.(All that symbol usually does is drag in the runtime support. You don't want that support becuase, as I said, it won't work in kernel mode.) You'll undoubtedly need some assembly language code for the initialization steps. If you have a system thread that will be doing all your floating point math, you can initialize the coprocesor once at the start of the thread. The system will save and restore your state as necessary from then on. Don't forget that you can only do floating point at IRQL < DISPATCH_LEVEL. -- Walter Oney [url] http://www.oneysoft.com[/url] |
|
|
6楼#
发布于:2007-07-12 11:23
我试了一下,如果是用WDK编译,加msvcrt.lib不行,但是加ntstrsafe.lib就可以了:
TARGETLIBS=$(DDK_LIB_PATH)\ntstrsafe.lib 在代码里面加 ULONG _fltused; 的方法也可以编译通过,但不知道运算结果对不对,你试一下吧,把结果贴出来 ^-^ |
|
|
7楼#
发布于:2007-07-12 15:14
Thanks!
我用的是DDK, 加了MSVCRT.LIB, 编译即通过. 感谢 seaquester 的热情帮助. |
|
8楼#
发布于:2007-07-13 10:53
我从网上找到的资料,不管是使用什么版本的DDK(或WDK),kernel-mode驱动链接 libcntpr.lib 就行了。MSVCRT.LIB是给用户模式程序使用的。
来源: http://www.osronline.com/showThread.cfm?link=98633 |
|
|
9楼#
发布于:2007-07-13 18:16
使用 libcntpr.lib 同样也编译通过, 那一个能真正工作等我过段时间测试后向大家汇报.
再次表示感谢! |
|
10楼#
发布于:2009-01-02 19:07
我在高于 DISPATCH_ LEVEL 里进行浮点计算没有任何问题
谁知道为什么会有 irql <= DISPATCH_ LEVEL 的限制 |
|
11楼#
发布于:2009-01-04 16:23
用户被禁言,该主题自动屏蔽! |
|