Alexandre Lision | d204ea5 | 2013-10-15 10:16:25 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1996 by Jutta Degener and Carsten Bormann, Technische |
| 3 | * Universitaet Berlin. See the accompanying file "COPYRIGHT" for |
| 4 | * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE. |
| 5 | */ |
| 6 | |
| 7 | /*$Header*/ |
| 8 | |
| 9 | /* Generate code to pack a bit array from a name:#bits description */ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include "taste.h" |
| 13 | #include "proto.h" |
| 14 | #include <limits.h> |
| 15 | |
| 16 | /* This module goes back to one Jeff Chilton used for his implementation |
| 17 | * of the #49 WAV GSM format. (In his original patch 8, it replaced |
| 18 | * bitter.c.) |
| 19 | * |
| 20 | * In Microsoft's WAV #49 version of the GSM format, two 32 1/2 |
| 21 | * byte GSM frames are packed together to make one WAV frame, and |
| 22 | * the GSM parameters are packed into bytes right-to-left rather |
| 23 | * than left-to-right. |
| 24 | * |
| 25 | * That is, where toast's GSM format writes |
| 26 | * |
| 27 | * aaaaaabb bbbbcccc cdddddee ... |
| 28 | * ___1____ ___2____ ___3____ |
| 29 | * |
| 30 | * for parameters a (6 bits), b (6 bits), c (5 bits), d (5 bits), e .. |
| 31 | * the WAV format has |
| 32 | * |
| 33 | * bbaaaaaa ccccbbbb eedddddc ... |
| 34 | * ___1____ ___2____ ___3____ |
| 35 | * |
| 36 | * (This format looks a lot prettier if one pictures octets coming |
| 37 | * in through a fifo queue from the left, rather than waiting in the |
| 38 | * right-hand remainder of a C array.) |
| 39 | */ |
| 40 | |
| 41 | #define WORD_BITS 16 /* sizeof(uword) * CHAR_BIT on the |
| 42 | * target architecture---if this isn't 16, |
| 43 | * you're in trouble with this library anyway. |
| 44 | */ |
| 45 | |
| 46 | #define CHAR_BITS 8 /* CHAR_BIT on the target architecture--- |
| 47 | * if this isn't 8, you're in *deep* trouble. |
| 48 | */ |
| 49 | |
| 50 | void write_code P2((s_spex, n_spex), struct spex * s_spex, int n_spex) |
| 51 | { |
| 52 | struct spex * sp = s_spex; |
| 53 | int n_in = 0; |
| 54 | |
| 55 | printf("uword sr = 0;\n"); |
| 56 | |
| 57 | for (; n_spex > 0; n_spex--, sp++) { |
| 58 | |
| 59 | /* insert old |
| 60 | * new var value unused |
| 61 | * here |
| 62 | * |
| 63 | * [____________xxxxxx**********] |
| 64 | * |
| 65 | * <----- n_in ------> |
| 66 | */ |
| 67 | printf("sr = sr >> %d | %s << %d;\n", |
| 68 | sp->varsize, |
| 69 | sp->var, |
| 70 | WORD_BITS - sp->varsize); |
| 71 | |
| 72 | n_in += sp->varsize; |
| 73 | |
| 74 | while (n_in >= CHAR_BIT) { |
| 75 | printf("*c++ = sr >> %d;\n", |
| 76 | WORD_BITS - n_in); |
| 77 | n_in -= CHAR_BIT; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | while (n_in >= CHAR_BIT) { |
| 82 | printf("*c++ = sr >> %d;\n", WORD_BITS - n_in); |
| 83 | n_in -= CHAR_BIT; |
| 84 | } |
| 85 | |
| 86 | if (n_in > 0) { |
| 87 | fprintf(stderr, "warning: %d bits left over\n", n_in); |
| 88 | } |
| 89 | } |