Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/iostream.cfa

    re59e663 r1488f94  
    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 : Sat May  2 18:30:25 2020
     13// Update Count     : 1017
    1414//
    1515
     
    2929#include <complex.h>                                                                    // creal, cimag
    3030} // extern "C"
     31
     32#include <bitmanip.hfa>                                                                 // fms
    3133
    3234
     
    459461\
    460462                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 ); */ \
     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; \
    468466                        if ( ! f.flags.left ) {                                         /* right justified ? */ \
    469467                                /* Note, base prefix then zero padding or spacing then prefix. */ \
    470                                 if ( f.flags.pad0 || f.flags.pc ) { \
     468                                if ( f.flags.pc ) { \
     469                                        spaces = f.wd - f.pc; \
     470                                        if ( ! f.flags.nobsdp ) { spaces -= 2; } /* base prefix takes space */ \
     471                                        if ( spaces > 0 ) fmt( os, "%*s", spaces, " " ); /* space pad */ \
    471472                                        if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \
    472                                         if ( f.flags.pc ) spaces = f.pc - bits; \
     473                                        spaces = f.pc - bits; \
    473474                                        if ( spaces > 0 ) fmt( os, "%0*d", spaces, 0 ); /* zero pad */ \
    474475                                } else { \
    475                                         if ( spaces > 0 ) fmt( os, "%*s", spaces, " " ); /* space pad */ \
    476                                         if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \
     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 */ \
    477485                                } /* if */ \
    478                         } else if ( ! f.flags.nobsdp ) { \
    479                                 fmt( os, "0%c", f.base ); \
     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 */ \
    480496                        } /* if */ \
    481                         int shift = (bits - 1) / 4 * 4; /* floor( bits - 1, 4 ) */ \
     497                        int shift = floor( bits - 1, 4 ); \
    482498                        typeof( f.val ) temp = f.val; \
    483499                        fmt( os, "%s", shortbin[(temp >> shift) & 0xf] ); \
     
    534550#define IntegralFMTImpl128( T, SIGNED, CODE, IFMTNP, IFMTP ) \
    535551forall( 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 );   \
     552static 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 ); */ \
    543560                sepOff( os ); \
    544                 (ostype &)(os | fmt2); \
     561                (ostype &)(os | fmt); \
    545562        } else { \
    546                 printf( "fmt %c %lld %d\n", fmt.base, fmt.val, fmt.all ); \
     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 } }; \
    547565                (ostype &)(os | fmt); \
    548566        } /* if */ \
    549 } /* base10_128 */                                                \
     567} /* base10_128 */ \
    550568forall( dtype ostype | ostream( ostype ) ) { \
    551569        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
    552570                if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); \
    553571\
    554                 if ( f.base == 'b' | f.base == 'o' | f.base == 'x' | f.base == 'X' ) { \
     572                if ( f.base == 'b' | f.base == 'B' | f.base == 'o' | f.base == 'x' | f.base == 'X' ) { \
    555573                        unsigned long long int msig = (unsigned long long int)(f.val >> 64); \
    556574                        unsigned long long int lsig = (unsigned long long int)(f.val); \
     
    562580                        } else { \
    563581                                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; \
     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 ); */ \
    567601                                        (ostype &)(os | fmt | "" | fmt2); \
    568602                                } else if ( f.base == 'o' ) { \
     603                                        if ( fmt.flags.pc && fmt.pc > 22 ) fmt.pc -= 22; else { fmt.flags.pc = false; fmt.pc = 0; } \
    569604                                        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; \
     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; \
     618                                        } 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; \
     625                                        } /* 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 ); */ \
    577628                                        (ostype &)(os | fmt2); \
    578                                 } else { \
    579                                         if ( f.flags.left ) { \
    580                                                 if ( f.wd > 16 ) fmt2.wd = f.wd - 16;   \
    581                                                 fmt.wd = 16;                                                    \
     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; \
    582641                                        } else { \
    583                                                 if ( f.wd > 16 ) fmt.wd = f.wd - 16;    \
    584                                                 fmt2.wd = 16;                                                   \
     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; \
    585646                                        } /* if */ \
     647                                        /* printf( "C %llo %d %d '%c' %x\n", fmt2.val, fmt2.wd, fmt2.pc, fmt2.base, fmt2.all ); */ \
    586648                                        (ostype &)(os | fmt | "" | fmt2); \
    587649                                } /* if */ \
    588650                        } /* if */ \
    589651                } else { \
     652                        if ( CODE == 'd' ) { \
     653                                if ( f.val < 0 )  { fmt( os, "-" ); sepOff( os ); f.val = -f.val; f.flags.sign = false; } \
     654                        } /* if */ \
    590655                        base10_128( os, f ); \
    591656                } /* if */ \
Note: See TracChangeset for help on using the changeset viewer.