【问题标题】:How could one write Duff's device in F#?一个人怎么能用 F# 编写 Duff 的设备?
【发布时间】:2011-11-18 23:13:46
【问题描述】:

我正在编写一个性能非常密集的程序并且一直在使用 C,但是有人告诉我函数式编程有多酷,所以我决定用 F# 重写它。

无论如何,我在 F# 中难以复制算法的特定功能是 Duff's device。它不是典型的迭代,而是展开循环,因此每次迭代可以复制 8 个字节,而不仅仅是一个。

void copy_memory( char* to, char* from, size_t count ) {
    size_t n = (count+7)/8;
    switch( count%8 ) {
    case 0: do{ *to++ = *from++;
    case 7:     *to++ = *from++;
    case 6:     *to++ = *from++;
    case 5:     *to++ = *from++;
    case 4:     *to++ = *from++;
    case 3:     *to++ = *from++;
    case 2:     *to++ = *from++;
    case 1:     *to++ = *from++;
            }while(--n>0);
    }
}

这利用了 case fallthrough 和在 C 中跳转到循环中间的能力,据我所知,不幸的是,F# 似乎缺少这些功能。

我在 MSDN 上阅读了一些资料,并认为 F# 的 match 功能将是我能找到的最接近 C 的 switch 的功能。于是,我开始写这段代码

open System.Reflection
let copyMemory (pTo : Pointer) (pFrom : Pointer) length =
    let n = (length + 7) / 8
    match n % 8 with
    | 0 ->

然后我不知道该怎么办。它不会让我在这里开始一个循环并在另一种情况下结束它。

在 F# 中有什么东西可以用来做 case fall-through 并跳转到循环的中间吗?如果你能为我做到这一点,我想我可以自己解决剩下的问题。

【问题讨论】:

  • 祝你好运......我认为你必须直接发出 IL 代码 ;)
  • Duff 的设备在函数式语言或任何现代编译器上都没有意义,因为如果有意义的话,它们会像这样为您优化。
  • 我意识到我的第二次尝试是错误的,所以我用更忠实的 Duff 翻译替换它。

标签: pointers loops f# copy


【解决方案1】:

这是在 F# 中拨弄内存的惯用方法:-)

#nowarn "9" //stop telling me I'm going to break something
open Microsoft.FSharp.NativeInterop

let inline (~+) ptr = NativePtr.add ptr 1

let rec copyMemory src dest = function
  | 0 -> ()
  | n -> 
    NativePtr.read src |> NativePtr.write dest
    copyMemory +src +dest (n - 1)

但这可能更符合达夫的精神

let inline (+>) s d = 
  NativePtr.read !s |> NativePtr.write !d
  s:= NativePtr.add !s 1
  d:= NativePtr.add !d 1

let copyMemory src dst count =
  let n = ref ((count + 7) / 8)
  let s, d = ref src, ref dst
  let 
    rec case_0() = s +> d; case_7()
    and case_7() = s +> d; case_6()
    and case_6() = s +> d; case_5()
    and case_5() = s +> d; case_4()
    and case_4() = s +> d; case_3()
    and case_3() = s +> d; case_2()
    and case_2() = s +> d; case_1()
    and case_1() = s +> d; decr n; if !n > 0 then case_0()
  match count % 8 with
  | 7 -> case_7() | 6 -> case_6()
  | 5 -> case_5() | 4 -> case_4()
  | 3 -> case_3() | 2 -> case_2()
  | 1 -> case_1() | _ -> case_0()

但是说真的

System.Buffer.BlockCopy(src, 0, dest, 0, count)

【讨论】:

    【解决方案2】:

    F# 中没有标签。但是,您可以通过其他方式展开循环。

    let copy (dst : nativeptr<byte>) (src : nativeptr<byte>) count =
        let mutable s = src
        let mutable d = dst
    
        for n in 1 .. count / 8 do
            NativePtr.read s |> NativePtr.write d
            s <- NativePtr.ofNativeInt(NativePtr.toNativeInt s + nativeint 1)
            d <- NativePtr.ofNativeInt(NativePtr.toNativeInt d + nativeint 1)
            NativePtr.read s |> NativePtr.write d
            s <- NativePtr.ofNativeInt(NativePtr.toNativeInt s + nativeint 1)
            d <- NativePtr.ofNativeInt(NativePtr.toNativeInt d + nativeint 1)
            NativePtr.read s |> NativePtr.write d
            s <- NativePtr.ofNativeInt(NativePtr.toNativeInt s + nativeint 1)
            d <- NativePtr.ofNativeInt(NativePtr.toNativeInt d + nativeint 1)
            NativePtr.read s |> NativePtr.write d
            s <- NativePtr.ofNativeInt(NativePtr.toNativeInt s + nativeint 1)
            d <- NativePtr.ofNativeInt(NativePtr.toNativeInt d + nativeint 1)
            NativePtr.read s |> NativePtr.write d
            s <- NativePtr.ofNativeInt(NativePtr.toNativeInt s + nativeint 1)
            d <- NativePtr.ofNativeInt(NativePtr.toNativeInt d + nativeint 1)
            NativePtr.read s |> NativePtr.write d
            s <- NativePtr.ofNativeInt(NativePtr.toNativeInt s + nativeint 1)
            d <- NativePtr.ofNativeInt(NativePtr.toNativeInt d + nativeint 1)
            NativePtr.read s |> NativePtr.write d
            s <- NativePtr.ofNativeInt(NativePtr.toNativeInt s + nativeint 1)
            d <- NativePtr.ofNativeInt(NativePtr.toNativeInt d + nativeint 1)
            NativePtr.read s |> NativePtr.write d
            s <- NativePtr.ofNativeInt(NativePtr.toNativeInt s + nativeint 1)
            d <- NativePtr.ofNativeInt(NativePtr.toNativeInt d + nativeint 1)
    
        for n in 1 .. count % 8 do
            NativePtr.read s |> NativePtr.write d
            s <- NativePtr.ofNativeInt(NativePtr.toNativeInt s + nativeint 1)
            d <- NativePtr.ofNativeInt(NativePtr.toNativeInt d + nativeint 1)
    

    在相关说明中,NativePtr.add 将在 nativeptr 上调用 sizeof,因此我在上面转换为 nativeint。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-15
      • 1970-01-01
      • 2010-10-05
      • 2011-01-20
      相关资源
      最近更新 更多