Skip to contentSkip to conversation

Future of SoftFloat use in QEMU

Qemu-devel Alex Bennée 7 participants 8 messages May 8, 2017 – May 30, 2017

lore
Share & tools
lore
#0 lore raw

Hi,

We've got a task coming up to implement half-precision floating point (FP16) for ARMv8.2. As you know pretty much all our floating point in QEMU is handled by our internal fork of John R. Hauser's BSD SoftFloat library. Our current implementation is based on version 2a which doesn't support FP16.

As it happens there has been a new release of SoftFloat recently. Version 3 is a complete re-write which made a number of changes, some notable ones being:

- Complete rewrite, different use license than earlier releases.
- Renaming most types and functions, upgrading some algorithms
- restructuring the source files, and making SoftFloat into a true library.
- Added functions to convert between floating-point and unsigned integers, both 32-bit and 64-bit (uint32_t and uint64_t).
- Added functions for fused multiply-add, for all supported floating-point formats except 80-bit double-extended-precision.
- Added support for a fifth rounding mode, near_maxMag (round to nearest, with ties to maximum magnitude, away from zero).

And in the most recent release as of February 2017, 3c:

- Added optional rounding mode odd (round to odd, also known as jamming).
- Implemented the common 16-bit “half-precision” floating-point format (float16_t)

See: http://www.jhauser.us/arithmetic/SoftFloat-3c/doc/SoftFloat-history.html

Of course the softfloat in QEMU's tree hasn't been static either. We've made numerous changes over the years to add and fix various features, including features that have since been added to the upstream softfloat. It seems unlikely we could switch to the newer softfloat without risking breaking something. However if we look at back-porting stuff from the newer library we essentially get to own our version of softfloat forever.

So what else can we do?

We could investigate having both libraries included in QEMU. It seems the API has changed quite a bit so that might be possible although there would be hackage involved in having two different softfloat.h's involved.

This would be useful if we wanted to take a piecemeal approach to updating the library. For example we could just use softfloat3 when we need the newer features (e.g. FP16).

Or we could convert one architecture at a time so each qemu binary links against either a version 2 or version 3 softfloat library. Of course that does run the risk of permanently holding two versions of softfloat in the code if the less maintained guest architectures don't convert quickly.

So any thoughts about what would make the best approach?

--
Alex Bennée

#1 lore raw
Quoted 7 lines · Alex Bennée

Hi,

We've got a task coming up to implement half-precision floating point (FP16) for ARMv8.2. As you know pretty much all our floating point in QEMU is handled by our internal fork of John R. Hauser's BSD SoftFloat library. Our current implementation is based on version 2a which doesn't support FP16.

Our fork is stuck at 2a because the license of 2b had compatibility problems (it had legal restrictions that prevent interaction with GPL).

Quoted 6 lines

As it happens there has been a new release of SoftFloat recently. Version 3 is a complete re-write which made a number of changes, some notable ones being:

- Complete rewrite, different use license than earlier releases.

And this different license is what?

Quoted 1 line

See: http://www.jhauser.us/arithmetic/SoftFloat-3c/doc/SoftFloat-history.html

which says merely "Replaced the license text supplied by the University of California, Berkeley." but not WHAT the actual license text is. Are we lucky enough that it might have a 2-clause BSD license?

/me goes digging...

http://www.jhauser.us/arithmetic/SoftFloat-3c/doc/SoftFloat.html

Quoted 6 lines

The following applies to the whole of SoftFloat Release 3c as well as to each source file individually.

Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the University of California. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Quoted 7 lines

Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation and/or other materials provided with the distribution.

Neither the name of the University nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS “AS IS”, AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Not a 2-clause BSD, but sure looks exactly like the stock 3-clause BSD license. Yay - https://www.gnu.org/licenses/license-list.en.html says it is compatible with GPL, and thus we can use it in qemu!

Quoted 1 line

So any thoughts about what would make the best approach?

No quick thoughts on the best way to proceed, but at least (from my non-lawyer perspective) it looks like we CAN quit maintaining our fork.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#2 lore raw
In reply to Alex Bennée

Hi,

Quoted 26 lines · Alex Bennée

Hi,

We've got a task coming up to implement half-precision floating point (FP16) for ARMv8.2. As you know pretty much all our floating point in QEMU is handled by our internal fork of John R. Hauser's BSD SoftFloat library. Our current implementation is based on version 2a which doesn't support FP16.

As it happens there has been a new release of SoftFloat recently. Version 3 is a complete re-write which made a number of changes, some notable ones being:

- Complete rewrite, different use license than earlier releases.
- Renaming most types and functions, upgrading some algorithms
- restructuring the source files, and making SoftFloat into a true library.
- Added functions to convert between floating-point and unsigned integers, both 32-bit and 64-bit (uint32_t and uint64_t).
- Added functions for fused multiply-add, for all supported floating-point formats except 80-bit double-extended-precision.
- Added support for a fifth rounding mode, near_maxMag (round to nearest, with ties to maximum magnitude, away from zero).

And in the most recent release as of February 2017, 3c:

- Added optional rounding mode odd (round to odd, also known as jamming).
- Implemented the common 16-bit “half-precision” floating-point format (float16_t)

See: http://www.jhauser.us/arithmetic/SoftFloat-3c/doc/SoftFloat-history.html

Quoted 7 lines

Of course the softfloat in QEMU's tree hasn't been static either. We've made numerous changes over the years to add and fix various features, including features that have since been added to the upstream softfloat. It seems unlikely we could switch to the newer softfloat without risking breaking something. However if we look at back-porting stuff from the newer library we essentially get to own our version of softfloat forever.

There have been many many changes in our forked version of softfloat:
qNaN/sNaN, IEEE754-2008 functions, squash input denormal, many floatx80 fixes, ...

Do we know if all those changes are also in the new softfloat version, and if it is not the case if we can work with upstream to implement that? That seems to me like a pre-requisite before trying to get this new version in QEMU, either as an option or as the only version.

Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                 http://www.aurel32.net
#3 lore raw
Quoted 37 lines · Aurelien Jarno

Hi,

Quoted 26 lines · Alex Bennée

Hi,

We've got a task coming up to implement half-precision floating point (FP16) for ARMv8.2. As you know pretty much all our floating point in QEMU is handled by our internal fork of John R. Hauser's BSD SoftFloat library. Our current implementation is based on version 2a which doesn't support FP16.

As it happens there has been a new release of SoftFloat recently. Version 3 is a complete re-write which made a number of changes, some notable ones being:

- Complete rewrite, different use license than earlier releases.
- Renaming most types and functions, upgrading some algorithms
- restructuring the source files, and making SoftFloat into a true library.
- Added functions to convert between floating-point and unsigned integers, both 32-bit and 64-bit (uint32_t and uint64_t).
- Added functions for fused multiply-add, for all supported floating-point formats except 80-bit double-extended-precision.
- Added support for a fifth rounding mode, near_maxMag (round to nearest, with ties to maximum magnitude, away from zero).

And in the most recent release as of February 2017, 3c:

- Added optional rounding mode odd (round to odd, also known as jamming).
- Implemented the common 16-bit “half-precision” floating-point format (float16_t)

See: http://www.jhauser.us/arithmetic/SoftFloat-3c/doc/SoftFloat-history.html

Quoted 7 lines

Of course the softfloat in QEMU's tree hasn't been static either. We've made numerous changes over the years to add and fix various features, including features that have since been added to the upstream softfloat. It seems unlikely we could switch to the newer softfloat without risking breaking something. However if we look at back-porting stuff from the newer library we essentially get to own our version of softfloat forever.

Quoted 3 lines

There have been many many changes in our forked version of softfloat:
qNaN/sNaN, IEEE754-2008 functions, squash input denormal, many floatx80 fixes, ...

Note that we've apparently also got plenty of bugs in our version of softloat left, for example:

- https://bugs.launchpad.net/qemu/+bug/645662
- http://lists.nongnu.org/archive/html/qemu-ppc/2017-05/msg00187.html

... would be interesting to know whether such issues are fixed with the newer version of softfloat...

Thomas

#4 lore raw
Quoted 38 lines · Thomas Huth
Quoted 37 lines · Aurelien Jarno

Hi,

Quoted 33 lines · Alex Bennée

Hi,

We've got a task coming up to implement half-precision floating point (FP16) for ARMv8.2. As you know pretty much all our floating point in QEMU is handled by our internal fork of John R. Hauser's BSD SoftFloat library. Our current implementation is based on version 2a which doesn't support FP16.

As it happens there has been a new release of SoftFloat recently. Version 3 is a complete re-write which made a number of changes, some notable ones being:

- Complete rewrite, different use license than earlier releases.
- Renaming most types and functions, upgrading some algorithms
- restructuring the source files, and making SoftFloat into a true library.
- Added functions to convert between floating-point and unsigned integers, both 32-bit and 64-bit (uint32_t and uint64_t).
- Added functions for fused multiply-add, for all supported floating-point formats except 80-bit double-extended-precision.
- Added support for a fifth rounding mode, near_maxMag (round to nearest, with ties to maximum magnitude, away from zero).

And in the most recent release as of February 2017, 3c:

- Added optional rounding mode odd (round to odd, also known as jamming).
- Implemented the common 16-bit “half-precision” floating-point format (float16_t)

See: http://www.jhauser.us/arithmetic/SoftFloat-3c/doc/SoftFloat-history.html

Of course the softfloat in QEMU's tree hasn't been static either. We've made numerous changes over the years to add and fix various features, including features that have since been added to the upstream softfloat. It seems unlikely we could switch to the newer softfloat without risking breaking something. However if we look at back-porting stuff from the newer library we essentially get to own our version of softfloat forever.

Quoted 6 lines
Quoted 3 lines

There have been many many changes in our forked version of softfloat:
qNaN/sNaN, IEEE754-2008 functions, squash input denormal, many floatx80 fixes, ...

Note that we've apparently also got plenty of bugs in our version of softloat left, for example:

I don't say that our version of softfloat is bug free, but I would not blame softfloat without further investigation:

Quoted 1 line

- https://bugs.launchpad.net/qemu/+bug/645662

qemu/i386 doesn't use softfloat for many instructions, but rather rely on the float/double type of the host. This is mostly because softfloat doesn't provide directly usable trigonometric functions. There are also a few simple ones that might be more easily converted to softfloat.

Quoted 4 lines

- http://lists.nongnu.org/archive/html/qemu-ppc/2017-05/msg00187.html

... would be interesting to know whether such issues are fixed with the newer version of softfloat...

The exception flags are likely to be a bug in the PowerPC implementation, as this CPU provides more fine grained exception than what softfloat provides. The actual wrong answers which match the expected fpscr value look very suspicious to me.

Aurelien

Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                 http://www.aurel32.net
#5 lore raw
In reply to Alex Bennée
Quoted 3 lines · Alex Bennée

Of course the softfloat in QEMU's tree hasn't been static either. We've made numerous changes over the years to add and fix various features, including features that have since been added to the upstream softfloat.

Based on way Peter achieved the switch to softfloat 2a, we can reasonably identify the set of custom QEMU changes to the original 2a code base. From that diff, plus the subsequent git history, it is within realm of practicality to at least identify areas where we might need enhance/customize version 3.

Quoted 4 lines

It seems unlikely we could switch to the newer softfloat without risking breaking something. However if we look at back-porting stuff from the newer library we essentially get to own our version of softfloat forever.

Maintaining our own fork forever certainly feels unappealing, particularly with the big feature gap you identify for FP16

Quoted 11 lines

So what else can we do?

We could investigate having both libraries included in QEMU. It seems the API has changed quite a bit so that might be possible although there would be hackage involved in having two different softfloat.h's involved.

This would be useful if we wanted to take a piecemeal approach to updating the library. For example we could just use softfloat3 when we need the newer features (e.g. FP16).

Quoted 7 lines

Or we could convert one architecture at a time so each qemu binary links against either a version 2 or version 3 softfloat library. Of course that does run the risk of permanently holding two versions of softfloat in the code if the less maintained guest architectures don't convert quickly.

So any thoughts about what would make the best approach?

WRT to supporting the FP16 feature, I'd certainly vote for using softfloat3, over trying to re-implement FP16 ourselves in the old codebase we have a fork of, even if that means us carrying both versions forever (provided having both versions present in QEMU doesn't cause us symbol clashes ?).

Incrementally converting existing usage to softfloat3 is reasonable, but doesn't seem like a decision that needs to be an immediate blocker for FP16 usage. Given our desire to kill off areas of code which are not actively maintained, it could be reasonable to set a timebox on softfloat3 conversion for architectures eg must be completed within 3 release cycles, or we'll consider the architecture unmaintained and subject for removal.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#6 lore raw
In reply to Alex Bennée
Quoted 8 lines · Alex Bennée

Hi,

We've got a task coming up to implement half-precision floating point (FP16) for ARMv8.2. As you know pretty much all our floating point in QEMU is handled by our internal fork of John R. Hauser's BSD SoftFloat library. Our current implementation is based on version 2a which doesn't support FP16.

Quoted 7 lines

As it happens there has been a new release of SoftFloat recently. Version 3 is a complete re-write which made a number of changes, some notable ones being:

- Complete rewrite, different use license than earlier releases.
- Renaming most types and functions, upgrading some algorithms
- restructuring the source files, and making SoftFloat into a true library.

But also now using thread-local globals instead of passing in a structure with all of the parameters. So, really, we'd probably wind up touching every function if we were to import it.

Quoted 11 lines

So what else can we do?

We could investigate having both libraries included in QEMU. It seems the API has changed quite a bit so that might be possible although there would be hackage involved in having two different softfloat.h's involved.

This would be useful if we wanted to take a piecemeal approach to updating the library. For example we could just use softfloat3 when we need the newer features (e.g. FP16).

Quoted 5 lines

Or we could convert one architecture at a time so each qemu binary links against either a version 2 or version 3 softfloat library. Of course that does run the risk of permanently holding two versions of softfloat in the code if the less maintained guest architectures don't convert quickly.

Another possibility is the code that's shared between the linux kernel (include/math-emu) and glibc (soft-fp). The glibc version has support for f16 while the kernel doesn't. But the glibc version is LGPL 2.1, which I'd expect to be ok.

It is mildly nasty in that it's a collection of macros, but that's how we managed really good performance with the compiler tech of 15 years ago. On the other hand, it would be fairly easy to adjust the support macros to achive source compatibility with our current code.

Just a thought...

r~

#7 lore raw
Quoted 9 lines · Richard Henderson

Another possibility is the code that's shared between the linux kernel (include/math-emu) and glibc (soft-fp). The glibc version has support for f16 while the kernel doesn't. But the glibc version is LGPL 2.1, which I'd expect to be ok.

It is mildly nasty in that it's a collection of macros, but that's how we managed really good performance with the compiler tech of 15 years ago. On the other hand, it would be fairly easy to adjust the support macros to achive source compatibility with our current code.

Having tangled with that collection of macros 15 years ago I definitely can't recommend it ;-)

thanks
-- PMM