Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6828235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:21:34+00:00 2026-05-26T22:21:34+00:00

I am writing a very performance intense program and have been using C, but

  • 0

I am writing a very performance intense program and have been using C, but somebody told me how cool functional programming is, so I’ve decided to rewrite it in F#.

Anyway, the particular function I am having a hard replicating the algorithm in F# is Duff’s device. Instead of the typical iteration, it unwinds the loop so it can copy 8 bytes per iteration instead of just one.

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);
    }
}

This takes advantage of case fallthrough and the ability to jump to the middle of a loop in C, which, as far as I can tell, are unfortunately features that F# seems to be missing.

I read some stuff on MSDN, and figured that F#’s match feature would be the closest I could get to C’s switch. So, I started to write this bit of code

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

and then I couldn’t figure out what to do. It wouldn’t let me start a loop here and end it in another case.

Is there something in F# that I can use to do case fall-through and jump into the middle of a loop? If you can do that for me, I think I can figure out the rest myself.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:21:34+00:00Added an answer on May 26, 2026 at 10:21 pm

    Here’s the idiomatic way to fiddle memory in 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)
    

    But this is probably more in the spirit of Duff’s

    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()
    

    But seriously

    System.Buffer.BlockCopy(src, 0, dest, 0, count)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing Objective-C code with LLVM. I have one file full of very performance-critical
I am writing a program that is very critical to performance. I am polling
I'm writing a very simple text editor and have run into a somewhat minor
Background I am writing and using a very simple CGI-based (Perl) content management tool
I have been asked to look into writing an application that will be a
I'm writing very processor-intensive cryptography code (C#), so I'm looking for any performance gains,
I am writing a lot of JQuery code in a page and have decided
Hello I have always been thinking which is the best way of writing html
I am writing a very specialized app in C# that floats as a mostly
I am writing a (very small) framework for checking pre- and postconditions of methods.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.