Notes on GSoC Decimal arithmetic

Hi all --

A few people have reached out about the Decimal64 GSoC project, so I thought I would write up one set of notes and have a single thread to answer any questions.

This project is about implementing arithmetic and basic operations in the IEEE 754 Decimal 64 format (decimal64 floating-point format - Wikipedia). This is distinct from Foundation.Decimal provided by Foundation, and from "big decimal" arbitrary-precision formats provided in some languages. The Wikipedia page (and links from there) is a good resource to familiarize yourself with the basics. The formal reference is the IEEE 754 standard, but that document costs money, so I would not expect you to have a copy while writing a proposal. I will arrange for you to have access if your proposal is funded.

Decimal64 will be delivered in a new module within Swift Numerics; following a period of some polish, I would expect it to be merged into the standard library at some future point (this will require a Swift Evolution proposal, which you might choose to do or help with, but that is outside the scope of this GSoC project).

There are a few implementations of Decimal64 in other languages. The most widely used open-source implementation that I know of is Intel's (misc/intel), written in C and BSD-licensed. It is useful as a reference (especially for testing purposes), but we will probably make some different choices in the course of our own implementation.

The Decimal64 type should end up conforming to the following protocols:

Essentially all of the API surface of the type flows from these two protocols (and the protocols they refine), so there is little or no API design to be done as part of this project*, and the semantics of these operations are almost fully specified by the IEEE 754 bindings, so this is in some sense "pure" implementation--there is a nearly complete specification, all that is to be done is to make it work.

(*) A follow-on / stretch task would be to define new API that handles rounding control, which Swift doesn't currently model for binary floating-point.

10 Likes

Questions from other threads:

Foundation.Decimal (a.k.a NSDecimal) is fine, but pre-dates the IEEE 754 standard for decimal arithmetic, and makes some different choices than the standard does. For the purposes of interoperation and data interchange with other languages, it is more desirable to have an implementation of the standardized arithmetic in Swift.

That's a perfectly useful feature, but it's a fundamentally different feature. Fixed-size types are especially useful for data interchange, but also provide opportunities for much better performance. Just like we have Float and Double and Int, we want to have fixed-width decimal as well.

This is addressed in my first post; the initial API is entirely determined by the protocols to which the type would conform and the semantics determined by the bindings of those requirements to the IEEE 754 standard.

4 Likes

Probably useful to link to your earlier reply as well with a bunch of useful information:

1 Like

Hi, all
As Swift.org - Project Ideas for GSoC 2021 mentioned, we can accomplish the project in C or Swift. (I know there's a way to execute C code in Swift by the bridging header file.) However, I found all implementations are written in Swift when I take a look into the swift-numerics repository. Can we implement Decimal Floating-Point in C because of lack of familiarity with Swift? Does it matter whether to implement it in C or Swift?

Part of the motivation for the project is to exercise parts of the Swift compiler and optimizer that are relatively less-used. Therefore this should be implemented in Swift (also, if we were going to do a C implementation, we would simply wrap Intel's existing BSD-licensed library instead).

That said, this does not require deep familiarity with Swift, because it will necessarily mainly use the more "C-like" parts of the Swift language.

Was this GSoC proposal accepted / did this move forward?

Not finding much about it beyond this post and those from potential implementers.

No. I am planning to do an implementation myself sometime in the middle-future, unless someone decides to give it a go.

2 Likes

Is it a pure swift implementation or is c / c++ required?
How much time would this feature take?

For Swift Numerics, I would like to have a pure-swift implementation¹. It would be a fairly simple hobby project to write Swift wrappers for Intel's BSD-licensed library in another package, though.


¹ I've been not very secretly landing some of the integer primitives that make building it easier already.

3 Likes