Annotation of lucent/sys/src/libgnot/mktab.c, revision 1.1

1.1     ! root        1: #include       <u.h>
        !             2: #include       <libc.h>
        !             3: 
        !             4: int    table(int n);
        !             5: int    in, lin;
        !             6: int    ou, lou;
        !             7: int    b;
        !             8: int    lendian;
        !             9: 
        !            10: main(int argc, char *argv[])
        !            11: {
        !            12:        int i, osiz, osizb;
        !            13:        ulong v;
        !            14: 
        !            15:        ARGBEGIN{
        !            16:        case 'l':
        !            17:                lendian = 1;
        !            18:                break;
        !            19:        default:
        !            20:                fprint(2, "Usage: %s [-l] in-ldepth out-ldepth\n", argv0);
        !            21:                exits("usage");
        !            22:        }ARGEND
        !            23:        if(argc != 2) {
        !            24:                fprint(2, "Usage: %s [-l] in-ldepth out-ldepth\n", argv0);
        !            25:                exits("usage");
        !            26:        }
        !            27:        lin = atol(argv[0]);
        !            28:        lou = atol(argv[1]);
        !            29:        if(in < 0 || in > 5 || ou < 0 || ou > 5) {
        !            30:                fprint(2, "ldepths must be in range [0,5]\n");
        !            31:                exits("ldepth range");
        !            32:        }
        !            33:        in = 1<<lin;
        !            34:        ou = 1<<lou;
        !            35:        if(ou > in){
        !            36:                osizb = 0;
        !            37:                if(ou/in <= 4) {
        !            38:                        b = 8;
        !            39:                        osiz = (2*ou)/in;
        !            40:                } else {
        !            41:                        b = (32*in)/ou;
        !            42:                        osiz = 8;
        !            43:                }
        !            44:        } else {
        !            45:                if(in/ou > 8) {
        !            46:                        fprint(2, "too big a contraction table\n");
        !            47:                        exits("toobig");
        !            48:                }
        !            49:                if(in == ou) {
        !            50:                        fprint(2, "identity\n");
        !            51:                        exits("identity");
        !            52:                }
        !            53:                b = 8;
        !            54:                osiz = lendian? 0 : 1;
        !            55:                osizb = 8 >> (lin-lou);
        !            56:        }
        !            57:        if(lendian)
        !            58:                print("static ulong ");
        !            59:        else if(osiz <= 2)
        !            60:                print("static uchar ");
        !            61:        else if(osiz <= 4)
        !            62:                print("static ushort ");
        !            63:        else
        !            64:                print("static ulong ");
        !            65:        print("tab%d%d[%d] =\n{\n", lin, lou, (1<<b));
        !            66:        for(i=0; i<(1<<b); i++) {
        !            67:                v = table(i);
        !            68:                if(lendian){
        !            69:                        v <<= 32 - 4*osiz - osizb;
        !            70:                        print(" 0x%.8ux,", v);
        !            71:                } else
        !            72:                        print(" 0x%.*ux,", osiz, v);
        !            73:                if(lendian || osiz==8) {
        !            74:                        if(i%4 == 3)
        !            75:                                print("\n");
        !            76:                } else {
        !            77:                        if(i%8 == 7)
        !            78:                                print("\n");
        !            79:                }
        !            80:        }
        !            81:        print("};\n");
        !            82:        exits(0);
        !            83: }
        !            84: 
        !            85: /*
        !            86:  * Byte n of size b bits contains pixels with in bits/pixel.
        !            87:  * Convert each pixel to a pixel with ou bits/pixel,
        !            88:  * and assemble into an int.
        !            89:  * When expanding, a 0 pixel goes to 0, an all 1's pixel
        !            90:  * goes to all 1's, and the other input pixels equally divide
        !            91:  * the output pixel maximum.  (E.g., in=2, ou=8: 0->0, 1->255/3,
        !            92:  * 2-> 255/3 * 2, 3-> 255).  You get this effect by just
        !            93:  * duplicating the input pixel as many times as necessary.
        !            94:  * This works even if lendian is true.
        !            95:  */
        !            96: int
        !            97: table(int n)
        !            98: {
        !            99:        int i, j, x, t;
        !           100: 
        !           101:        x = 0;
        !           102:        for(i=0; i<b/in; i++) {
        !           103:                t = n >> (b-in);
        !           104:                n <<= in;
        !           105:                t &= (1<<in) - 1;
        !           106:                x <<= ou;
        !           107:                if(ou > in) {
        !           108:                        for(j=0; j<ou/in; j++) {
        !           109:                                x |= t;
        !           110:                                t <<= in;
        !           111:                        }
        !           112:                } else {
        !           113:                        x |= t >> (in-ou);
        !           114:                }
        !           115:        }
        !           116:        return x;
        !           117: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.