On Thu, Jul 27, 2023 at 06:41:44PM +0000, Joseph Myers wrote: > On Thu, 27 Jul 2023, Jakub Jelinek via Gcc-patches wrote: > > > - _BitInt(N) bit-fields aren't supported yet (the patch rejects them); I'd like > > to enable those incrementally, but don't really see details on how such > > bit-fields should be laid-out in memory nor passed inside of function > > arguments; LLVM implements something, but it is a question if that is what > > the various ABIs want > > So if the x86-64 ABI (or any other _BitInt ABI that already exists) > doesn't specify this adequately then an issue should be filed (at > https://gitlab.com/x86-psABIs/x86-64-ABI/-/issues in the x86-64 case). > > (Note that the language specifies that e.g. _BitInt(123):45 gets promoted > to _BitInt(123) by the integer promotions, rather than left as a type with > the bit-field width.) Ok, I'll try to investigate in detail what LLVM does and what GCC would do if I just enabled the bitfield support and report. Still, I'd like to handle this only in incremental step after the rest of _BitInt support goes in. > > - conversions between large/huge (see later) _BitInt and _Decimal{32,64,128} > > aren't support and emit a sorry; I'm not familiar enough with DFP stuff > > to implement that > > Doing things incrementally might indicate first doing this only for BID > (so sufficing for x86-64), with DPD support to be added when _BitInt > support is added for an architecture using DPD, i.e. powerpc / s390. > > This conversion is a mix of base conversion and things specific to DFP > types. I had a brief look at libbid and am totally unimpressed. Seems we don't implement {,unsigned} __int128 <-> _Decimal{32,64,128} conversions at all (we emit calls to __bid_* functions which don't exist), the library (or the way we configure it) doesn't care about exceptions nor rounding mode (see following testcase) and for integral <-> _Decimal32 conversions implement them as integral <-> _Decimal64 <-> _Decimal32 conversions. While in the _Decimal32 -> _Decimal64 -> integral direction that is probably ok, even if exceptions and rounding (other than to nearest) were supported, the other direction I'm sure can suffer from double rounding. So, wonder if it wouldn't be better to implement these in the soft-fp infrastructure which at least has the exception and rounding mode support. Unlike DPD, decoding BID seems to be about 2 simple tests of the 4 bits below the sign bit and doing some shifts, so not something one needs a 10MB of a library for. Now, sure, 5MB out of that are generated tables in bid_binarydecimal.c, but unfortunately those are static and not in a form which could be directly fed into multiplication (unless we'd want to go through conversions to/from strings). So, it seems to be easier to guess needed power of 10 from number of binary digits or vice versa, have a small table of powers of 10 (say those which fit into a limb) and construct larger powers of 10 by multiplicating those several times, _Decimal128 has exponent up to 6144 which is ~ 2552 bytes or 319 64-bit limbs, but having a table with all the 6144 powers of ten would be just huge. In 64-bit limb fit power of ten until 10^19, so we might need say < 32 multiplications to cover it all (but with the current 575 bits limitation far less). Perhaps later on write a few selected powers of 10 as _BitInt to decrease that number. > For conversion *from _BitInt to DFP*, the _BitInt value needs to be > expressed in decimal. In the absence of optimized multiplication / > division for _BitInt, it seems reasonable enough to do this naively > (repeatedly dividing by a power of 10 that fits in one limb to determine > base 10^N digits from the least significant end, for example), modulo > detecting obvious overflow cases up front (if the absolute value is at Wouldn't it be cheaper to guess using the 10^3 ~= 2^10 approximation and instead repeatedly multiply like in the other direction and then just divide once with remainder? Jakub