Ignore:
Timestamp:
Jan 7, 2021, 3:27:00 PM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
2b4daf2, 64aeca0
Parents:
3c64c668 (diff), eef8dfb (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into park_unpark

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/iostream.cfa

    r3c64c668 r58fe85a  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Feb 20 15:53:23 2020
    13 // Update Count     : 829
     12// Last Modified On : Mon Aug 24 08:31:35 2020
     13// Update Count     : 1130
    1414//
    1515
    1616#include "iostream.hfa"
    1717
    18 extern "C" {
    1918#include <stdio.h>
    2019#include <stdbool.h>                                                                    // true/false
    2120#include <stdint.h>                                                                             // UINT64_MAX
    22 //#include <string.h>                                                                   // strlen, strcmp
     21#include <float.h>                                                                              // DBL_DIG, LDBL_DIG
     22#include <complex.h>                                                                    // creal, cimag
     23//#include <string.h>                                                                   // strlen, strcmp, memcpy
     24extern "C" {
    2325extern size_t strlen (const char *__s) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1)));
    2426extern int strcmp (const char *__s1, const char *__s2) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2)));
    2527extern char *strcpy (char *__restrict __dest, const char *__restrict __src) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2)));
    2628extern void *memcpy (void *__restrict __dest, const void *__restrict __src, size_t __n) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2)));
    27 #include <float.h>                                                                              // DBL_DIG, LDBL_DIG
    28 #include <math.h>                                                                               // isfinite
    29 #include <complex.h>                                                                    // creal, cimag
    3029} // extern "C"
    3130
    32 
    33 //*********************************** ostream ***********************************
     31#include "math.hfa"                                                                             // isfinite, floor, ceiling_div
     32#include "bitmanip.hfa"                                                                 // high1
     33
     34
     35// *********************************** ostream ***********************************
    3436
    3537
    3638forall( dtype ostype | ostream( ostype ) ) {
    37         ostype & ?|?( ostype & os, zero_t ) {
    38                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
    39                 fmt( os, "%d", 0n );
    40                 return os;
    41         } // ?|?
    42         void ?|?( ostype & os, zero_t z ) {
    43                 (ostype &)(os | z); ends( os );
    44         } // ?|?
    45 
    46         ostype & ?|?( ostype & os, one_t ) {
    47                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
    48                 fmt( os, "%d", 1n );
    49                 return os;
    50         } // ?|?
    51         void ?|?( ostype & os, one_t o ) {
    52                 (ostype &)(os | o); ends( os );
    53         } // ?|?
    54 
    5539        ostype & ?|?( ostype & os, bool b ) {
    5640                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     
    165149        #define P10_UINT64 10_000_000_000_000_000_000_ULL       // 19 zeroes
    166150
    167         static void base10_128( ostype & os, unsigned int128 val ) {
    168                 if ( val > UINT64_MAX ) {
     151        static inline void base10_128( ostype & os, unsigned int128 val ) {
     152#if defined(__GNUC__) && __GNUC_PREREQ(7,0)                             // gcc version >= 7
     153                if ( val > P10_UINT64 ) {
     154#else
     155                if ( (uint64_t)(val >> 64) != 0 || (uint64_t)val > P10_UINT64 ) { // patch gcc 5 & 6 -O3 bug
     156#endif // __GNUC_PREREQ(7,0)
    169157                        base10_128( os, val / P10_UINT64 );                     // recursive
    170158                        fmt( os, "%.19lu", (uint64_t)(val % P10_UINT64) );
     
    174162        } // base10_128
    175163
    176         static void base10_128( ostype & os, int128 val ) {
     164        static inline void base10_128( ostype & os, int128 val ) {
    177165                if ( val < 0 ) {
    178166                        fmt( os, "-" );                                                         // leading negative sign
     
    445433} // distribution
    446434
    447 //*********************************** manipulators ***********************************
    448 
    449 //*********************************** integral ***********************************
     435// *********************************** manipulators ***********************************
     436
     437// *********************************** integral ***********************************
    450438
    451439static const char * shortbin[] = { "0", "1", "10", "11", "100", "101", "110", "111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" };
     
    453441
    454442// Default prefix for non-decimal prints is 0b, 0, 0x.
    455 #define IntegralFMTImpl( T, CODE, IFMTNP, IFMTP ) \
     443#define IntegralFMTImpl( T, IFMTNP, IFMTP ) \
    456444forall( dtype ostype | ostream( ostype ) ) { \
    457445        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
     
    459447\
    460448                if ( f.base == 'b' || f.base == 'B' ) {                 /* bespoke binary format */ \
    461                         int bits;                                                                                                       \
    462                         if ( f.val == (T){0} ) bits = 1;                        /* force at least one bit to print */ \
    463                         else bits = sizeof(long long int) * 8 - __builtin_clzll( f.val ); /* position of most significant bit */ \
    464                         bits = bits > sizeof(f.val) * 8 ? sizeof(f.val) * 8 : bits; \
    465                         int spaces = f.wd - bits;                                       /* can be negative */ \
    466                         if ( ! f.flags.nobsdp ) { spaces -= 2; }        /* base prefix takes space */ \
    467                         /* printf( "%d %d\n", bits, spaces ); */ \
     449                        int bits = high1( f.val );                                      /* position of most significant bit */ \
     450                        if ( bits == 0 ) bits = 1;                                      /* 0 value => force one bit to print */ \
     451                        int spaces; \
    468452                        if ( ! f.flags.left ) {                                         /* right justified ? */ \
    469453                                /* Note, base prefix then zero padding or spacing then prefix. */ \
    470                                 if ( f.flags.pad0 || f.flags.pc ) { \
     454                                if ( f.flags.pc ) { \
     455                                        spaces = f.wd - f.pc; \
     456                                        if ( ! f.flags.nobsdp ) { spaces -= 2; } /* base prefix takes space */ \
     457                                        if ( spaces > 0 ) fmt( os, "%*s", spaces, " " ); /* space pad */ \
    471458                                        if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \
    472                                         if ( f.flags.pc ) spaces = f.pc - bits; \
     459                                        spaces = f.pc - bits; \
    473460                                        if ( spaces > 0 ) fmt( os, "%0*d", spaces, 0 ); /* zero pad */ \
    474461                                } else { \
    475                                         if ( spaces > 0 ) fmt( os, "%*s", spaces, " " ); /* space pad */ \
    476                                         if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \
     462                                        spaces = f.wd - bits; \
     463                                        if ( ! f.flags.nobsdp ) { spaces -= 2; } /* base prefix takes space */ \
     464                                        if ( f.flags.pad0 ) { \
     465                                                if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \
     466                                                if ( spaces > 0 ) fmt( os, "%0*d", spaces, 0 ); /* zero pad */ \
     467                                        } else { \
     468                                                if ( spaces > 0 ) fmt( os, "%*s", spaces, " " ); /* space pad */ \
     469                                                if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \
     470                                        } /* if */ \
    477471                                } /* if */ \
    478                         } else if ( ! f.flags.nobsdp ) { \
    479                                 fmt( os, "0%c", f.base ); \
     472                        } else { \
     473                                if ( ! f.flags.nobsdp ) fmt( os, "0%c", f.base ); \
     474                                if ( f.flags.pc ) { \
     475                                        spaces = f.pc - bits; \
     476                                        if ( spaces > 0 ) fmt( os, "%0*d", spaces, 0 ); /* zero pad */ \
     477                                        spaces = f.wd - f.pc; \
     478                                } else { /* pad0 flag ignored with left flag */ \
     479                                        spaces = f.wd - bits; \
     480                                } /* if */ \
     481                                if ( ! f.flags.nobsdp ) { spaces -= 2; } /* base prefix takes space */ \
    480482                        } /* if */ \
    481                         int shift = (bits - 1) / 4 * 4; /* floor( bits - 1, 4 ) */ \
     483                        int shift = floor( bits - 1, 4 ); \
    482484                        typeof( f.val ) temp = f.val; \
    483485                        fmt( os, "%s", shortbin[(temp >> shift) & 0xf] ); \
     
    490492                        if ( f.flags.left && spaces > 0 ) fmt( os, "%*s", spaces, " " ); \
    491493                        return os; \
    492                 } /* if  */ \
     494                } /* if */ \
    493495\
    494496                char fmtstr[sizeof(IFMTP)];                                             /* sizeof includes '\0' */ \
     
    500502                if ( ! f.flags.nobsdp ) { fmtstr[star] = '#'; star -= 1; } \
    501503                if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } \
    502                 if ( f.flags.sign && f.base == CODE ) { fmtstr[star] = '+'; star -= 1; } \
     504                if ( f.flags.sign ) { fmtstr[star] = '+'; star -= 1; } \
    503505                if ( f.flags.pad0 && ! f.flags.pc ) { fmtstr[star] = '0'; star -= 1; } \
    504506                fmtstr[star] = '%'; \
     
    506508                if ( ! f.flags.pc ) {                                                   /* no precision */ \
    507509                        fmtstr[sizeof(IFMTNP)-2] = f.base;                      /* sizeof includes '\0' */ \
    508                         /* printf( "%s %c %c\n", &fmtstr[star], f.base, CODE ); */ \
     510                        /* printf( "%s %c\n", &fmtstr[star], f.base ); */ \
    509511                        fmt( os, &fmtstr[star], f.wd, f.val ); \
    510512                } else {                                                                                /* precision */ \
    511513                        fmtstr[sizeof(IFMTP)-2] = f.base;                       /* sizeof includes '\0' */ \
    512                         /* printf( "%s %c %c\n", &fmtstr[star], f.base, CODE ); */ \
     514                        /* printf( "%s %c\n", &fmtstr[star], f.base ); */ \
    513515                        fmt( os, &fmtstr[star], f.wd, f.pc, f.val ); \
    514516                } /* if */ \
     
    518520} // distribution
    519521
    520 IntegralFMTImpl( signed char, 'd', "%    *hh ", "%    *.*hh " )
    521 IntegralFMTImpl( unsigned char, 'u', "%    *hh ", "%    *.*hh " )
    522 IntegralFMTImpl( signed short int, 'd', "%    *h ", "%    *.*h " )
    523 IntegralFMTImpl( unsigned short int, 'u', "%    *h ", "%    *.*h " )
    524 IntegralFMTImpl( signed int, 'd', "%    * ", "%    *.* " )
    525 IntegralFMTImpl( unsigned int, 'u', "%    * ", "%    *.* " )
    526 IntegralFMTImpl( signed long int, 'd', "%    *l ", "%    *.*l " )
    527 IntegralFMTImpl( unsigned long int, 'u', "%    *l ", "%    *.*l " )
    528 IntegralFMTImpl( signed long long int, 'd', "%    *ll ", "%    *.*ll " )
    529 IntegralFMTImpl( unsigned long long int, 'u', "%    *ll ", "%    *.*ll " )
    530 
    531 
     522IntegralFMTImpl( signed char, "%    *hh ", "%    *.*hh " )
     523IntegralFMTImpl( unsigned char, "%    *hh ", "%    *.*hh " )
     524IntegralFMTImpl( signed short int, "%    *h ", "%    *.*h " )
     525IntegralFMTImpl( unsigned short int, "%    *h ", "%    *.*h " )
     526IntegralFMTImpl( signed int, "%    * ", "%    *.* " )
     527IntegralFMTImpl( unsigned int, "%    * ", "%    *.* " )
     528IntegralFMTImpl( signed long int, "%    *l ", "%    *.*l " )
     529IntegralFMTImpl( unsigned long int, "%    *l ", "%    *.*l " )
     530IntegralFMTImpl( signed long long int, "%    *ll ", "%    *.*ll " )
     531IntegralFMTImpl( unsigned long long int, "%    *ll ", "%    *.*ll " )
     532
     533#if 0
    532534#if defined( __SIZEOF_INT128__ )
    533535// Default prefix for non-decimal prints is 0b, 0, 0x.
    534536#define IntegralFMTImpl128( T, SIGNED, CODE, IFMTNP, IFMTP ) \
    535537forall( dtype ostype | ostream( ostype ) ) \
    536 static void base10_128( ostype & os, _Ostream_Manip(T) fmt ) { \
    537         if ( fmt.val > UINT64_MAX ) { \
    538                 fmt.val /= P10_UINT64; \
    539                 base10_128( os, fmt ); /* recursive */ \
    540                 _Ostream_Manip(unsigned long long int) fmt2 @= { (uint64_t)(fmt.val % P10_UINT64), 0, 19, 'u', { .all : 0 } }; \
    541                 fmt2.flags.nobsdp = true; \
    542                 printf( "fmt2 %c %lld %d\n", fmt2.base, fmt2.val, fmt2.all );   \
     538static void base10_128( ostype & os, _Ostream_Manip(T) f ) { \
     539        if ( f.val > UINT64_MAX ) { \
     540                unsigned long long int lsig = f.val % P10_UINT64; \
     541                f.val /= P10_UINT64; /* msig */ \
     542                base10_128( os, f ); /* recursion */ \
     543                _Ostream_Manip(unsigned long long int) fmt @= { lsig, 0, 19, 'u', { .all : 0 } }; \
     544                fmt.flags.nobsdp = true; \
     545                /* printf( "fmt1 %c %lld %d\n", fmt.base, fmt.val, fmt.all ); */ \
    543546                sepOff( os ); \
    544                 (ostype &)(os | fmt2); \
     547                (ostype &)(os | fmt); \
    545548        } else { \
    546                 printf( "fmt %c %lld %d\n", fmt.base, fmt.val, fmt.all ); \
     549                /* printf( "fmt2 %c %lld %d\n", f.base, (unsigned long long int)f.val, f.all ); */ \
     550                _Ostream_Manip(SIGNED long long int) fmt @= { (SIGNED long long int)f.val, f.wd, f.pc, f.base, { .all : f.all } }; \
    547551                (ostype &)(os | fmt); \
    548552        } /* if */ \
    549 } /* base10_128 */                                                \
     553} /* base10_128 */ \
    550554forall( dtype ostype | ostream( ostype ) ) { \
    551555        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
    552556                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); \
    553557\
    554                 if ( f.base == 'b' | f.base == 'o' | f.base == 'x' | f.base == 'X' ) { \
     558                if ( f.base == 'b' | f.base == 'B' | f.base == 'o' | f.base == 'x' | f.base == 'X' ) { \
    555559                        unsigned long long int msig = (unsigned long long int)(f.val >> 64); \
    556560                        unsigned long long int lsig = (unsigned long long int)(f.val); \
     
    562566                        } else { \
    563567                                fmt2.flags.pad0 = fmt2.flags.nobsdp = true;     \
    564                                 if ( f.base == 'b' ) { \
    565                                         if ( f.wd > 64 ) fmt.wd = f.wd - 64; \
    566                                         fmt2.wd = 64; \
     568                                if ( f.base == 'b' | f.base == 'B' ) { \
     569                                        if ( fmt.flags.pc && fmt.pc > 64 ) fmt.pc -= 64; else { fmt.flags.pc = false; fmt.pc = 0; } \
     570                                        if ( fmt.flags.left ) { \
     571                                                fmt.flags.left = false; \
     572                                                fmt.wd = 0; \
     573                                                /* printf( "L %llo %llo %llo %d %d '%c' %x\n", msig, lsig, fmt.val, fmt.wd, fmt.pc, fmt.base, fmt.all ); */ \
     574                                                fmt2.flags.left = true; \
     575                                                int msigd = high1( msig ); \
     576                                                fmt2.wd = f.wd - (fmt.pc > msigd ? fmt.pc : msigd); \
     577                                                if ( ! fmt.flags.nobsdp ) fmt2.wd -= 2; /* compensate for 0b base specifier */ \
     578                                                if ( (int)fmt2.wd < 64 ) fmt2.wd = 64; /* cast deals with negative value */ \
     579                                                fmt2.flags.pc = true; fmt2.pc = 64; \
     580                                        } else { \
     581                                                if ( fmt.wd > 64 ) fmt.wd -= 64; \
     582                                                else fmt.wd = 1; \
     583                                                /* printf( "R %llo %llo %llo %d %d '%c' %x\n", msig, lsig, fmt.val, fmt.wd, fmt.pc, fmt.base, fmt.all ); */ \
     584                                                fmt2.wd = 64; \
     585                                        } /* if */ \
     586                                        /* printf( "C %llo %d %d '%c' %x\n", fmt2.val, fmt2.wd, fmt2.pc, fmt2.base, fmt2.all ); */ \
    567587                                        (ostype &)(os | fmt | "" | fmt2); \
    568588                                } else if ( f.base == 'o' ) { \
     589                                        if ( fmt.flags.pc && fmt.pc > 22 ) fmt.pc -= 22; else { fmt.flags.pc = false; fmt.pc = 0; } \
    569590                                        fmt.val = (unsigned long long int)fmt.val >> 2; \
    570                                         if ( f.wd > 21 ) fmt.wd = f.wd - 21; \
    571                                         fmt2.wd = 1; \
    572                                         fmt2.val = ((msig & 0x3) << 1) + 1; \
    573                                         (ostype &)(os | fmt | "" | fmt2); \
    574                                         sepOff( os ); \
    575                                         fmt2.wd = 21; \
    576                                         fmt2.val = lsig & 0x7fffffffffffffff; \
     591                                        fmt2.val = ((msig & 0x3) << 1) + ((lsig & 0x8000000000000000U) != 0); \
     592                                        if ( fmt.flags.left ) { \
     593                                                fmt.flags.left = false; \
     594                                                fmt.wd = 0; \
     595                                                /* printf( "L %llo %llo %llo %d %d '%c' %x %llo %d %d '%c' %x\n", msig, lsig, fmt.val, fmt.wd, fmt.pc, fmt.base, fmt.all, fmt2.val, fmt2.wd, fmt2.pc, fmt2.base, fmt2.all ); */ \
     596                                                (ostype &)(os | fmt | "" | fmt2); \
     597                                                sepOff( os ); \
     598                                                fmt2.flags.left = true; \
     599                                                int msigd = ceiling_div( high1( fmt.val ), 3 ); \
     600                                                fmt2.wd = f.wd - (fmt.pc > msigd ? fmt.pc : msigd); \
     601                                                if ( ! fmt.flags.nobsdp ) fmt2.wd -= 1; /* compensate for 0 base specifier */ \
     602                                                if ( (int)fmt2.wd < 21 ) fmt2.wd = 21; /* cast deals with negative value */ \
     603                                                fmt2.flags.pc = true; fmt2.pc = 21; \
     604                                        } else { \
     605                                                if ( fmt.wd > 22 ) fmt.wd -= 22; \
     606                                                else fmt.wd = 1; \
     607                                                /* printf( "R %llo %llo %llo %d %d '%c' %x %llo %d %d '%c' %x\n", msig, lsig, fmt.val, fmt.wd, fmt.pc, fmt.base, fmt.all, fmt2.val, fmt2.wd, fmt2.pc, fmt2.base, fmt2.all ); */ \
     608                                                (ostype &)(os | fmt | "" | fmt2); \
     609                                                sepOff( os ); \
     610                                                fmt2.wd = 21; \
     611                                        } /* if */ \
     612                                        fmt2.val = lsig & 0x7fffffffffffffffU; \
     613                                        /* printf( "\nC %llo %d %d '%c' %x\n", fmt2.val, fmt2.wd, fmt2.pc, fmt2.base, fmt2.all ); */ \
    577614                                        (ostype &)(os | fmt2); \
    578                                 } else { \
    579                                         if ( f.flags.left ) { \
    580                                                 if ( f.wd > 16 ) fmt2.wd = f.wd - 16;   \
    581                                                 fmt.wd = 16;                                                    \
     615                                } else { /* f.base == 'x'  | f.base == 'X' */ \
     616                                        if ( fmt.flags.pc && fmt.pc > 16 ) fmt.pc -= 16; else { fmt.flags.pc = false; fmt.pc = 0; } \
     617                                        if ( fmt.flags.left ) { \
     618                                                fmt.flags.left = false; \
     619                                                fmt.wd = 0; \
     620                                                /* printf( "L %llo %llo %llo %d %d '%c' %x\n", msig, lsig, fmt.val, fmt.wd, fmt.pc, fmt.base, fmt.all ); */ \
     621                                                fmt2.flags.left = true; \
     622                                                int msigd = high1( msig ); \
     623                                                fmt2.wd = f.wd - (fmt.pc > msigd ? fmt.pc : msigd); \
     624                                                if ( ! fmt.flags.nobsdp ) fmt2.wd -= 2; /* compensate for 0x base specifier */ \
     625                                                if ( (int)fmt2.wd < 16 ) fmt2.wd = 16; /* cast deals with negative value */ \
     626                                                fmt2.flags.pc = true; fmt2.pc = 16; \
    582627                                        } else { \
    583                                                 if ( f.wd > 16 ) fmt.wd = f.wd - 16;    \
    584                                                 fmt2.wd = 16;                                                   \
     628                                                if ( fmt.wd > 16 ) fmt.wd -= 16; \
     629                                                else fmt.wd = 1; \
     630                                                /* printf( "R %llo %llo %llo %d %d '%c' %x\n", msig, lsig, fmt.val, fmt.wd, fmt.pc, fmt.base, fmt.all ); */ \
     631                                                fmt2.wd = 16; \
    585632                                        } /* if */ \
     633                                        /* printf( "C %llo %d %d '%c' %x\n", fmt2.val, fmt2.wd, fmt2.pc, fmt2.base, fmt2.all ); */ \
    586634                                        (ostype &)(os | fmt | "" | fmt2); \
    587635                                } /* if */ \
    588636                        } /* if */ \
    589637                } else { \
     638                        if ( CODE == 'd' ) { \
     639                                if ( f.val < 0 )  { fmt( os, "-" ); sepOff( os ); f.val = -f.val; f.flags.sign = false; } \
     640                        } /* if */ \
    590641                        base10_128( os, f ); \
    591642                } /* if */ \
     
    598649IntegralFMTImpl128( unsigned int128, unsigned, 'u', "%    *ll ", "%    *.*ll " )
    599650#endif // __SIZEOF_INT128__
    600 
    601 //*********************************** floating point ***********************************
     651#endif // 0
     652
     653#if 1
     654#if defined( __SIZEOF_INT128__ )
     655// Default prefix for non-decimal prints is 0b, 0, 0x.
     656forall( dtype ostype | ostream( ostype ) )
     657static inline void base_128( ostype & os, unsigned int128 val, unsigned int128 power, _Ostream_Manip(uint64_t) & f, unsigned int maxdig, unsigned int bits, unsigned int cnt = 0 ) {
     658        int wd = 1;                                                                                     // f.wd is never 0 because 0 implies left-pad
     659        if ( val > power ) {                                                            // subdivide value into printable 64-bit values
     660                base_128( os, val / power, power, f, maxdig, bits, cnt + 1 ); // recursive
     661                f.val = val % power;
     662                if ( cnt == 1 && f.flags.left ) { wd = f.wd; f.wd = maxdig; } // copy f.wd and reset for printing middle chunk
     663                // printf( "R val:%#lx(%lu) wd:%u pc:%u base:%c neg:%d pc:%d left:%d nobsdp:%d sign:%d pad0:%d\n",
     664                //              f.val, f.val, f.wd, f.pc, f.base, f.flags.neg, f.flags.pc, f.flags.left, f.flags.nobsdp, f.flags.sign, f.flags.pad0 );
     665                (ostype &)(os | f);
     666                if ( cnt == 1 ) {
     667                        if ( f.flags.left ) { wd -= maxdig; f.wd = wd < 0 ? 1 : wd; } // update and restore f.wd for printing end chunk
     668                        sepOff( os );                                                           // no seperator between chunks
     669                } // if
     670        } else {                                                                                        // print start chunk
     671                f.val = val;
     672                // f.pc is unsigned => use wd
     673                if ( f.flags.pc && f.pc > maxdig * cnt ) { wd = f.pc - maxdig * cnt; f.pc = wd < 0 ? 0 : wd; }
     674                else { f.flags.pc = false; f.pc = 0; }
     675
     676                if ( ! f.flags.left ) {                                                 // right justify
     677                        wd = f.wd - maxdig * cnt;
     678                        f.wd = wd < 0 ? 1 : wd;
     679                        wd = maxdig;
     680                } else {                                                                                // left justify
     681                        if ( cnt != 0 ) {                                                       // value >= 2^64 ?
     682                                unsigned int dig, bs = 0;
     683                                // compute size of prefix digits and base
     684                                if ( f.base == 'd' || f.base == 'u' ) { // no base prefix
     685                                        dig = ceil( log10( f.val ) );           // use floating-point
     686                                        if ( f.base == 'd' && (f.flags.neg || f.flags.sign) ) bs = 1; // sign ?
     687                                } else {
     688                                        dig = ceiling_div( high1( f.val ), bits );
     689                                        if ( ! f.flags.nobsdp ) {                       // base prefix ?
     690                                                if ( f.base == 'o' ) {
     691                                                        // 0 prefix for octal is not added for precision with leading zero
     692                                                        if ( f.pc <= dig ) bs = 1;      // 1 character prefix
     693                                                } else bs = 2;                                  // 2 character prefix
     694                                        } // if
     695                                } // if
     696                                wd = f.wd - (f.pc > dig ? f.pc : dig) - bs; // precision > leading digits ?
     697                                if ( wd < 0 ) wd = 1;
     698                                f.wd = 1;
     699                        } // if
     700                        // all manipulators handled implicitly for value < 2^64
     701                } // if
     702                // prior checks ensure wd not negative
     703
     704                if ( f.flags.neg ) f.val = -f.val;
     705                // printf( "L val:%#lx(%lu) wd:%u pc:%u base:%c neg:%d pc:%d left:%d nobsdp:%d sign:%d pad0:%d\n",
     706                //              f.val, f.val, f.wd, f.pc, f.base, f.flags.neg, f.flags.pc, f.flags.left, f.flags.nobsdp, f.flags.sign, f.flags.pad0 );
     707                (ostype &)(os | f);
     708
     709                // remaining middle and end chunks are padded with 0s on the left
     710                if ( ! f.flags.left ) { f.flags.pad0 = true; f.flags.pc = false; } // left pad with 0s
     711                else { f.pc = maxdig; f.flags.pc = true; }              // left pad with precision
     712
     713                if ( cnt != 0 ) sepOff( os );                                   // no seperator between chunks
     714                f.wd = wd;                                                                              // reset f.wd for next chunk
     715                f.flags.sign = false;                                                   // no leading +/- sign
     716                f.flags.nobsdp = true;                                                  // no leading base prefix
     717        } // if
     718} // base_128
     719
     720#define IntegralFMTImpl128( T ) \
     721forall( dtype ostype | ostream( ostype ) ) { \
     722        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
     723                _Ostream_Manip(uint64_t) fmt; \
     724                fmt.[wd, pc, base, all] = f.[wd, pc, base, all]; \
     725                if ( f.base == 'b' | f.base == 'B' ) { \
     726                        base_128( os, f.val, (unsigned int128)1 << 64, fmt, 64, 1 ); \
     727                } else if ( f.base == 'o' ) { \
     728                        base_128( os, f.val, (unsigned int128)1 << 63, fmt, 21, 3 ); \
     729                } else if ( f.base == 'd' || f.base == 'u' ) { \
     730                        if ( f.base == 'd' && f.val < 0 ) { f.val = -f.val; fmt.flags.neg = true; } \
     731                        base_128( os, f.val, (unsigned int128)10_000_000_000_000_000_000UL, fmt, 19, 0 ); \
     732                } else { \
     733                        base_128( os, f.val, (unsigned int128)1 << 64, fmt, 16, 4 ); \
     734                } /* if */ \
     735                return os; \
     736        } /* ?|? */ \
     737        void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); ends( os ); } \
     738} // distribution
     739
     740IntegralFMTImpl128( int128 )
     741IntegralFMTImpl128( unsigned int128 )
     742#endif // __SIZEOF_INT128__
     743#endif // 0
     744
     745// *********************************** floating point ***********************************
    602746
    603747#define PrintWithDP2( os, format, val, ... ) \
     
    655799FloatingPointFMTImpl( long double, "%    *L ", "%    *.*L " )
    656800
    657 //*********************************** character ***********************************
     801// *********************************** character ***********************************
    658802
    659803forall( dtype ostype | ostream( ostype ) ) {
     
    688832} // distribution
    689833
    690 //*********************************** C string ***********************************
     834// *********************************** C string ***********************************
    691835
    692836forall( dtype ostype | ostream( ostype ) ) {
     
    735879
    736880
    737 //*********************************** istream ***********************************
     881// *********************************** istream ***********************************
    738882
    739883
     
    812956        } // ?|?
    813957
     958#if defined( __SIZEOF_INT128__ )
     959        istype & ?|?( istype & is, int128 & i128 ) {
     960                return (istype &)(is | (unsigned int128 &)i128);
     961        } // ?|?
     962
     963        istype & ?|?( istype & is, unsigned int128 & ui128 ) {
     964                char s[40];
     965                bool sign = false;
     966
     967                if ( fmt( is, " %[-]", s ) == 1 ) sign = true;  // skip whitespace, negative sign ?
     968                // If the input is too large, the value returned is undefined. If there is no input, no value is returned
     969                if ( fmt( is, "%39[0-9]%*[0-9]", s ) == 1 ) {   // take first 39 characters, ignore remaining
     970                        ui128 = 0;
     971                        for ( unsigned int i = 0; s[i] != '\0'; i += 1 ) {
     972                                ui128 = ui128 * 10 + s[i] - '0';
     973                        } // for
     974                        if ( sign ) ui128 = -ui128;
     975                } else if ( sign ) ungetc( is, '-' );                   // return minus when no digits
     976                return is;
     977        } // ?|?
     978#endif // __SIZEOF_INT128__
    814979
    815980        istype & ?|?( istype & is, float & f ) {
     
    8811046} // distribution
    8821047
    883 //*********************************** manipulators ***********************************
     1048// *********************************** manipulators ***********************************
    8841049
    8851050forall( dtype istype | istream( istype ) )
Note: See TracChangeset for help on using the changeset viewer.