Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/FSharp.Core/array.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1578,19 +1578,38 @@ module Array =
checkNonNull "array" array
Microsoft.FSharp.Primitives.Basics.Array.permute indexMap array

[<CompiledName("Sum")>]
let inline sum (array: ^T array) : ^T =
let inline private classicSum (array: ^T array) : ^T =
checkNonNull "array" array
let mutable acc = LanguagePrimitives.GenericZero< ^T>

for i = 0 to array.Length - 1 do
acc <- Checked.(+) acc array.[i]

acc
when ^T : float = (System.Linq.Enumerable.Sum : IEnumerable<float> -> float) (# "" array : IEnumerable<float> #)
when ^T : float32 = (System.Linq.Enumerable.Sum : IEnumerable<float32> -> float32) (# "" array : IEnumerable<float32> #)
when ^T : int = (System.Linq.Enumerable.Sum : IEnumerable<int> -> int) (# "" array : IEnumerable<int> #)
when ^T : int64 = (System.Linq.Enumerable.Sum : IEnumerable<int64> -> int64) (# "" array : IEnumerable<int64> #)

[<CompiledName("Sum")>]
let inline sum (array: ^T array) : ^T =
classicSum array
when ^T : float =
if System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith ".NET Framework" then classicSum array
else
let r = (System.Linq.Enumerable.Sum : IEnumerable<float> -> float) (# "" array : IEnumerable<float> #)
(# "" r : 'T #)
when ^T : float32 =
if System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith ".NET Framework" then classicSum array
else
let r = (System.Linq.Enumerable.Sum : IEnumerable<float32> -> float32) (# "" array : IEnumerable<float32> #)
(# "" r : 'T #)
when ^T : int =
if System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith ".NET Framework" then classicSum array
else
let r = (System.Linq.Enumerable.Sum : IEnumerable<int> -> int) (# "" array : IEnumerable<int> #)
(# "" r : 'T #)
when ^T : int64 =
if System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith ".NET Framework" then classicSum array
else
let r = (System.Linq.Enumerable.Sum : IEnumerable<int64> -> int64) (# "" array : IEnumerable<int64> #)
(# "" r : 'T #)

[<CompiledName("SumBy")>]
let inline sumBy ([<InlineIfLambda>] projection: 'T -> ^U) (array: 'T array) : ^U =
Expand Down
31 changes: 25 additions & 6 deletions src/FSharp.Core/seq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1464,19 +1464,38 @@ module Seq =
else
mkDelayedSeq (fun () -> countByRefType projection source)

[<CompiledName("Sum")>]
let inline sum (source: seq< ^a >) : ^a =
let inline private classicSum (source: seq< ^a >) : ^a =
use e = source.GetEnumerator()
let mutable acc = LanguagePrimitives.GenericZero< ^a>

while e.MoveNext() do
acc <- Checked.(+) acc e.Current

acc
when ^a: int64 = (System.Linq.Enumerable.Sum: IEnumerable<int64> -> int64) (# "" source : IEnumerable<int64> #)
when ^a: int = (System.Linq.Enumerable.Sum: IEnumerable<int> -> int) (# "" source : IEnumerable<int> #)
when ^a: float32 = (System.Linq.Enumerable.Sum: IEnumerable<float32> -> float32) (# "" source : IEnumerable<float32> #)
when ^a: float = (System.Linq.Enumerable.Sum: IEnumerable<float> -> float) (# "" source : IEnumerable<float> #)

[<CompiledName("Sum")>]
let inline sum (source: seq< ^a >) : ^a =
classicSum source
when ^a: int64 =
if System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith ".NET Framework" then classicSum source
else
let r = (System.Linq.Enumerable.Sum: IEnumerable<int64> -> int64) (# "" source : IEnumerable<int64> #)
(# "" r : 'a #)
when ^a: int =
if System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith ".NET Framework" then classicSum source
else
let r = (System.Linq.Enumerable.Sum: IEnumerable<int> -> int) (# "" source : IEnumerable<int> #)
(# "" r : 'a #)
when ^a: float32 =
if System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith ".NET Framework" then classicSum source
else
let r = (System.Linq.Enumerable.Sum: IEnumerable<float32> -> float32) (# "" source : IEnumerable<float32> #)
(# "" r : 'a #)
when ^a: float =
if System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith ".NET Framework" then classicSum source
else
let r = (System.Linq.Enumerable.Sum: IEnumerable<float> -> float) (# "" source : IEnumerable<float> #)
(# "" r : 'a #)

[<CompiledName("SumBy")>]
let inline sumBy ([<InlineIfLambda>] projection: 'T -> ^U) (source: seq<'T>) : ^U =
Expand Down
Loading