Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/iostream.cfa

    r1488f94 re59e663  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat May  2 18:30:25 2020
    13 // Update Count     : 1017
     12// Last Modified On : Thu Feb 20 15:53:23 2020
     13// Update Count     : 829
    1414//
    1515
     
    2929#include <complex.h>                                                                    // creal, cimag
    3030} // extern "C"
    31 
    32 #include <bitmanip.hfa>                                                                 // fms
    3331
    3432
     
    461459\
    462460                if ( f.base == 'b' || f.base == 'B' ) {                 /* bespoke binary format */ \
    463                         int bits = high1( f.val );                                      /* position of most significant bit */ \
    464                         if ( bits == 0 ) bits = 1;                                      /* 0 value => force one bit to print */ \
    465                         int spaces; \
     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 ); */ \
    466468                        if ( ! f.flags.left ) {                                         /* right justified ? */ \
    467469                                /* Note, base prefix then zero padding or spacing then prefix. */ \
    468                                 if ( f.flags.pc ) { \
    469                                         spaces = f.wd - f.pc; \
    470                                         if ( ! f.flags.nobsdp ) { spaces -= 2; } /* base prefix takes space */ \
     470                                if ( f.flags.pad0 || f.flags.pc ) { \
     471                                        if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \
     472                                        if ( f.flags.pc ) spaces = f.pc - bits; \
     473                                        if ( spaces > 0 ) fmt( os, "%0*d", spaces, 0 ); /* zero pad */ \
     474                                } else { \
    471475                                        if ( spaces > 0 ) fmt( os, "%*s", spaces, " " ); /* space pad */ \
    472476                                        if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \
    473                                         spaces = f.pc - bits; \
    474                                         if ( spaces > 0 ) fmt( os, "%0*d", spaces, 0 ); /* zero pad */ \
    475                                 } else { \
    476                                         spaces = f.wd - bits; \
    477                                         if ( ! f.flags.nobsdp ) { spaces -= 2; } /* base prefix takes space */ \
    478                                         if ( f.flags.pad0 ) { \
    479                                                 if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \
    480                                                 if ( spaces > 0 ) fmt( os, "%0*d", spaces, 0 ); /* zero pad */ \
    481                                         } else { \
    482                                                 if ( spaces > 0 ) fmt( os, "%*s", spaces, " " ); /* space pad */ \
    483                                                 if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \
    484                                         } /* if */ \
    485477                                } /* if */ \
    486                         } else { \
    487                                 if ( ! f.flags.nobsdp ) fmt( os, "0%c", f.base ); \
    488                                 if ( f.flags.pc ) { \
    489                                         spaces = f.pc - bits; \
    490                                         if ( spaces > 0 ) fmt( os, "%0*d", spaces, 0 ); /* zero pad */ \
    491                                         spaces = f.wd - f.pc; \
    492                                 } else { /* pad0 flag ignored with left flag */ \
    493                                         spaces = f.wd - bits; \
    494                                 } /* if */ \
    495                                 if ( ! f.flags.nobsdp ) { spaces -= 2; } /* base prefix takes space */ \
     478                        } else if ( ! f.flags.nobsdp ) { \
     479                                fmt( os, "0%c", f.base ); \
    496480                        } /* if */ \
    497                         int shift = floor( bits - 1, 4 ); \
     481                        int shift = (bits - 1) / 4 * 4; /* floor( bits - 1, 4 ) */ \
    498482                        typeof( f.val ) temp = f.val; \
    499483                        fmt( os, "%s", shortbin[(temp >> shift) & 0xf] ); \
     
    550534#define IntegralFMTImpl128( T, SIGNED, CODE, IFMTNP, IFMTP ) \
    551535forall( dtype ostype | ostream( ostype ) ) \
    552 static void base10_128( ostype & os, _Ostream_Manip(T) f ) { \
    553         if ( f.val > UINT64_MAX ) { \
    554                 unsigned long long int lsig = f.val % P10_UINT64; \
    555                 f.val /= P10_UINT64; /* msig */ \
    556                 base10_128( os, f ); /* recursion */ \
    557                 _Ostream_Manip(unsigned long long int) fmt @= { lsig, 0, 19, 'u', { .all : 0 } }; \
    558                 fmt.flags.nobsdp = true; \
    559                 /* printf( "fmt1 %c %lld %d\n", fmt.base, fmt.val, fmt.all ); */ \
     536static 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 );   \
    560543                sepOff( os ); \
    561                 (ostype &)(os | fmt); \
     544                (ostype &)(os | fmt2); \
    562545        } else { \
    563                 /* printf( "fmt2 %c %lld %d\n", f.base, (unsigned long long int)f.val, f.all ); */ \
    564                 _Ostream_Manip(SIGNED long long int) fmt @= { (SIGNED long long int)f.val, f.wd, f.pc, f.base, { .all : f.all } }; \
     546                printf( "fmt %c %lld %d\n", fmt.base, fmt.val, fmt.all ); \
    565547                (ostype &)(os | fmt); \
    566548        } /* if */ \
    567 } /* base10_128 */ \
     549} /* base10_128 */                                                \
    568550forall( dtype ostype | ostream( ostype ) ) { \
    569551        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
    570552                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); \
    571553\
    572                 if ( f.base == 'b' | f.base == 'B' | f.base == 'o' | f.base == 'x' | f.base == 'X' ) { \
     554                if ( f.base == 'b' | f.base == 'o' | f.base == 'x' | f.base == 'X' ) { \
    573555                        unsigned long long int msig = (unsigned long long int)(f.val >> 64); \
    574556                        unsigned long long int lsig = (unsigned long long int)(f.val); \
     
    580562                        } else { \
    581563                                fmt2.flags.pad0 = fmt2.flags.nobsdp = true;     \
    582                                 if ( f.base == 'b' | f.base == 'B' ) { \
    583                                         if ( fmt.flags.pc && fmt.pc > 64 ) fmt.pc -= 64; else { fmt.flags.pc = false; fmt.pc = 0; } \
    584                                         if ( fmt.flags.left ) { \
    585                                                 fmt.flags.left = false; \
    586                                                 fmt.wd = 0; \
    587                                                 /* printf( "L %llo %llo %llo %d %d '%c' %x\n", msig, lsig, fmt.val, fmt.wd, fmt.pc, fmt.base, fmt.all ); */ \
    588                                                 fmt2.flags.left = true; \
    589                                                 int msigd = high1( msig ); \
    590                                                 fmt2.wd = f.wd - (fmt.pc > msigd ? fmt.pc : msigd); \
    591                                                 if ( ! fmt.flags.nobsdp ) fmt2.wd -= 2; /* compensate for 0b base specifier */ \
    592                                                 if ( (int)fmt2.wd < 64 ) fmt2.wd = 64; /* cast deals with negative value */ \
    593                                                 fmt2.flags.pc = true; fmt2.pc = 64; \
    594                                         } else { \
    595                                                 if ( fmt.wd > 64 ) fmt.wd -= 64; \
    596                                                 else fmt.wd = 1; \
    597                                                 /* printf( "R %llo %llo %llo %d %d '%c' %x\n", msig, lsig, fmt.val, fmt.wd, fmt.pc, fmt.base, fmt.all ); */ \
    598                                                 fmt2.wd = 64; \
    599                                         } /* if */ \
    600                                         /* printf( "C %llo %d %d '%c' %x\n", fmt2.val, fmt2.wd, fmt2.pc, fmt2.base, fmt2.all ); */ \
     564                                if ( f.base == 'b' ) { \
     565                                        if ( f.wd > 64 ) fmt.wd = f.wd - 64; \
     566                                        fmt2.wd = 64; \
    601567                                        (ostype &)(os | fmt | "" | fmt2); \
    602568                                } else if ( f.base == 'o' ) { \
    603                                         if ( fmt.flags.pc && fmt.pc > 22 ) fmt.pc -= 22; else { fmt.flags.pc = false; fmt.pc = 0; } \
    604569                                        fmt.val = (unsigned long long int)fmt.val >> 2; \
    605                                         fmt2.val = ((msig & 0x3) << 1) + ((lsig & 0x8000000000000000U) != 0); \
    606                                         if ( fmt.flags.left ) { \
    607                                                 fmt.flags.left = false; \
    608                                                 fmt.wd = 0; \
    609                                                 /* 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 ); */ \
    610                                                 (ostype &)(os | fmt | "" | fmt2); \
    611                                                 sepOff( os ); \
    612                                                 fmt2.flags.left = true; \
    613                                                 int msigd = ceiling( high1( fmt.val ), 3 ); \
    614                                                 fmt2.wd = f.wd - (fmt.pc > msigd ? fmt.pc : msigd); \
    615                                                 if ( ! fmt.flags.nobsdp ) fmt2.wd -= 1; /* compensate for 0 base specifier */ \
    616                                                 if ( (int)fmt2.wd < 21 ) fmt2.wd = 21; /* cast deals with negative value */ \
    617                                                 fmt2.flags.pc = true; fmt2.pc = 21; \
     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; \
     577                                        (ostype &)(os | fmt2); \
     578                                } else { \
     579                                        if ( f.flags.left ) { \
     580                                                if ( f.wd > 16 ) fmt2.wd = f.wd - 16;   \
     581                                                fmt.wd = 16;                                                    \
    618582                                        } else { \
    619                                                 if ( fmt.wd > 22 ) fmt.wd -= 22; \
    620                                                 else fmt.wd = 1; \
    621                                                 /* 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 ); */ \
    622                                                 (ostype &)(os | fmt | "" | fmt2); \
    623                                                 sepOff( os ); \
    624                                                 fmt2.wd = 21; \
     583                                                if ( f.wd > 16 ) fmt.wd = f.wd - 16;    \
     584                                                fmt2.wd = 16;                                                   \
    625585                                        } /* if */ \
    626                                         fmt2.val = lsig & 0x7fffffffffffffffU; \
    627                                         /* printf( "\nC %llo %d %d '%c' %x\n", fmt2.val, fmt2.wd, fmt2.pc, fmt2.base, fmt2.all ); */ \
    628                                         (ostype &)(os | fmt2); \
    629                                 } else { /* f.base == 'x'  | f.base == 'X' */ \
    630                                         if ( fmt.flags.pc && fmt.pc > 16 ) fmt.pc -= 16; else { fmt.flags.pc = false; fmt.pc = 0; } \
    631                                         if ( fmt.flags.left ) { \
    632                                                 fmt.flags.left = false; \
    633                                                 fmt.wd = 0; \
    634                                                 /* printf( "L %llo %llo %llo %d %d '%c' %x\n", msig, lsig, fmt.val, fmt.wd, fmt.pc, fmt.base, fmt.all ); */ \
    635                                                 fmt2.flags.left = true; \
    636                                                 int msigd = high1( msig ); \
    637                                                 fmt2.wd = f.wd - (fmt.pc > msigd ? fmt.pc : msigd); \
    638                                                 if ( ! fmt.flags.nobsdp ) fmt2.wd -= 2; /* compensate for 0x base specifier */ \
    639                                                 if ( (int)fmt2.wd < 16 ) fmt2.wd = 16; /* cast deals with negative value */ \
    640                                                 fmt2.flags.pc = true; fmt2.pc = 16; \
    641                                         } else { \
    642                                                 if ( fmt.wd > 16 ) fmt.wd -= 16; \
    643                                                 else fmt.wd = 1; \
    644                                                 /* printf( "R %llo %llo %llo %d %d '%c' %x\n", msig, lsig, fmt.val, fmt.wd, fmt.pc, fmt.base, fmt.all ); */ \
    645                                                 fmt2.wd = 16; \
    646                                         } /* if */ \
    647                                         /* printf( "C %llo %d %d '%c' %x\n", fmt2.val, fmt2.wd, fmt2.pc, fmt2.base, fmt2.all ); */ \
    648586                                        (ostype &)(os | fmt | "" | fmt2); \
    649587                                } /* if */ \
    650588                        } /* if */ \
    651589                } else { \
    652                         if ( CODE == 'd' ) { \
    653                                 if ( f.val < 0 )  { fmt( os, "-" ); sepOff( os ); f.val = -f.val; f.flags.sign = false; } \
    654                         } /* if */ \
    655590                        base10_128( os, f ); \
    656591                } /* if */ \
Note: See TracChangeset for help on using the changeset viewer.