home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume43 / tclmidi / part06 < prev    next >
Internet Message Format  |  1994-07-21  |  62KB

  1. From: durian@boogie.com (Mike Durian)
  2. Newsgroups: comp.sources.misc
  3. Subject: v43i114:  tclmidi - A language for manipulating MIDI files, v2.0, Part06/14
  4. Date: 21 Jul 1994 19:26:54 -0500
  5. Organization: Sterling Software
  6. Sender: kent@sparky.sterling.com
  7. Approved: kent@sparky.sterling.com
  8. Message-ID: <30n3oe$74r@sparky.sterling.com>
  9. X-Md4-Signature: 0f4176b9fb1b820fc1cf4157df28ed05
  10.  
  11. Submitted-by: durian@boogie.com (Mike Durian)
  12. Posting-number: Volume 43, Issue 114
  13. Archive-name: tclmidi/part06
  14. Environment: POSIX, (BSDI, NetBSD, LINUX, SVR4 for optional driver), C++, TCL
  15. Supersedes: tclm: Volume 37, Issue 43-47
  16.  
  17. #! /bin/sh
  18. # This is a shell archive.  Remove anything before this line, then feed it
  19. # into a shell via "sh file" or similar.  To overwrite existing files,
  20. # type "sh file -c".
  21. # Contents:  tclmidi-2.0/drivers/BSD/quad.c
  22. #   tclmidi-2.0/drivers/LINUX/quad.c tclmidi-2.0/drivers/SVR4/quad.c
  23. #   tclmidi-2.0/events/MetaInstName.C tclmidi-2.0/events/MetaKey.C
  24. # Wrapped by kent@sparky on Thu Jul 21 19:05:15 1994
  25. PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin:$PATH ; export PATH
  26. echo If this archive is complete, you will see the following message:
  27. echo '          "shar: End of archive 6 (of 14)."'
  28. if test -f 'tclmidi-2.0/drivers/BSD/quad.c' -a "${1}" != "-c" ; then 
  29.   echo shar: Will not clobber existing file \"'tclmidi-2.0/drivers/BSD/quad.c'\"
  30. else
  31.   echo shar: Extracting \"'tclmidi-2.0/drivers/BSD/quad.c'\" \(15157 characters\)
  32.   sed "s/^X//" >'tclmidi-2.0/drivers/BSD/quad.c' <<'END_OF_FILE'
  33. X/*
  34. X * I need long long for midi timing accuracy. - mbd
  35. X */
  36. X/*-
  37. X * Copyright (c) 1992 The Regents of the University of California.
  38. X * All rights reserved.
  39. X *
  40. X * This software was developed by the Computer Systems Engineering group
  41. X * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
  42. X * contributed to Berkeley.
  43. X *
  44. X * Redistribution and use in source and binary forms, with or without
  45. X * modification, are permitted provided that the following conditions
  46. X * are met:
  47. X * 1. Redistributions of source code must retain the above copyright
  48. X *    notice, this list of conditions and the following disclaimer.
  49. X * 2. Redistributions in binary form must reproduce the above copyright
  50. X *    notice, this list of conditions and the following disclaimer in the
  51. X *    documentation and/or other materials provided with the distribution.
  52. X * 3. All advertising materials mentioning features or use of this software
  53. X *    must display the following acknowledgement:
  54. X *    This product includes software developed by the University of
  55. X *    California, Berkeley and its contributors.
  56. X * 4. Neither the name of the University nor the names of its contributors
  57. X *    may be used to endorse or promote products derived from this software
  58. X *    without specific prior written permission.
  59. X *
  60. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  61. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  62. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  63. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  64. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  65. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  66. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  67. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  68. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  69. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  70. X * SUCH DAMAGE.
  71. X */
  72. X
  73. X#if defined(LIBC_SCCS) && !defined(lint)
  74. Xstatic char sccsid[] = "@(#)adddi3.c    5.5 (Berkeley) 6/25/92";
  75. X#endif /* LIBC_SCCS and not lint */
  76. X
  77. X#include "quad.h"
  78. X
  79. X/*
  80. X * Add two quads.  This is trivial since a one-bit carry from a single
  81. X * u_long addition x+y occurs if and only if the sum x+y is less than
  82. X * either x or y (the choice to compare with x or y is arbitrary).
  83. X */
  84. Xquad_t
  85. X__adddi3(a, b)
  86. X    quad_t a, b;
  87. X{
  88. X    union uu aa, bb, sum;
  89. X
  90. X    aa.q = a;
  91. X    bb.q = b;
  92. X    sum.ul[L] = aa.ul[L] + bb.ul[L];
  93. X    sum.ul[H] = aa.ul[H] + bb.ul[H] + (sum.ul[L] < bb.ul[L]);
  94. X    return (sum.q);
  95. X}
  96. X
  97. X/*
  98. X * Divide two signed quads.
  99. X * ??? if -1/2 should produce -1 on this machine, this code is wrong
  100. X */
  101. Xquad_t
  102. X__divdi3(a, b)
  103. X    quad_t a, b;
  104. X{
  105. X    u_quad_t ua, ub, uq;
  106. X    int neg;
  107. X
  108. X    if (a < 0)
  109. X        ua = -(u_quad_t)a, neg = 1;
  110. X    else
  111. X        ua = a, neg = 0;
  112. X    if (b < 0)
  113. X        ub = -(u_quad_t)b, neg ^= 1;
  114. X    else
  115. X        ub = b;
  116. X    uq = __qdivrem(ua, ub, (u_quad_t *)0);
  117. X    return (neg ? -uq : uq);
  118. X}
  119. X
  120. X
  121. X/*
  122. X * Multiply two quads.
  123. X *
  124. X * Our algorithm is based on the following.  Split incoming quad values
  125. X * u and v (where u,v >= 0) into
  126. X *
  127. X *    u = 2^n u1  *  u0    (n = number of bits in `u_long', usu. 32)
  128. X *
  129. X * and 
  130. X *
  131. X *    v = 2^n v1  *  v0
  132. X *
  133. X * Then
  134. X *
  135. X *    uv = 2^2n u1 v1  +  2^n u1 v0  +  2^n v1 u0  +  u0 v0
  136. X *       = 2^2n u1 v1  +     2^n (u1 v0 + v1 u0)   +  u0 v0
  137. X *
  138. X * Now add 2^n u1 v1 to the first term and subtract it from the middle,
  139. X * and add 2^n u0 v0 to the last term and subtract it from the middle.
  140. X * This gives:
  141. X *
  142. X *    uv = (2^2n + 2^n) (u1 v1)  +
  143. X *             (2^n)    (u1 v0 - u1 v1 + u0 v1 - u0 v0)  +
  144. X *           (2^n + 1)  (u0 v0)
  145. X *
  146. X * Factoring the middle a bit gives us:
  147. X *
  148. X *    uv = (2^2n + 2^n) (u1 v1)  +            [u1v1 = high]
  149. X *         (2^n)    (u1 - u0) (v0 - v1)  +    [(u1-u0)... = mid]
  150. X *           (2^n + 1)  (u0 v0)            [u0v0 = low]
  151. X *
  152. X * The terms (u1 v1), (u1 - u0) (v0 - v1), and (u0 v0) can all be done
  153. X * in just half the precision of the original.  (Note that either or both
  154. X * of (u1 - u0) or (v0 - v1) may be negative.)
  155. X *
  156. X * This algorithm is from Knuth vol. 2 (2nd ed), section 4.3.3, p. 278.
  157. X *
  158. X * Since C does not give us a `long * long = quad' operator, we split
  159. X * our input quads into two longs, then split the two longs into two
  160. X * shorts.  We can then calculate `short * short = long' in native
  161. X * arithmetic.
  162. X *
  163. X * Our product should, strictly speaking, be a `long quad', with 128
  164. X * bits, but we are going to discard the upper 64.  In other words,
  165. X * we are not interested in uv, but rather in (uv mod 2^2n).  This
  166. X * makes some of the terms above vanish, and we get:
  167. X *
  168. X *    (2^n)(high) + (2^n)(mid) + (2^n + 1)(low)
  169. X *
  170. X * or
  171. X *
  172. X *    (2^n)(high + mid + low) + low
  173. X *
  174. X * Furthermore, `high' and `mid' can be computed mod 2^n, as any factor
  175. X * of 2^n in either one will also vanish.  Only `low' need be computed
  176. X * mod 2^2n, and only because of the final term above.
  177. X */
  178. Xstatic quad_t __lmulq(u_long, u_long);
  179. X
  180. Xquad_t
  181. X__muldi3(a, b)
  182. X    quad_t a, b;
  183. X{
  184. X    union uu u, v, low, prod;
  185. X    register u_long high, mid, udiff, vdiff;
  186. X    register int negall, negmid;
  187. X#define    u1    u.ul[H]
  188. X#define    u0    u.ul[L]
  189. X#define    v1    v.ul[H]
  190. X#define    v0    v.ul[L]
  191. X
  192. X    /*
  193. X     * Get u and v such that u, v >= 0.  When this is finished,
  194. X     * u1, u0, v1, and v0 will be directly accessible through the
  195. X     * longword fields.
  196. X     */
  197. X    if (a >= 0)
  198. X        u.q = a, negall = 0;
  199. X    else
  200. X        u.q = -a, negall = 1;
  201. X    if (b >= 0)
  202. X        v.q = b;
  203. X    else
  204. X        v.q = -b, negall ^= 1;
  205. X
  206. X    if (u1 == 0 && v1 == 0) {
  207. X        /*
  208. X         * An (I hope) important optimization occurs when u1 and v1
  209. X         * are both 0.  This should be common since most numbers
  210. X         * are small.  Here the product is just u0*v0.
  211. X         */
  212. X        prod.q = __lmulq(u0, v0);
  213. X    } else {
  214. X        /*
  215. X         * Compute the three intermediate products, remembering
  216. X         * whether the middle term is negative.  We can discard
  217. X         * any upper bits in high and mid, so we can use native
  218. X         * u_long * u_long => u_long arithmetic.
  219. X         */
  220. X        low.q = __lmulq(u0, v0);
  221. X
  222. X        if (u1 >= u0)
  223. X            negmid = 0, udiff = u1 - u0;
  224. X        else
  225. X            negmid = 1, udiff = u0 - u1;
  226. X        if (v0 >= v1)
  227. X            vdiff = v0 - v1;
  228. X        else
  229. X            vdiff = v1 - v0, negmid ^= 1;
  230. X        mid = udiff * vdiff;
  231. X
  232. X        high = u1 * v1;
  233. X
  234. X        /*
  235. X         * Assemble the final product.
  236. X         */
  237. X        prod.ul[H] = high + (negmid ? -mid : mid) + low.ul[L] +
  238. X            low.ul[H];
  239. X        prod.ul[L] = low.ul[L];
  240. X    }
  241. X    return (negall ? -prod.q : prod.q);
  242. X#undef u1
  243. X#undef u0
  244. X#undef v1
  245. X#undef v0
  246. X}
  247. X
  248. X/*
  249. X * Multiply two 2N-bit longs to produce a 4N-bit quad, where N is half
  250. X * the number of bits in a long (whatever that is---the code below
  251. X * does not care as long as quad.h does its part of the bargain---but
  252. X * typically N==16).
  253. X *
  254. X * We use the same algorithm from Knuth, but this time the modulo refinement
  255. X * does not apply.  On the other hand, since N is half the size of a long,
  256. X * we can get away with native multiplication---none of our input terms
  257. X * exceeds (ULONG_MAX >> 1).
  258. X *
  259. X * Note that, for u_long l, the quad-precision result
  260. X *
  261. X *    l << N
  262. X *
  263. X * splits into high and low longs as HHALF(l) and LHUP(l) respectively.
  264. X */
  265. Xstatic quad_t
  266. X__lmulq(u_long u, u_long v)
  267. X{
  268. X    u_long u1, u0, v1, v0, udiff, vdiff, high, mid, low;
  269. X    u_long prodh, prodl, was;
  270. X    union uu prod;
  271. X    int neg;
  272. X
  273. X    u1 = HHALF(u);
  274. X    u0 = LHALF(u);
  275. X    v1 = HHALF(v);
  276. X    v0 = LHALF(v);
  277. X
  278. X    low = u0 * v0;
  279. X
  280. X    /* This is the same small-number optimization as before. */
  281. X    if (u1 == 0 && v1 == 0)
  282. X        return (low);
  283. X
  284. X    if (u1 >= u0)
  285. X        udiff = u1 - u0, neg = 0;
  286. X    else
  287. X        udiff = u0 - u1, neg = 1;
  288. X    if (v0 >= v1)
  289. X        vdiff = v0 - v1;
  290. X    else
  291. X        vdiff = v1 - v0, neg ^= 1;
  292. X    mid = udiff * vdiff;
  293. X
  294. X    high = u1 * v1;
  295. X
  296. X    /* prod = (high << 2N) + (high << N); */
  297. X    prodh = high + HHALF(high);
  298. X    prodl = LHUP(high);
  299. X
  300. X    /* if (neg) prod -= mid << N; else prod += mid << N; */
  301. X    if (neg) {
  302. X        was = prodl;
  303. X        prodl -= LHUP(mid);
  304. X        prodh -= HHALF(mid) + (prodl > was);
  305. X    } else {
  306. X        was = prodl;
  307. X        prodl += LHUP(mid);
  308. X        prodh += HHALF(mid) + (prodl < was);
  309. X    }
  310. X
  311. X    /* prod += low << N */
  312. X    was = prodl;
  313. X    prodl += LHUP(low);
  314. X    prodh += HHALF(low) + (prodl < was);
  315. X    /* ... + low; */
  316. X    if ((prodl += low) < low)
  317. X        prodh++;
  318. X
  319. X    /* return 4N-bit product */
  320. X    prod.ul[H] = prodh;
  321. X    prod.ul[L] = prodl;
  322. X    return (prod.q);
  323. X}
  324. X
  325. X/*
  326. X * Return remainder after dividing two signed quads.
  327. X *
  328. X * XXX
  329. X * If -1/2 should produce -1 on this machine, this code is wrong.
  330. X */
  331. Xquad_t
  332. X__moddi3(a, b)
  333. X    quad_t a, b;
  334. X{
  335. X    u_quad_t ua, ub, ur;
  336. X    int neg;
  337. X
  338. X    if (a < 0)
  339. X        ua = -(u_quad_t)a, neg = 1;
  340. X    else
  341. X        ua = a, neg = 0;
  342. X    if (b < 0)
  343. X        ub = -(u_quad_t)b, neg ^= 1;
  344. X    else
  345. X        ub = b;
  346. X    (void)__qdivrem(ua, ub, &ur);
  347. X    return (neg ? -ur : ur);
  348. X}
  349. X
  350. X/*
  351. X * Return 0, 1, or 2 as a <, =, > b respectively.
  352. X * Both a and b are considered signed---which means only the high word is
  353. X * signed.
  354. X */
  355. Xint
  356. X__cmpdi2(a, b)
  357. X    quad_t a, b;
  358. X{
  359. X    union uu aa, bb;
  360. X
  361. X    aa.q = a;
  362. X    bb.q = b;
  363. X    return (aa.sl[H] < bb.sl[H] ? 0 : aa.sl[H] > bb.sl[H] ? 2 :
  364. X        aa.ul[L] < bb.ul[L] ? 0 : aa.ul[L] > bb.ul[L] ? 2 : 1);
  365. X}
  366. X
  367. X/*
  368. X * Return -a (or, equivalently, 0 - a), in quad.  See subdi3.c.
  369. X */
  370. Xquad_t
  371. X__negdi2(a)
  372. X    quad_t a;
  373. X{
  374. X    union uu aa, res;
  375. X
  376. X    aa.q = a;
  377. X    res.ul[L] = -aa.ul[L];
  378. X    res.ul[H] = -aa.ul[H] - (res.ul[L] > 0);
  379. X    return (res.q);
  380. X}
  381. X
  382. X/*
  383. X * Multiprecision divide.  This algorithm is from Knuth vol. 2 (2nd ed),
  384. X * section 4.3.1, pp. 257--259.
  385. X */
  386. X
  387. X#define    B    (1 << HALF_BITS)    /* digit base */
  388. X
  389. X/* Combine two `digits' to make a single two-digit number. */
  390. X#define    COMBINE(a, b) (((u_long)(a) << HALF_BITS) | (b))
  391. X
  392. X/* select a type for digits in base B: use unsigned short if they fit */
  393. X#if ULONG_MAX == 0xffffffff && USHRT_MAX >= 0xffff
  394. Xtypedef unsigned short digit;
  395. X#else
  396. Xtypedef u_long digit;
  397. X#endif
  398. X
  399. X/*
  400. X * Shift p[0]..p[len] left `sh' bits, ignoring any bits that
  401. X * `fall out' the left (there never will be any such anyway).
  402. X * We may assume len >= 0.  NOTE THAT THIS WRITES len+1 DIGITS.
  403. X */
  404. Xstatic void
  405. Xshl(register digit *p, register int len, register int sh)
  406. X{
  407. X    register int i;
  408. X
  409. X    for (i = 0; i < len; i++)
  410. X        p[i] = LHALF(p[i] << sh) | (p[i + 1] >> (HALF_BITS - sh));
  411. X    p[i] = LHALF(p[i] << sh);
  412. X}
  413. X
  414. X/*
  415. X * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v.
  416. X *
  417. X * We do this in base 2-sup-HALF_BITS, so that all intermediate products
  418. X * fit within u_long.  As a consequence, the maximum length dividend and
  419. X * divisor are 4 `digits' in this base (they are shorter if they have
  420. X * leading zeros).
  421. X */
  422. Xu_quad_t
  423. X__qdivrem(uq, vq, arq)
  424. X    u_quad_t uq, vq, *arq;
  425. X{
  426. X    union uu tmp;
  427. X    digit *u, *v, *q;
  428. X    register digit v1, v2;
  429. X    u_long qhat, rhat, t;
  430. X    int m, n, d, j, i;
  431. X    digit uspace[5], vspace[5], qspace[5];
  432. X
  433. X    /*
  434. X     * Take care of special cases: divide by zero, and u < v.
  435. X     */
  436. X    if (vq == 0) {
  437. X        /* divide by zero. */
  438. X        static volatile const unsigned int zero = 0;
  439. X
  440. X        tmp.ul[H] = tmp.ul[L] = 1 / zero;
  441. X        if (arq)
  442. X            *arq = uq;
  443. X        return (tmp.q);
  444. X    }
  445. X    if (uq < vq) {
  446. X        if (arq)
  447. X            *arq = uq;
  448. X        return (0);
  449. X    }
  450. X    u = &uspace[0];
  451. X    v = &vspace[0];
  452. X    q = &qspace[0];
  453. X
  454. X    /*
  455. X     * Break dividend and divisor into digits in base B, then
  456. X     * count leading zeros to determine m and n.  When done, we
  457. X     * will have:
  458. X     *    u = (u[1]u[2]...u[m+n]) sub B
  459. X     *    v = (v[1]v[2]...v[n]) sub B
  460. X     *    v[1] != 0
  461. X     *    1 < n <= 4 (if n = 1, we use a different division algorithm)
  462. X     *    m >= 0 (otherwise u < v, which we already checked)
  463. X     *    m + n = 4
  464. X     * and thus
  465. X     *    m = 4 - n <= 2
  466. X     */
  467. X    tmp.uq = uq;
  468. X    u[0] = 0;
  469. X    u[1] = HHALF(tmp.ul[H]);
  470. X    u[2] = LHALF(tmp.ul[H]);
  471. X    u[3] = HHALF(tmp.ul[L]);
  472. X    u[4] = LHALF(tmp.ul[L]);
  473. X    tmp.uq = vq;
  474. X    v[1] = HHALF(tmp.ul[H]);
  475. X    v[2] = LHALF(tmp.ul[H]);
  476. X    v[3] = HHALF(tmp.ul[L]);
  477. X    v[4] = LHALF(tmp.ul[L]);
  478. X    for (n = 4; v[1] == 0; v++) {
  479. X        if (--n == 1) {
  480. X            u_long rbj;    /* r*B+u[j] (not root boy jim) */
  481. X            digit q1, q2, q3, q4;
  482. X
  483. X            /*
  484. X             * Change of plan, per exercise 16.
  485. X             *    r = 0;
  486. X             *    for j = 1..4:
  487. X             *        q[j] = floor((r*B + u[j]) / v),
  488. X             *        r = (r*B + u[j]) % v;
  489. X             * We unroll this completely here.
  490. X             */
  491. X            t = v[2];    /* nonzero, by definition */
  492. X            q1 = u[1] / t;
  493. X            rbj = COMBINE(u[1] % t, u[2]);
  494. X            q2 = rbj / t;
  495. X            rbj = COMBINE(rbj % t, u[3]);
  496. X            q3 = rbj / t;
  497. X            rbj = COMBINE(rbj % t, u[4]);
  498. X            q4 = rbj / t;
  499. X            if (arq)
  500. X                *arq = rbj % t;
  501. X            tmp.ul[H] = COMBINE(q1, q2);
  502. X            tmp.ul[L] = COMBINE(q3, q4);
  503. X            return (tmp.q);
  504. X        }
  505. X    }
  506. X
  507. X    /*
  508. X     * By adjusting q once we determine m, we can guarantee that
  509. X     * there is a complete four-digit quotient at &qspace[1] when
  510. X     * we finally stop.
  511. X     */
  512. X    for (m = 4 - n; u[1] == 0; u++)
  513. X        m--;
  514. X    for (i = 4 - m; --i >= 0;)
  515. X        q[i] = 0;
  516. X    q += 4 - m;
  517. X
  518. X    /*
  519. X     * Here we run Program D, translated from MIX to C and acquiring
  520. X     * a few minor changes.
  521. X     *
  522. X     * D1: choose multiplier 1 << d to ensure v[1] >= B/2.
  523. X     */
  524. X    d = 0;
  525. X    for (t = v[1]; t < B / 2; t <<= 1)
  526. X        d++;
  527. X    if (d > 0) {
  528. X        shl(&u[0], m + n, d);        /* u <<= d */
  529. X        shl(&v[1], n - 1, d);        /* v <<= d */
  530. X    }
  531. X    /*
  532. X     * D2: j = 0.
  533. X     */
  534. X    j = 0;
  535. X    v1 = v[1];    /* for D3 -- note that v[1..n] are constant */
  536. X    v2 = v[2];    /* for D3 */
  537. X    do {
  538. X        register digit uj0, uj1, uj2;
  539. X        
  540. X        /*
  541. X         * D3: Calculate qhat (\^q, in TeX notation).
  542. X         * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and
  543. X         * let rhat = (u[j]*B + u[j+1]) mod v[1].
  544. X         * While rhat < B and v[2]*qhat > rhat*B+u[j+2],
  545. X         * decrement qhat and increase rhat correspondingly.
  546. X         * Note that if rhat >= B, v[2]*qhat < rhat*B.
  547. X         */
  548. X        uj0 = u[j + 0];    /* for D3 only -- note that u[j+...] change */
  549. X        uj1 = u[j + 1];    /* for D3 only */
  550. X        uj2 = u[j + 2];    /* for D3 only */
  551. X        if (uj0 == v1) {
  552. X            qhat = B;
  553. X            rhat = uj1;
  554. X            goto qhat_too_big;
  555. X        } else {
  556. X            u_long n = COMBINE(uj0, uj1);
  557. X            qhat = n / v1;
  558. X            rhat = n % v1;
  559. X        }
  560. X        while (v2 * qhat > COMBINE(rhat, uj2)) {
  561. X    qhat_too_big:
  562. X            qhat--;
  563. X            if ((rhat += v1) >= B)
  564. X                break;
  565. X        }
  566. X        /*
  567. X         * D4: Multiply and subtract.
  568. X         * The variable `t' holds any borrows across the loop.
  569. X         * We split this up so that we do not require v[0] = 0,
  570. X         * and to eliminate a final special case.
  571. X         */
  572. X        for (t = 0, i = n; i > 0; i--) {
  573. X            t = u[i + j] - v[i] * qhat - t;
  574. X            u[i + j] = LHALF(t);
  575. X            t = (B - HHALF(t)) & (B - 1);
  576. X        }
  577. X        t = u[j] - t;
  578. X        u[j] = LHALF(t);
  579. X        /*
  580. X         * D5: test remainder.
  581. X         * There is a borrow if and only if HHALF(t) is nonzero;
  582. X         * in that (rare) case, qhat was too large (by exactly 1).
  583. X         * Fix it by adding v[1..n] to u[j..j+n].
  584. X         */
  585. X        if (HHALF(t)) {
  586. X            qhat--;
  587. X            for (t = 0, i = n; i > 0; i--) { /* D6: add back. */
  588. X                t += u[i + j] + v[i];
  589. X                u[i + j] = LHALF(t);
  590. X                t = HHALF(t);
  591. X            }
  592. X            u[j] = LHALF(u[j] + t);
  593. X        }
  594. X        q[j] = qhat;
  595. X    } while (++j <= m);        /* D7: loop on j. */
  596. X
  597. X    /*
  598. X     * If caller wants the remainder, we have to calculate it as
  599. X     * u[m..m+n] >> d (this is at most n digits and thus fits in
  600. X     * u[m+1..m+n], but we may need more source digits).
  601. X     */
  602. X    if (arq) {
  603. X        if (d) {
  604. X            for (i = m + n; i > m; --i)
  605. X                u[i] = (u[i] >> d) |
  606. X                    LHALF(u[i - 1] << (HALF_BITS - d));
  607. X            u[i] = 0;
  608. X        }
  609. X        tmp.ul[H] = COMBINE(uspace[1], uspace[2]);
  610. X        tmp.ul[L] = COMBINE(uspace[3], uspace[4]);
  611. X        *arq = tmp.q;
  612. X    }
  613. X
  614. X    tmp.ul[H] = COMBINE(qspace[1], qspace[2]);
  615. X    tmp.ul[L] = COMBINE(qspace[3], qspace[4]);
  616. X    return (tmp.q);
  617. X}
  618. X
  619. X/*
  620. X * Return 0, 1, or 2 as a <, =, > b respectively.
  621. X * Neither a nor b are considered signed.
  622. X */
  623. Xint
  624. X__ucmpdi2(a, b)
  625. X    u_quad_t a, b;
  626. X{
  627. X    union uu aa, bb;
  628. X
  629. X    aa.uq = a;
  630. X    bb.uq = b;
  631. X    return (aa.ul[H] < bb.ul[H] ? 0 : aa.ul[H] > bb.ul[H] ? 2 :
  632. X        aa.ul[L] < bb.ul[L] ? 0 : aa.ul[L] > bb.ul[L] ? 2 : 1);
  633. X}
  634. END_OF_FILE
  635.   if test 15157 -ne `wc -c <'tclmidi-2.0/drivers/BSD/quad.c'`; then
  636.     echo shar: \"'tclmidi-2.0/drivers/BSD/quad.c'\" unpacked with wrong size!
  637.   fi
  638.   # end of 'tclmidi-2.0/drivers/BSD/quad.c'
  639. fi
  640. if test -f 'tclmidi-2.0/drivers/LINUX/quad.c' -a "${1}" != "-c" ; then 
  641.   echo shar: Will not clobber existing file \"'tclmidi-2.0/drivers/LINUX/quad.c'\"
  642. else
  643.   echo shar: Extracting \"'tclmidi-2.0/drivers/LINUX/quad.c'\" \(15234 characters\)
  644.   sed "s/^X//" >'tclmidi-2.0/drivers/LINUX/quad.c' <<'END_OF_FILE'
  645. X/*
  646. X * I need long long for midi timing accuracy. - mbd
  647. X *
  648. X * Inline argument prototype declarations to suppress gcc 2 warnings - lig
  649. X */
  650. X
  651. X/*-
  652. X * Copyright (c) 1992 The Regents of the University of California.
  653. X * All rights reserved.
  654. X *
  655. X * This software was developed by the Computer Systems Engineering group
  656. X * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
  657. X * contributed to Berkeley.
  658. X *
  659. X * Redistribution and use in source and binary forms, with or without
  660. X * modification, are permitted provided that the following conditions
  661. X * are met:
  662. X * 1. Redistributions of source code must retain the above copyright
  663. X *    notice, this list of conditions and the following disclaimer.
  664. X * 2. Redistributions in binary form must reproduce the above copyright
  665. X *    notice, this list of conditions and the following disclaimer in the
  666. X *    documentation and/or other materials provided with the distribution.
  667. X * 3. All advertising materials mentioning features or use of this software
  668. X *    must display the following acknowledgement:
  669. X *    This product includes software developed by the University of
  670. X *    California, Berkeley and its contributors.
  671. X * 4. Neither the name of the University nor the names of its contributors
  672. X *    may be used to endorse or promote products derived from this software
  673. X *    without specific prior written permission.
  674. X *
  675. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  676. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  677. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  678. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  679. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  680. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  681. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  682. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  683. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  684. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  685. X * SUCH DAMAGE.
  686. X */
  687. X
  688. X#if defined(LIBC_SCCS) && !defined(lint)
  689. Xstatic char sccsid[] = "@(#)adddi3.c    5.5 (Berkeley) 6/25/92";
  690. X#endif /* LIBC_SCCS and not lint */
  691. X
  692. X#include "quad.h"
  693. X
  694. X/*
  695. X * Add two quads.  This is trivial since a one-bit carry from a single
  696. X * u_long addition x+y occurs if and only if the sum x+y is less than
  697. X * either x or y (the choice to compare with x or y is arbitrary).
  698. X */
  699. Xquad_t
  700. X__adddi3(quad_t a, quad_t b)
  701. X{
  702. X    union uu aa, bb, sum;
  703. X
  704. X    aa.q = a;
  705. X    bb.q = b;
  706. X    sum.ul[L] = aa.ul[L] + bb.ul[L];
  707. X    sum.ul[H] = aa.ul[H] + bb.ul[H] + (sum.ul[L] < bb.ul[L]);
  708. X    return (sum.q);
  709. X}
  710. X
  711. X/*
  712. X * Divide two signed quads.
  713. X * ??? if -1/2 should produce -1 on this machine, this code is wrong
  714. X */
  715. Xquad_t
  716. X__divdi3(quad_t a, quad_t b)
  717. X{
  718. X    u_quad_t ua, ub, uq;
  719. X    int neg;
  720. X
  721. X    if (a < 0)
  722. X        ua = -(u_quad_t)a, neg = 1;
  723. X    else
  724. X        ua = a, neg = 0;
  725. X    if (b < 0)
  726. X        ub = -(u_quad_t)b, neg ^= 1;
  727. X    else
  728. X        ub = b;
  729. X    uq = __qdivrem(ua, ub, (u_quad_t *)0);
  730. X    return (neg ? -uq : uq);
  731. X}
  732. X
  733. X
  734. X/*
  735. X * Multiply two quads.
  736. X *
  737. X * Our algorithm is based on the following.  Split incoming quad values
  738. X * u and v (where u,v >= 0) into
  739. X *
  740. X *    u = 2^n u1  *  u0    (n = number of bits in `u_long', usu. 32)
  741. X *
  742. X * and 
  743. X *
  744. X *    v = 2^n v1  *  v0
  745. X *
  746. X * Then
  747. X *
  748. X *    uv = 2^2n u1 v1  +  2^n u1 v0  +  2^n v1 u0  +  u0 v0
  749. X *       = 2^2n u1 v1  +     2^n (u1 v0 + v1 u0)   +  u0 v0
  750. X *
  751. X * Now add 2^n u1 v1 to the first term and subtract it from the middle,
  752. X * and add 2^n u0 v0 to the last term and subtract it from the middle.
  753. X * This gives:
  754. X *
  755. X *    uv = (2^2n + 2^n) (u1 v1)  +
  756. X *             (2^n)    (u1 v0 - u1 v1 + u0 v1 - u0 v0)  +
  757. X *           (2^n + 1)  (u0 v0)
  758. X *
  759. X * Factoring the middle a bit gives us:
  760. X *
  761. X *    uv = (2^2n + 2^n) (u1 v1)  +            [u1v1 = high]
  762. X *         (2^n)    (u1 - u0) (v0 - v1)  +    [(u1-u0)... = mid]
  763. X *           (2^n + 1)  (u0 v0)            [u0v0 = low]
  764. X *
  765. X * The terms (u1 v1), (u1 - u0) (v0 - v1), and (u0 v0) can all be done
  766. X * in just half the precision of the original.  (Note that either or both
  767. X * of (u1 - u0) or (v0 - v1) may be negative.)
  768. X *
  769. X * This algorithm is from Knuth vol. 2 (2nd ed), section 4.3.3, p. 278.
  770. X *
  771. X * Since C does not give us a `long * long = quad' operator, we split
  772. X * our input quads into two longs, then split the two longs into two
  773. X * shorts.  We can then calculate `short * short = long' in native
  774. X * arithmetic.
  775. X *
  776. X * Our product should, strictly speaking, be a `long quad', with 128
  777. X * bits, but we are going to discard the upper 64.  In other words,
  778. X * we are not interested in uv, but rather in (uv mod 2^2n).  This
  779. X * makes some of the terms above vanish, and we get:
  780. X *
  781. X *    (2^n)(high) + (2^n)(mid) + (2^n + 1)(low)
  782. X *
  783. X * or
  784. X *
  785. X *    (2^n)(high + mid + low) + low
  786. X *
  787. X * Furthermore, `high' and `mid' can be computed mod 2^n, as any factor
  788. X * of 2^n in either one will also vanish.  Only `low' need be computed
  789. X * mod 2^2n, and only because of the final term above.
  790. X */
  791. Xstatic quad_t __lmulq(u_long, u_long);
  792. X
  793. Xquad_t
  794. X__muldi3(quad_t a, quad_t b)
  795. X{
  796. X    union uu u, v, low, prod;
  797. X    register u_long high, mid, udiff, vdiff;
  798. X    register int negall, negmid;
  799. X#define    u1    u.ul[H]
  800. X#define    u0    u.ul[L]
  801. X#define    v1    v.ul[H]
  802. X#define    v0    v.ul[L]
  803. X
  804. X    /*
  805. X     * Get u and v such that u, v >= 0.  When this is finished,
  806. X     * u1, u0, v1, and v0 will be directly accessible through the
  807. X     * longword fields.
  808. X     */
  809. X    if (a >= 0)
  810. X        u.q = a, negall = 0;
  811. X    else
  812. X        u.q = -a, negall = 1;
  813. X    if (b >= 0)
  814. X        v.q = b;
  815. X    else
  816. X        v.q = -b, negall ^= 1;
  817. X
  818. X    if (u1 == 0 && v1 == 0) {
  819. X        /*
  820. X         * An (I hope) important optimization occurs when u1 and v1
  821. X         * are both 0.  This should be common since most numbers
  822. X         * are small.  Here the product is just u0*v0.
  823. X         */
  824. X        prod.q = __lmulq(u0, v0);
  825. X    } else {
  826. X        /*
  827. X         * Compute the three intermediate products, remembering
  828. X         * whether the middle term is negative.  We can discard
  829. X         * any upper bits in high and mid, so we can use native
  830. X         * u_long * u_long => u_long arithmetic.
  831. X         */
  832. X        low.q = __lmulq(u0, v0);
  833. X
  834. X        if (u1 >= u0)
  835. X            negmid = 0, udiff = u1 - u0;
  836. X        else
  837. X            negmid = 1, udiff = u0 - u1;
  838. X        if (v0 >= v1)
  839. X            vdiff = v0 - v1;
  840. X        else
  841. X            vdiff = v1 - v0, negmid ^= 1;
  842. X        mid = udiff * vdiff;
  843. X
  844. X        high = u1 * v1;
  845. X
  846. X        /*
  847. X         * Assemble the final product.
  848. X         */
  849. X        prod.ul[H] = high + (negmid ? -mid : mid) + low.ul[L] +
  850. X            low.ul[H];
  851. X        prod.ul[L] = low.ul[L];
  852. X    }
  853. X    return (negall ? -prod.q : prod.q);
  854. X#undef u1
  855. X#undef u0
  856. X#undef v1
  857. X#undef v0
  858. X}
  859. X
  860. X/*
  861. X * Multiply two 2N-bit longs to produce a 4N-bit quad, where N is half
  862. X * the number of bits in a long (whatever that is---the code below
  863. X * does not care as long as quad.h does its part of the bargain---but
  864. X * typically N==16).
  865. X *
  866. X * We use the same algorithm from Knuth, but this time the modulo refinement
  867. X * does not apply.  On the other hand, since N is half the size of a long,
  868. X * we can get away with native multiplication---none of our input terms
  869. X * exceeds (ULONG_MAX >> 1).
  870. X *
  871. X * Note that, for u_long l, the quad-precision result
  872. X *
  873. X *    l << N
  874. X *
  875. X * splits into high and low longs as HHALF(l) and LHUP(l) respectively.
  876. X */
  877. Xstatic quad_t
  878. X__lmulq(u_long u, u_long v)
  879. X{
  880. X    u_long u1, u0, v1, v0, udiff, vdiff, high, mid, low;
  881. X    u_long prodh, prodl, was;
  882. X    union uu prod;
  883. X    int neg;
  884. X
  885. X    u1 = HHALF(u);
  886. X    u0 = LHALF(u);
  887. X    v1 = HHALF(v);
  888. X    v0 = LHALF(v);
  889. X
  890. X    low = u0 * v0;
  891. X
  892. X    /* This is the same small-number optimization as before. */
  893. X    if (u1 == 0 && v1 == 0)
  894. X        return (low);
  895. X
  896. X    if (u1 >= u0)
  897. X        udiff = u1 - u0, neg = 0;
  898. X    else
  899. X        udiff = u0 - u1, neg = 1;
  900. X    if (v0 >= v1)
  901. X        vdiff = v0 - v1;
  902. X    else
  903. X        vdiff = v1 - v0, neg ^= 1;
  904. X    mid = udiff * vdiff;
  905. X
  906. X    high = u1 * v1;
  907. X
  908. X    /* prod = (high << 2N) + (high << N); */
  909. X    prodh = high + HHALF(high);
  910. X    prodl = LHUP(high);
  911. X
  912. X    /* if (neg) prod -= mid << N; else prod += mid << N; */
  913. X    if (neg) {
  914. X        was = prodl;
  915. X        prodl -= LHUP(mid);
  916. X        prodh -= HHALF(mid) + (prodl > was);
  917. X    } else {
  918. X        was = prodl;
  919. X        prodl += LHUP(mid);
  920. X        prodh += HHALF(mid) + (prodl < was);
  921. X    }
  922. X
  923. X    /* prod += low << N */
  924. X    was = prodl;
  925. X    prodl += LHUP(low);
  926. X    prodh += HHALF(low) + (prodl < was);
  927. X    /* ... + low; */
  928. X    if ((prodl += low) < low)
  929. X        prodh++;
  930. X
  931. X    /* return 4N-bit product */
  932. X    prod.ul[H] = prodh;
  933. X    prod.ul[L] = prodl;
  934. X    return (prod.q);
  935. X}
  936. X
  937. X/*
  938. X * Return remainder after dividing two signed quads.
  939. X *
  940. X * XXX
  941. X * If -1/2 should produce -1 on this machine, this code is wrong.
  942. X */
  943. Xquad_t
  944. X__moddi3(quad_t a, quad_t b)
  945. X{
  946. X    u_quad_t ua, ub, ur;
  947. X    int neg;
  948. X
  949. X    if (a < 0)
  950. X        ua = -(u_quad_t)a, neg = 1;
  951. X    else
  952. X        ua = a, neg = 0;
  953. X    if (b < 0)
  954. X        ub = -(u_quad_t)b, neg ^= 1;
  955. X    else
  956. X        ub = b;
  957. X    (void)__qdivrem(ua, ub, &ur);
  958. X    return (neg ? -ur : ur);
  959. X}
  960. X
  961. X/*
  962. X * Return 0, 1, or 2 as a <, =, > b respectively.
  963. X * Both a and b are considered signed---which means only the high word is
  964. X * signed.
  965. X */
  966. Xint
  967. X__cmpdi2(quad_t a, quad_t b)
  968. X{
  969. X    union uu aa, bb;
  970. X
  971. X    aa.q = a;
  972. X    bb.q = b;
  973. X    return (aa.sl[H] < bb.sl[H] ? 0 : aa.sl[H] > bb.sl[H] ? 2 :
  974. X        aa.ul[L] < bb.ul[L] ? 0 : aa.ul[L] > bb.ul[L] ? 2 : 1);
  975. X}
  976. X
  977. X/*
  978. X * Return -a (or, equivalently, 0 - a), in quad.  See subdi3.c.
  979. X */
  980. Xquad_t
  981. X__negdi2(quad_t a)
  982. X{
  983. X    union uu aa, res;
  984. X
  985. X    aa.q = a;
  986. X    res.ul[L] = -aa.ul[L];
  987. X    res.ul[H] = -aa.ul[H] - (res.ul[L] > 0);
  988. X    return (res.q);
  989. X}
  990. X
  991. X/*
  992. X * Multiprecision divide.  This algorithm is from Knuth vol. 2 (2nd ed),
  993. X * section 4.3.1, pp. 257--259.
  994. X */
  995. X
  996. X#define    B    (1 << HALF_BITS)    /* digit base */
  997. X
  998. X/* Combine two `digits' to make a single two-digit number. */
  999. X#define    COMBINE(a, b) (((u_long)(a) << HALF_BITS) | (b))
  1000. X
  1001. X/* select a type for digits in base B: use unsigned short if they fit */
  1002. X#if ULONG_MAX == 0xffffffff && USHRT_MAX >= 0xffff
  1003. Xtypedef unsigned short digit;
  1004. X#else
  1005. Xtypedef u_long digit;
  1006. X#endif
  1007. X
  1008. X/*
  1009. X * Shift p[0]..p[len] left `sh' bits, ignoring any bits that
  1010. X * `fall out' the left (there never will be any such anyway).
  1011. X * We may assume len >= 0.  NOTE THAT THIS WRITES len+1 DIGITS.
  1012. X */
  1013. Xstatic void
  1014. Xshl(register digit *p, register int len, register int sh)
  1015. X{
  1016. X    register int i;
  1017. X
  1018. X    for (i = 0; i < len; i++)
  1019. X        p[i] = LHALF(p[i] << sh) | (p[i + 1] >> (HALF_BITS - sh));
  1020. X    p[i] = LHALF(p[i] << sh);
  1021. X}
  1022. X
  1023. X/*
  1024. X * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v.
  1025. X *
  1026. X * We do this in base 2-sup-HALF_BITS, so that all intermediate products
  1027. X * fit within u_long.  As a consequence, the maximum length dividend and
  1028. X * divisor are 4 `digits' in this base (they are shorter if they have
  1029. X * leading zeros).
  1030. X */
  1031. Xu_quad_t
  1032. X__qdivrem(uq, vq, arq)
  1033. X    u_quad_t uq, vq, *arq;
  1034. X{
  1035. X    union uu tmp;
  1036. X    digit *u, *v, *q;
  1037. X    register digit v1, v2;
  1038. X    u_long qhat, rhat, t;
  1039. X    int m, n, d, j, i;
  1040. X    digit uspace[5], vspace[5], qspace[5];
  1041. X
  1042. X    /*
  1043. X     * Take care of special cases: divide by zero, and u < v.
  1044. X     */
  1045. X    if (vq == 0) {
  1046. X        /* divide by zero. */
  1047. X        static volatile const unsigned int zero = 0;
  1048. X
  1049. X        tmp.ul[H] = tmp.ul[L] = 1 / zero;
  1050. X        if (arq)
  1051. X            *arq = uq;
  1052. X        return (tmp.q);
  1053. X    }
  1054. X    if (uq < vq) {
  1055. X        if (arq)
  1056. X            *arq = uq;
  1057. X        return (0);
  1058. X    }
  1059. X    u = &uspace[0];
  1060. X    v = &vspace[0];
  1061. X    q = &qspace[0];
  1062. X
  1063. X    /*
  1064. X     * Break dividend and divisor into digits in base B, then
  1065. X     * count leading zeros to determine m and n.  When done, we
  1066. X     * will have:
  1067. X     *    u = (u[1]u[2]...u[m+n]) sub B
  1068. X     *    v = (v[1]v[2]...v[n]) sub B
  1069. X     *    v[1] != 0
  1070. X     *    1 < n <= 4 (if n = 1, we use a different division algorithm)
  1071. X     *    m >= 0 (otherwise u < v, which we already checked)
  1072. X     *    m + n = 4
  1073. X     * and thus
  1074. X     *    m = 4 - n <= 2
  1075. X     */
  1076. X    tmp.uq = uq;
  1077. X    u[0] = 0;
  1078. X    u[1] = HHALF(tmp.ul[H]);
  1079. X    u[2] = LHALF(tmp.ul[H]);
  1080. X    u[3] = HHALF(tmp.ul[L]);
  1081. X    u[4] = LHALF(tmp.ul[L]);
  1082. X    tmp.uq = vq;
  1083. X    v[1] = HHALF(tmp.ul[H]);
  1084. X    v[2] = LHALF(tmp.ul[H]);
  1085. X    v[3] = HHALF(tmp.ul[L]);
  1086. X    v[4] = LHALF(tmp.ul[L]);
  1087. X    for (n = 4; v[1] == 0; v++) {
  1088. X        if (--n == 1) {
  1089. X            u_long rbj;    /* r*B+u[j] (not root boy jim) */
  1090. X            digit q1, q2, q3, q4;
  1091. X
  1092. X            /*
  1093. X             * Change of plan, per exercise 16.
  1094. X             *    r = 0;
  1095. X             *    for j = 1..4:
  1096. X             *        q[j] = floor((r*B + u[j]) / v),
  1097. X             *        r = (r*B + u[j]) % v;
  1098. X             * We unroll this completely here.
  1099. X             */
  1100. X            t = v[2];    /* nonzero, by definition */
  1101. X            q1 = u[1] / t;
  1102. X            rbj = COMBINE(u[1] % t, u[2]);
  1103. X            q2 = rbj / t;
  1104. X            rbj = COMBINE(rbj % t, u[3]);
  1105. X            q3 = rbj / t;
  1106. X            rbj = COMBINE(rbj % t, u[4]);
  1107. X            q4 = rbj / t;
  1108. X            if (arq)
  1109. X                *arq = rbj % t;
  1110. X            tmp.ul[H] = COMBINE(q1, q2);
  1111. X            tmp.ul[L] = COMBINE(q3, q4);
  1112. X            return (tmp.q);
  1113. X        }
  1114. X    }
  1115. X
  1116. X    /*
  1117. X     * By adjusting q once we determine m, we can guarantee that
  1118. X     * there is a complete four-digit quotient at &qspace[1] when
  1119. X     * we finally stop.
  1120. X     */
  1121. X    for (m = 4 - n; u[1] == 0; u++)
  1122. X        m--;
  1123. X    for (i = 4 - m; --i >= 0;)
  1124. X        q[i] = 0;
  1125. X    q += 4 - m;
  1126. X
  1127. X    /*
  1128. X     * Here we run Program D, translated from MIX to C and acquiring
  1129. X     * a few minor changes.
  1130. X     *
  1131. X     * D1: choose multiplier 1 << d to ensure v[1] >= B/2.
  1132. X     */
  1133. X    d = 0;
  1134. X    for (t = v[1]; t < B / 2; t <<= 1)
  1135. X        d++;
  1136. X    if (d > 0) {
  1137. X        shl(&u[0], m + n, d);        /* u <<= d */
  1138. X        shl(&v[1], n - 1, d);        /* v <<= d */
  1139. X    }
  1140. X    /*
  1141. X     * D2: j = 0.
  1142. X     */
  1143. X    j = 0;
  1144. X    v1 = v[1];    /* for D3 -- note that v[1..n] are constant */
  1145. X    v2 = v[2];    /* for D3 */
  1146. X    do {
  1147. X        register digit uj0, uj1, uj2;
  1148. X        
  1149. X        /*
  1150. X         * D3: Calculate qhat (\^q, in TeX notation).
  1151. X         * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and
  1152. X         * let rhat = (u[j]*B + u[j+1]) mod v[1].
  1153. X         * While rhat < B and v[2]*qhat > rhat*B+u[j+2],
  1154. X         * decrement qhat and increase rhat correspondingly.
  1155. X         * Note that if rhat >= B, v[2]*qhat < rhat*B.
  1156. X         */
  1157. X        uj0 = u[j + 0];    /* for D3 only -- note that u[j+...] change */
  1158. X        uj1 = u[j + 1];    /* for D3 only */
  1159. X        uj2 = u[j + 2];    /* for D3 only */
  1160. X        if (uj0 == v1) {
  1161. X            qhat = B;
  1162. X            rhat = uj1;
  1163. X            goto qhat_too_big;
  1164. X        } else {
  1165. X            u_long n = COMBINE(uj0, uj1);
  1166. X            qhat = n / v1;
  1167. X            rhat = n % v1;
  1168. X        }
  1169. X        while (v2 * qhat > COMBINE(rhat, uj2)) {
  1170. X    qhat_too_big:
  1171. X            qhat--;
  1172. X            if ((rhat += v1) >= B)
  1173. X                break;
  1174. X        }
  1175. X        /*
  1176. X         * D4: Multiply and subtract.
  1177. X         * The variable `t' holds any borrows across the loop.
  1178. X         * We split this up so that we do not require v[0] = 0,
  1179. X         * and to eliminate a final special case.
  1180. X         */
  1181. X        for (t = 0, i = n; i > 0; i--) {
  1182. X            t = u[i + j] - v[i] * qhat - t;
  1183. X            u[i + j] = LHALF(t);
  1184. X            t = (B - HHALF(t)) & (B - 1);
  1185. X        }
  1186. X        t = u[j] - t;
  1187. X        u[j] = LHALF(t);
  1188. X        /*
  1189. X         * D5: test remainder.
  1190. X         * There is a borrow if and only if HHALF(t) is nonzero;
  1191. X         * in that (rare) case, qhat was too large (by exactly 1).
  1192. X         * Fix it by adding v[1..n] to u[j..j+n].
  1193. X         */
  1194. X        if (HHALF(t)) {
  1195. X            qhat--;
  1196. X            for (t = 0, i = n; i > 0; i--) { /* D6: add back. */
  1197. X                t += u[i + j] + v[i];
  1198. X                u[i + j] = LHALF(t);
  1199. X                t = HHALF(t);
  1200. X            }
  1201. X            u[j] = LHALF(u[j] + t);
  1202. X        }
  1203. X        q[j] = qhat;
  1204. X    } while (++j <= m);        /* D7: loop on j. */
  1205. X
  1206. X    /*
  1207. X     * If caller wants the remainder, we have to calculate it as
  1208. X     * u[m..m+n] >> d (this is at most n digits and thus fits in
  1209. X     * u[m+1..m+n], but we may need more source digits).
  1210. X     */
  1211. X    if (arq) {
  1212. X        if (d) {
  1213. X            for (i = m + n; i > m; --i)
  1214. X                u[i] = (u[i] >> d) |
  1215. X                    LHALF(u[i - 1] << (HALF_BITS - d));
  1216. X            u[i] = 0;
  1217. X        }
  1218. X        tmp.ul[H] = COMBINE(uspace[1], uspace[2]);
  1219. X        tmp.ul[L] = COMBINE(uspace[3], uspace[4]);
  1220. X        *arq = tmp.q;
  1221. X    }
  1222. X
  1223. X    tmp.ul[H] = COMBINE(qspace[1], qspace[2]);
  1224. X    tmp.ul[L] = COMBINE(qspace[3], qspace[4]);
  1225. X    return (tmp.q);
  1226. X}
  1227. X
  1228. X/*
  1229. X * Return 0, 1, or 2 as a <, =, > b respectively.
  1230. X * Neither a nor b are considered signed.
  1231. X */
  1232. Xint
  1233. X__ucmpdi2(u_quad_t a, u_quad_t b)
  1234. X{
  1235. X    union uu aa, bb;
  1236. X
  1237. X    aa.uq = a;
  1238. X    bb.uq = b;
  1239. X    return (aa.ul[H] < bb.ul[H] ? 0 : aa.ul[H] > bb.ul[H] ? 2 :
  1240. X        aa.ul[L] < bb.ul[L] ? 0 : aa.ul[L] > bb.ul[L] ? 2 : 1);
  1241. X}
  1242. END_OF_FILE
  1243.   if test 15234 -ne `wc -c <'tclmidi-2.0/drivers/LINUX/quad.c'`; then
  1244.     echo shar: \"'tclmidi-2.0/drivers/LINUX/quad.c'\" unpacked with wrong size!
  1245.   fi
  1246.   # end of 'tclmidi-2.0/drivers/LINUX/quad.c'
  1247. fi
  1248. if test -f 'tclmidi-2.0/drivers/SVR4/quad.c' -a "${1}" != "-c" ; then 
  1249.   echo shar: Will not clobber existing file \"'tclmidi-2.0/drivers/SVR4/quad.c'\"
  1250. else
  1251.   echo shar: Extracting \"'tclmidi-2.0/drivers/SVR4/quad.c'\" \(15234 characters\)
  1252.   sed "s/^X//" >'tclmidi-2.0/drivers/SVR4/quad.c' <<'END_OF_FILE'
  1253. X/*
  1254. X * I need long long for midi timing accuracy. - mbd
  1255. X *
  1256. X * Inline argument prototype declarations to suppress gcc 2 warnings - lig
  1257. X */
  1258. X
  1259. X/*-
  1260. X * Copyright (c) 1992 The Regents of the University of California.
  1261. X * All rights reserved.
  1262. X *
  1263. X * This software was developed by the Computer Systems Engineering group
  1264. X * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
  1265. X * contributed to Berkeley.
  1266. X *
  1267. X * Redistribution and use in source and binary forms, with or without
  1268. X * modification, are permitted provided that the following conditions
  1269. X * are met:
  1270. X * 1. Redistributions of source code must retain the above copyright
  1271. X *    notice, this list of conditions and the following disclaimer.
  1272. X * 2. Redistributions in binary form must reproduce the above copyright
  1273. X *    notice, this list of conditions and the following disclaimer in the
  1274. X *    documentation and/or other materials provided with the distribution.
  1275. X * 3. All advertising materials mentioning features or use of this software
  1276. X *    must display the following acknowledgement:
  1277. X *    This product includes software developed by the University of
  1278. X *    California, Berkeley and its contributors.
  1279. X * 4. Neither the name of the University nor the names of its contributors
  1280. X *    may be used to endorse or promote products derived from this software
  1281. X *    without specific prior written permission.
  1282. X *
  1283. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  1284. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1285. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1286. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  1287. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1288. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1289. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1290. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1291. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1292. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1293. X * SUCH DAMAGE.
  1294. X */
  1295. X
  1296. X#if defined(LIBC_SCCS) && !defined(lint)
  1297. Xstatic char sccsid[] = "@(#)adddi3.c    5.5 (Berkeley) 6/25/92";
  1298. X#endif /* LIBC_SCCS and not lint */
  1299. X
  1300. X#include "quad.h"
  1301. X
  1302. X/*
  1303. X * Add two quads.  This is trivial since a one-bit carry from a single
  1304. X * u_long addition x+y occurs if and only if the sum x+y is less than
  1305. X * either x or y (the choice to compare with x or y is arbitrary).
  1306. X */
  1307. Xquad_t
  1308. X__adddi3(quad_t a, quad_t b)
  1309. X{
  1310. X    union uu aa, bb, sum;
  1311. X
  1312. X    aa.q = a;
  1313. X    bb.q = b;
  1314. X    sum.ul[L] = aa.ul[L] + bb.ul[L];
  1315. X    sum.ul[H] = aa.ul[H] + bb.ul[H] + (sum.ul[L] < bb.ul[L]);
  1316. X    return (sum.q);
  1317. X}
  1318. X
  1319. X/*
  1320. X * Divide two signed quads.
  1321. X * ??? if -1/2 should produce -1 on this machine, this code is wrong
  1322. X */
  1323. Xquad_t
  1324. X__divdi3(quad_t a, quad_t b)
  1325. X{
  1326. X    u_quad_t ua, ub, uq;
  1327. X    int neg;
  1328. X
  1329. X    if (a < 0)
  1330. X        ua = -(u_quad_t)a, neg = 1;
  1331. X    else
  1332. X        ua = a, neg = 0;
  1333. X    if (b < 0)
  1334. X        ub = -(u_quad_t)b, neg ^= 1;
  1335. X    else
  1336. X        ub = b;
  1337. X    uq = __qdivrem(ua, ub, (u_quad_t *)0);
  1338. X    return (neg ? -uq : uq);
  1339. X}
  1340. X
  1341. X
  1342. X/*
  1343. X * Multiply two quads.
  1344. X *
  1345. X * Our algorithm is based on the following.  Split incoming quad values
  1346. X * u and v (where u,v >= 0) into
  1347. X *
  1348. X *    u = 2^n u1  *  u0    (n = number of bits in `u_long', usu. 32)
  1349. X *
  1350. X * and 
  1351. X *
  1352. X *    v = 2^n v1  *  v0
  1353. X *
  1354. X * Then
  1355. X *
  1356. X *    uv = 2^2n u1 v1  +  2^n u1 v0  +  2^n v1 u0  +  u0 v0
  1357. X *       = 2^2n u1 v1  +     2^n (u1 v0 + v1 u0)   +  u0 v0
  1358. X *
  1359. X * Now add 2^n u1 v1 to the first term and subtract it from the middle,
  1360. X * and add 2^n u0 v0 to the last term and subtract it from the middle.
  1361. X * This gives:
  1362. X *
  1363. X *    uv = (2^2n + 2^n) (u1 v1)  +
  1364. X *             (2^n)    (u1 v0 - u1 v1 + u0 v1 - u0 v0)  +
  1365. X *           (2^n + 1)  (u0 v0)
  1366. X *
  1367. X * Factoring the middle a bit gives us:
  1368. X *
  1369. X *    uv = (2^2n + 2^n) (u1 v1)  +            [u1v1 = high]
  1370. X *         (2^n)    (u1 - u0) (v0 - v1)  +    [(u1-u0)... = mid]
  1371. X *           (2^n + 1)  (u0 v0)            [u0v0 = low]
  1372. X *
  1373. X * The terms (u1 v1), (u1 - u0) (v0 - v1), and (u0 v0) can all be done
  1374. X * in just half the precision of the original.  (Note that either or both
  1375. X * of (u1 - u0) or (v0 - v1) may be negative.)
  1376. X *
  1377. X * This algorithm is from Knuth vol. 2 (2nd ed), section 4.3.3, p. 278.
  1378. X *
  1379. X * Since C does not give us a `long * long = quad' operator, we split
  1380. X * our input quads into two longs, then split the two longs into two
  1381. X * shorts.  We can then calculate `short * short = long' in native
  1382. X * arithmetic.
  1383. X *
  1384. X * Our product should, strictly speaking, be a `long quad', with 128
  1385. X * bits, but we are going to discard the upper 64.  In other words,
  1386. X * we are not interested in uv, but rather in (uv mod 2^2n).  This
  1387. X * makes some of the terms above vanish, and we get:
  1388. X *
  1389. X *    (2^n)(high) + (2^n)(mid) + (2^n + 1)(low)
  1390. X *
  1391. X * or
  1392. X *
  1393. X *    (2^n)(high + mid + low) + low
  1394. X *
  1395. X * Furthermore, `high' and `mid' can be computed mod 2^n, as any factor
  1396. X * of 2^n in either one will also vanish.  Only `low' need be computed
  1397. X * mod 2^2n, and only because of the final term above.
  1398. X */
  1399. Xstatic quad_t __lmulq(u_long, u_long);
  1400. X
  1401. Xquad_t
  1402. X__muldi3(quad_t a, quad_t b)
  1403. X{
  1404. X    union uu u, v, low, prod;
  1405. X    register u_long high, mid, udiff, vdiff;
  1406. X    register int negall, negmid;
  1407. X#define    u1    u.ul[H]
  1408. X#define    u0    u.ul[L]
  1409. X#define    v1    v.ul[H]
  1410. X#define    v0    v.ul[L]
  1411. X
  1412. X    /*
  1413. X     * Get u and v such that u, v >= 0.  When this is finished,
  1414. X     * u1, u0, v1, and v0 will be directly accessible through the
  1415. X     * longword fields.
  1416. X     */
  1417. X    if (a >= 0)
  1418. X        u.q = a, negall = 0;
  1419. X    else
  1420. X        u.q = -a, negall = 1;
  1421. X    if (b >= 0)
  1422. X        v.q = b;
  1423. X    else
  1424. X        v.q = -b, negall ^= 1;
  1425. X
  1426. X    if (u1 == 0 && v1 == 0) {
  1427. X        /*
  1428. X         * An (I hope) important optimization occurs when u1 and v1
  1429. X         * are both 0.  This should be common since most numbers
  1430. X         * are small.  Here the product is just u0*v0.
  1431. X         */
  1432. X        prod.q = __lmulq(u0, v0);
  1433. X    } else {
  1434. X        /*
  1435. X         * Compute the three intermediate products, remembering
  1436. X         * whether the middle term is negative.  We can discard
  1437. X         * any upper bits in high and mid, so we can use native
  1438. X         * u_long * u_long => u_long arithmetic.
  1439. X         */
  1440. X        low.q = __lmulq(u0, v0);
  1441. X
  1442. X        if (u1 >= u0)
  1443. X            negmid = 0, udiff = u1 - u0;
  1444. X        else
  1445. X            negmid = 1, udiff = u0 - u1;
  1446. X        if (v0 >= v1)
  1447. X            vdiff = v0 - v1;
  1448. X        else
  1449. X            vdiff = v1 - v0, negmid ^= 1;
  1450. X        mid = udiff * vdiff;
  1451. X
  1452. X        high = u1 * v1;
  1453. X
  1454. X        /*
  1455. X         * Assemble the final product.
  1456. X         */
  1457. X        prod.ul[H] = high + (negmid ? -mid : mid) + low.ul[L] +
  1458. X            low.ul[H];
  1459. X        prod.ul[L] = low.ul[L];
  1460. X    }
  1461. X    return (negall ? -prod.q : prod.q);
  1462. X#undef u1
  1463. X#undef u0
  1464. X#undef v1
  1465. X#undef v0
  1466. X}
  1467. X
  1468. X/*
  1469. X * Multiply two 2N-bit longs to produce a 4N-bit quad, where N is half
  1470. X * the number of bits in a long (whatever that is---the code below
  1471. X * does not care as long as quad.h does its part of the bargain---but
  1472. X * typically N==16).
  1473. X *
  1474. X * We use the same algorithm from Knuth, but this time the modulo refinement
  1475. X * does not apply.  On the other hand, since N is half the size of a long,
  1476. X * we can get away with native multiplication---none of our input terms
  1477. X * exceeds (ULONG_MAX >> 1).
  1478. X *
  1479. X * Note that, for u_long l, the quad-precision result
  1480. X *
  1481. X *    l << N
  1482. X *
  1483. X * splits into high and low longs as HHALF(l) and LHUP(l) respectively.
  1484. X */
  1485. Xstatic quad_t
  1486. X__lmulq(u_long u, u_long v)
  1487. X{
  1488. X    u_long u1, u0, v1, v0, udiff, vdiff, high, mid, low;
  1489. X    u_long prodh, prodl, was;
  1490. X    union uu prod;
  1491. X    int neg;
  1492. X
  1493. X    u1 = HHALF(u);
  1494. X    u0 = LHALF(u);
  1495. X    v1 = HHALF(v);
  1496. X    v0 = LHALF(v);
  1497. X
  1498. X    low = u0 * v0;
  1499. X
  1500. X    /* This is the same small-number optimization as before. */
  1501. X    if (u1 == 0 && v1 == 0)
  1502. X        return (low);
  1503. X
  1504. X    if (u1 >= u0)
  1505. X        udiff = u1 - u0, neg = 0;
  1506. X    else
  1507. X        udiff = u0 - u1, neg = 1;
  1508. X    if (v0 >= v1)
  1509. X        vdiff = v0 - v1;
  1510. X    else
  1511. X        vdiff = v1 - v0, neg ^= 1;
  1512. X    mid = udiff * vdiff;
  1513. X
  1514. X    high = u1 * v1;
  1515. X
  1516. X    /* prod = (high << 2N) + (high << N); */
  1517. X    prodh = high + HHALF(high);
  1518. X    prodl = LHUP(high);
  1519. X
  1520. X    /* if (neg) prod -= mid << N; else prod += mid << N; */
  1521. X    if (neg) {
  1522. X        was = prodl;
  1523. X        prodl -= LHUP(mid);
  1524. X        prodh -= HHALF(mid) + (prodl > was);
  1525. X    } else {
  1526. X        was = prodl;
  1527. X        prodl += LHUP(mid);
  1528. X        prodh += HHALF(mid) + (prodl < was);
  1529. X    }
  1530. X
  1531. X    /* prod += low << N */
  1532. X    was = prodl;
  1533. X    prodl += LHUP(low);
  1534. X    prodh += HHALF(low) + (prodl < was);
  1535. X    /* ... + low; */
  1536. X    if ((prodl += low) < low)
  1537. X        prodh++;
  1538. X
  1539. X    /* return 4N-bit product */
  1540. X    prod.ul[H] = prodh;
  1541. X    prod.ul[L] = prodl;
  1542. X    return (prod.q);
  1543. X}
  1544. X
  1545. X/*
  1546. X * Return remainder after dividing two signed quads.
  1547. X *
  1548. X * XXX
  1549. X * If -1/2 should produce -1 on this machine, this code is wrong.
  1550. X */
  1551. Xquad_t
  1552. X__moddi3(quad_t a, quad_t b)
  1553. X{
  1554. X    u_quad_t ua, ub, ur;
  1555. X    int neg;
  1556. X
  1557. X    if (a < 0)
  1558. X        ua = -(u_quad_t)a, neg = 1;
  1559. X    else
  1560. X        ua = a, neg = 0;
  1561. X    if (b < 0)
  1562. X        ub = -(u_quad_t)b, neg ^= 1;
  1563. X    else
  1564. X        ub = b;
  1565. X    (void)__qdivrem(ua, ub, &ur);
  1566. X    return (neg ? -ur : ur);
  1567. X}
  1568. X
  1569. X/*
  1570. X * Return 0, 1, or 2 as a <, =, > b respectively.
  1571. X * Both a and b are considered signed---which means only the high word is
  1572. X * signed.
  1573. X */
  1574. Xint
  1575. X__cmpdi2(quad_t a, quad_t b)
  1576. X{
  1577. X    union uu aa, bb;
  1578. X
  1579. X    aa.q = a;
  1580. X    bb.q = b;
  1581. X    return (aa.sl[H] < bb.sl[H] ? 0 : aa.sl[H] > bb.sl[H] ? 2 :
  1582. X        aa.ul[L] < bb.ul[L] ? 0 : aa.ul[L] > bb.ul[L] ? 2 : 1);
  1583. X}
  1584. X
  1585. X/*
  1586. X * Return -a (or, equivalently, 0 - a), in quad.  See subdi3.c.
  1587. X */
  1588. Xquad_t
  1589. X__negdi2(quad_t a)
  1590. X{
  1591. X    union uu aa, res;
  1592. X
  1593. X    aa.q = a;
  1594. X    res.ul[L] = -aa.ul[L];
  1595. X    res.ul[H] = -aa.ul[H] - (res.ul[L] > 0);
  1596. X    return (res.q);
  1597. X}
  1598. X
  1599. X/*
  1600. X * Multiprecision divide.  This algorithm is from Knuth vol. 2 (2nd ed),
  1601. X * section 4.3.1, pp. 257--259.
  1602. X */
  1603. X
  1604. X#define    B    (1 << HALF_BITS)    /* digit base */
  1605. X
  1606. X/* Combine two `digits' to make a single two-digit number. */
  1607. X#define    COMBINE(a, b) (((u_long)(a) << HALF_BITS) | (b))
  1608. X
  1609. X/* select a type for digits in base B: use unsigned short if they fit */
  1610. X#if ULONG_MAX == 0xffffffff && USHRT_MAX >= 0xffff
  1611. Xtypedef unsigned short digit;
  1612. X#else
  1613. Xtypedef u_long digit;
  1614. X#endif
  1615. X
  1616. X/*
  1617. X * Shift p[0]..p[len] left `sh' bits, ignoring any bits that
  1618. X * `fall out' the left (there never will be any such anyway).
  1619. X * We may assume len >= 0.  NOTE THAT THIS WRITES len+1 DIGITS.
  1620. X */
  1621. Xstatic void
  1622. Xshl(register digit *p, register int len, register int sh)
  1623. X{
  1624. X    register int i;
  1625. X
  1626. X    for (i = 0; i < len; i++)
  1627. X        p[i] = LHALF(p[i] << sh) | (p[i + 1] >> (HALF_BITS - sh));
  1628. X    p[i] = LHALF(p[i] << sh);
  1629. X}
  1630. X
  1631. X/*
  1632. X * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v.
  1633. X *
  1634. X * We do this in base 2-sup-HALF_BITS, so that all intermediate products
  1635. X * fit within u_long.  As a consequence, the maximum length dividend and
  1636. X * divisor are 4 `digits' in this base (they are shorter if they have
  1637. X * leading zeros).
  1638. X */
  1639. Xu_quad_t
  1640. X__qdivrem(uq, vq, arq)
  1641. X    u_quad_t uq, vq, *arq;
  1642. X{
  1643. X    union uu tmp;
  1644. X    digit *u, *v, *q;
  1645. X    register digit v1, v2;
  1646. X    u_long qhat, rhat, t;
  1647. X    int m, n, d, j, i;
  1648. X    digit uspace[5], vspace[5], qspace[5];
  1649. X
  1650. X    /*
  1651. X     * Take care of special cases: divide by zero, and u < v.
  1652. X     */
  1653. X    if (vq == 0) {
  1654. X        /* divide by zero. */
  1655. X        static volatile const unsigned int zero = 0;
  1656. X
  1657. X        tmp.ul[H] = tmp.ul[L] = 1 / zero;
  1658. X        if (arq)
  1659. X            *arq = uq;
  1660. X        return (tmp.q);
  1661. X    }
  1662. X    if (uq < vq) {
  1663. X        if (arq)
  1664. X            *arq = uq;
  1665. X        return (0);
  1666. X    }
  1667. X    u = &uspace[0];
  1668. X    v = &vspace[0];
  1669. X    q = &qspace[0];
  1670. X
  1671. X    /*
  1672. X     * Break dividend and divisor into digits in base B, then
  1673. X     * count leading zeros to determine m and n.  When done, we
  1674. X     * will have:
  1675. X     *    u = (u[1]u[2]...u[m+n]) sub B
  1676. X     *    v = (v[1]v[2]...v[n]) sub B
  1677. X     *    v[1] != 0
  1678. X     *    1 < n <= 4 (if n = 1, we use a different division algorithm)
  1679. X     *    m >= 0 (otherwise u < v, which we already checked)
  1680. X     *    m + n = 4
  1681. X     * and thus
  1682. X     *    m = 4 - n <= 2
  1683. X     */
  1684. X    tmp.uq = uq;
  1685. X    u[0] = 0;
  1686. X    u[1] = HHALF(tmp.ul[H]);
  1687. X    u[2] = LHALF(tmp.ul[H]);
  1688. X    u[3] = HHALF(tmp.ul[L]);
  1689. X    u[4] = LHALF(tmp.ul[L]);
  1690. X    tmp.uq = vq;
  1691. X    v[1] = HHALF(tmp.ul[H]);
  1692. X    v[2] = LHALF(tmp.ul[H]);
  1693. X    v[3] = HHALF(tmp.ul[L]);
  1694. X    v[4] = LHALF(tmp.ul[L]);
  1695. X    for (n = 4; v[1] == 0; v++) {
  1696. X        if (--n == 1) {
  1697. X            u_long rbj;    /* r*B+u[j] (not root boy jim) */
  1698. X            digit q1, q2, q3, q4;
  1699. X
  1700. X            /*
  1701. X             * Change of plan, per exercise 16.
  1702. X             *    r = 0;
  1703. X             *    for j = 1..4:
  1704. X             *        q[j] = floor((r*B + u[j]) / v),
  1705. X             *        r = (r*B + u[j]) % v;
  1706. X             * We unroll this completely here.
  1707. X             */
  1708. X            t = v[2];    /* nonzero, by definition */
  1709. X            q1 = u[1] / t;
  1710. X            rbj = COMBINE(u[1] % t, u[2]);
  1711. X            q2 = rbj / t;
  1712. X            rbj = COMBINE(rbj % t, u[3]);
  1713. X            q3 = rbj / t;
  1714. X            rbj = COMBINE(rbj % t, u[4]);
  1715. X            q4 = rbj / t;
  1716. X            if (arq)
  1717. X                *arq = rbj % t;
  1718. X            tmp.ul[H] = COMBINE(q1, q2);
  1719. X            tmp.ul[L] = COMBINE(q3, q4);
  1720. X            return (tmp.q);
  1721. X        }
  1722. X    }
  1723. X
  1724. X    /*
  1725. X     * By adjusting q once we determine m, we can guarantee that
  1726. X     * there is a complete four-digit quotient at &qspace[1] when
  1727. X     * we finally stop.
  1728. X     */
  1729. X    for (m = 4 - n; u[1] == 0; u++)
  1730. X        m--;
  1731. X    for (i = 4 - m; --i >= 0;)
  1732. X        q[i] = 0;
  1733. X    q += 4 - m;
  1734. X
  1735. X    /*
  1736. X     * Here we run Program D, translated from MIX to C and acquiring
  1737. X     * a few minor changes.
  1738. X     *
  1739. X     * D1: choose multiplier 1 << d to ensure v[1] >= B/2.
  1740. X     */
  1741. X    d = 0;
  1742. X    for (t = v[1]; t < B / 2; t <<= 1)
  1743. X        d++;
  1744. X    if (d > 0) {
  1745. X        shl(&u[0], m + n, d);        /* u <<= d */
  1746. X        shl(&v[1], n - 1, d);        /* v <<= d */
  1747. X    }
  1748. X    /*
  1749. X     * D2: j = 0.
  1750. X     */
  1751. X    j = 0;
  1752. X    v1 = v[1];    /* for D3 -- note that v[1..n] are constant */
  1753. X    v2 = v[2];    /* for D3 */
  1754. X    do {
  1755. X        register digit uj0, uj1, uj2;
  1756. X        
  1757. X        /*
  1758. X         * D3: Calculate qhat (\^q, in TeX notation).
  1759. X         * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and
  1760. X         * let rhat = (u[j]*B + u[j+1]) mod v[1].
  1761. X         * While rhat < B and v[2]*qhat > rhat*B+u[j+2],
  1762. X         * decrement qhat and increase rhat correspondingly.
  1763. X         * Note that if rhat >= B, v[2]*qhat < rhat*B.
  1764. X         */
  1765. X        uj0 = u[j + 0];    /* for D3 only -- note that u[j+...] change */
  1766. X        uj1 = u[j + 1];    /* for D3 only */
  1767. X        uj2 = u[j + 2];    /* for D3 only */
  1768. X        if (uj0 == v1) {
  1769. X            qhat = B;
  1770. X            rhat = uj1;
  1771. X            goto qhat_too_big;
  1772. X        } else {
  1773. X            u_long n = COMBINE(uj0, uj1);
  1774. X            qhat = n / v1;
  1775. X            rhat = n % v1;
  1776. X        }
  1777. X        while (v2 * qhat > COMBINE(rhat, uj2)) {
  1778. X    qhat_too_big:
  1779. X            qhat--;
  1780. X            if ((rhat += v1) >= B)
  1781. X                break;
  1782. X        }
  1783. X        /*
  1784. X         * D4: Multiply and subtract.
  1785. X         * The variable `t' holds any borrows across the loop.
  1786. X         * We split this up so that we do not require v[0] = 0,
  1787. X         * and to eliminate a final special case.
  1788. X         */
  1789. X        for (t = 0, i = n; i > 0; i--) {
  1790. X            t = u[i + j] - v[i] * qhat - t;
  1791. X            u[i + j] = LHALF(t);
  1792. X            t = (B - HHALF(t)) & (B - 1);
  1793. X        }
  1794. X        t = u[j] - t;
  1795. X        u[j] = LHALF(t);
  1796. X        /*
  1797. X         * D5: test remainder.
  1798. X         * There is a borrow if and only if HHALF(t) is nonzero;
  1799. X         * in that (rare) case, qhat was too large (by exactly 1).
  1800. X         * Fix it by adding v[1..n] to u[j..j+n].
  1801. X         */
  1802. X        if (HHALF(t)) {
  1803. X            qhat--;
  1804. X            for (t = 0, i = n; i > 0; i--) { /* D6: add back. */
  1805. X                t += u[i + j] + v[i];
  1806. X                u[i + j] = LHALF(t);
  1807. X                t = HHALF(t);
  1808. X            }
  1809. X            u[j] = LHALF(u[j] + t);
  1810. X        }
  1811. X        q[j] = qhat;
  1812. X    } while (++j <= m);        /* D7: loop on j. */
  1813. X
  1814. X    /*
  1815. X     * If caller wants the remainder, we have to calculate it as
  1816. X     * u[m..m+n] >> d (this is at most n digits and thus fits in
  1817. X     * u[m+1..m+n], but we may need more source digits).
  1818. X     */
  1819. X    if (arq) {
  1820. X        if (d) {
  1821. X            for (i = m + n; i > m; --i)
  1822. X                u[i] = (u[i] >> d) |
  1823. X                    LHALF(u[i - 1] << (HALF_BITS - d));
  1824. X            u[i] = 0;
  1825. X        }
  1826. X        tmp.ul[H] = COMBINE(uspace[1], uspace[2]);
  1827. X        tmp.ul[L] = COMBINE(uspace[3], uspace[4]);
  1828. X        *arq = tmp.q;
  1829. X    }
  1830. X
  1831. X    tmp.ul[H] = COMBINE(qspace[1], qspace[2]);
  1832. X    tmp.ul[L] = COMBINE(qspace[3], qspace[4]);
  1833. X    return (tmp.q);
  1834. X}
  1835. X
  1836. X/*
  1837. X * Return 0, 1, or 2 as a <, =, > b respectively.
  1838. X * Neither a nor b are considered signed.
  1839. X */
  1840. Xint
  1841. X__ucmpdi2(u_quad_t a, u_quad_t b)
  1842. X{
  1843. X    union uu aa, bb;
  1844. X
  1845. X    aa.uq = a;
  1846. X    bb.uq = b;
  1847. X    return (aa.ul[H] < bb.ul[H] ? 0 : aa.ul[H] > bb.ul[H] ? 2 :
  1848. X        aa.ul[L] < bb.ul[L] ? 0 : aa.ul[L] > bb.ul[L] ? 2 : 1);
  1849. X}
  1850. END_OF_FILE
  1851.   if test 15234 -ne `wc -c <'tclmidi-2.0/drivers/SVR4/quad.c'`; then
  1852.     echo shar: \"'tclmidi-2.0/drivers/SVR4/quad.c'\" unpacked with wrong size!
  1853.   fi
  1854.   # end of 'tclmidi-2.0/drivers/SVR4/quad.c'
  1855. fi
  1856. if test -f 'tclmidi-2.0/events/MetaInstName.C' -a "${1}" != "-c" ; then 
  1857.   echo shar: Will not clobber existing file \"'tclmidi-2.0/events/MetaInstName.C'\"
  1858. else
  1859.   echo shar: Extracting \"'tclmidi-2.0/events/MetaInstName.C'\" \(2447 characters\)
  1860.   sed "s/^X//" >'tclmidi-2.0/events/MetaInstName.C' <<'END_OF_FILE'
  1861. X/*-
  1862. X * Copyright (c) 1993, 1994 Michael B. Durian.  All rights reserved.
  1863. X *
  1864. X * Redistribution and use in source and binary forms, with or without
  1865. X * modification, are permitted provided that the following conditions
  1866. X * are met:
  1867. X * 1. Redistributions of source code must retain the above copyright
  1868. X *    notice, this list of conditions and the following disclaimer.
  1869. X * 2. Redistributions in binary form must reproduce the above copyright
  1870. X *    notice, this list of conditions and the following disclaimer in the
  1871. X *    documentation and/or other materials provided with the distribution.
  1872. X * 3. All advertising materials mentioning features or use of this software
  1873. X *    must display the following acknowledgement:
  1874. X *    This product includes software developed by Michael B. Durian.
  1875. X * 4. The name of the the Author may be used to endorse or promote 
  1876. X *    products derived from this software without specific prior written 
  1877. X *    permission.
  1878. X *
  1879. X * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 
  1880. X * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  1881. X * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  
  1882. X * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
  1883. X * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1884. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1885. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1886. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1887. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1888. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1889. X * SUCH DAMAGE.
  1890. X */
  1891. X#include "MetaInstName.h"
  1892. X
  1893. XMetaInstrumentNameEvent::MetaInstrumentNameEvent()
  1894. X{
  1895. X}
  1896. X
  1897. XMetaInstrumentNameEvent::MetaInstrumentNameEvent(unsigned long t,
  1898. X    const char *str) : MetaTextEvent(t, str)
  1899. X{
  1900. X}
  1901. X
  1902. XMetaInstrumentNameEvent::MetaInstrumentNameEvent(
  1903. X    const MetaInstrumentNameEvent &e) : MetaTextEvent(e)
  1904. X{
  1905. X}
  1906. X
  1907. XMetaInstrumentNameEvent::~MetaInstrumentNameEvent()
  1908. X{
  1909. X}
  1910. X
  1911. X
  1912. XMetaInstrumentNameEvent &
  1913. XMetaInstrumentNameEvent::operator=(const MetaInstrumentNameEvent &e)
  1914. X{
  1915. X
  1916. X    (MetaTextEvent)*this = (MetaTextEvent)e;
  1917. X    return (*this);
  1918. X}
  1919. X
  1920. Xchar *
  1921. XMetaInstrumentNameEvent::GetEventStr(void) const
  1922. X{
  1923. X
  1924. X    return (MetaTextEvent::GetEventStr());
  1925. X}
  1926. X
  1927. Xostream &
  1928. Xoperator<<(ostream &os, const MetaInstrumentNameEvent &e)
  1929. X{
  1930. X    char *str;
  1931. X
  1932. X    os << (str = e.GetEventStr());
  1933. X    delete str;
  1934. X    return (os);
  1935. X}
  1936. END_OF_FILE
  1937.   if test 2447 -ne `wc -c <'tclmidi-2.0/events/MetaInstName.C'`; then
  1938.     echo shar: \"'tclmidi-2.0/events/MetaInstName.C'\" unpacked with wrong size!
  1939.   fi
  1940.   # end of 'tclmidi-2.0/events/MetaInstName.C'
  1941. fi
  1942. if test -f 'tclmidi-2.0/events/MetaKey.C' -a "${1}" != "-c" ; then 
  1943.   echo shar: Will not clobber existing file \"'tclmidi-2.0/events/MetaKey.C'\"
  1944. else
  1945.   echo shar: Extracting \"'tclmidi-2.0/events/MetaKey.C'\" \(7506 characters\)
  1946.   sed "s/^X//" >'tclmidi-2.0/events/MetaKey.C' <<'END_OF_FILE'
  1947. X/*-
  1948. X * Copyright (c) 1993, 1994 Michael B. Durian.  All rights reserved.
  1949. X *
  1950. X * Redistribution and use in source and binary forms, with or without
  1951. X * modification, are permitted provided that the following conditions
  1952. X * are met:
  1953. X * 1. Redistributions of source code must retain the above copyright
  1954. X *    notice, this list of conditions and the following disclaimer.
  1955. X * 2. Redistributions in binary form must reproduce the above copyright
  1956. X *    notice, this list of conditions and the following disclaimer in the
  1957. X *    documentation and/or other materials provided with the distribution.
  1958. X * 3. All advertising materials mentioning features or use of this software
  1959. X *    must display the following acknowledgement:
  1960. X *    This product includes software developed by Michael B. Durian.
  1961. X * 4. The name of the the Author may be used to endorse or promote 
  1962. X *    products derived from this software without specific prior written 
  1963. X *    permission.
  1964. X *
  1965. X * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 
  1966. X * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  1967. X * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  
  1968. X * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
  1969. X * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1970. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1971. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1972. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1973. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1974. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1975. X * SUCH DAMAGE.
  1976. X */
  1977. X#include <ctype.h>
  1978. X#include <string.h>
  1979. X#include "MetaKey.h"
  1980. X
  1981. XKey
  1982. XIntToKey(int i)
  1983. X{
  1984. X
  1985. X    switch (i) {
  1986. X    case -7:
  1987. X        return (KEY_CFLAT);
  1988. X    case -6:
  1989. X        return (KEY_GFLAT);
  1990. X    case -5:
  1991. X        return (KEY_DFLAT);
  1992. X    case -4:
  1993. X        return (KEY_AFLAT);
  1994. X    case -3:
  1995. X        return (KEY_EFLAT);
  1996. X    case -2:
  1997. X        return (KEY_BFLAT);
  1998. X    case -1:
  1999. X        return (KEY_F);
  2000. X    case 0:
  2001. X        return (KEY_C);
  2002. X    case 1:
  2003. X        return (KEY_G);
  2004. X    case 2:
  2005. X        return (KEY_D);
  2006. X    case 3:
  2007. X        return (KEY_A);
  2008. X    case 4:
  2009. X        return (KEY_E);
  2010. X    case 5:
  2011. X        return (KEY_B);
  2012. X    case 6:
  2013. X        return (KEY_FSHARP);
  2014. X    case 7:
  2015. X        return (KEY_CSHARP);
  2016. X    default:
  2017. X        return (KEY_C);
  2018. X    }
  2019. X}
  2020. X
  2021. Xint
  2022. XKeyToInt(Key k)
  2023. X{
  2024. X
  2025. X    switch (k) {
  2026. X    case KEY_CFLAT:
  2027. X        return (-7);
  2028. X    case KEY_GFLAT:
  2029. X        return (-6);
  2030. X    case KEY_DFLAT:
  2031. X        return (-5);
  2032. X    case KEY_AFLAT:
  2033. X        return (-4);
  2034. X    case KEY_EFLAT:
  2035. X        return (-3);
  2036. X    case KEY_BFLAT:
  2037. X        return (-2);
  2038. X    case KEY_F:
  2039. X        return (-1);
  2040. X    case KEY_C:
  2041. X        return (0);
  2042. X    case KEY_G:
  2043. X        return (1);
  2044. X    case KEY_D:
  2045. X        return (2);
  2046. X    case KEY_A:
  2047. X        return (3);
  2048. X    case KEY_E:
  2049. X        return (4);
  2050. X    case KEY_B:
  2051. X        return (5);
  2052. X    case KEY_FSHARP:
  2053. X        return (6);
  2054. X    case KEY_CSHARP:
  2055. X        return (7);
  2056. X    default:
  2057. X        return (0);
  2058. X    }
  2059. X}
  2060. X
  2061. XMetaKeyEvent::MetaKeyEvent() : key(KEY_C), mode(MODE_MAJOR)
  2062. X{
  2063. X}
  2064. X
  2065. XMetaKeyEvent::MetaKeyEvent(unsigned long t, Key k, Mode m) : MetaEvent(t),
  2066. X    key(k), mode(m)
  2067. X{
  2068. X}
  2069. X
  2070. XMetaKeyEvent::MetaKeyEvent(const MetaKeyEvent &e) : MetaEvent(e),
  2071. X    key(e.key), mode(e.mode)
  2072. X{
  2073. X}
  2074. X
  2075. Xconst char *
  2076. XMetaKeyEvent::GetKeyStr(void) const
  2077. X{
  2078. X
  2079. X    switch (key) {
  2080. X    case KEY_CFLAT:
  2081. X        return ("C Flat");
  2082. X    case KEY_GFLAT:
  2083. X        return ("G Flat");
  2084. X    case KEY_DFLAT:
  2085. X        return ("D Flat");
  2086. X    case KEY_AFLAT:
  2087. X        return ("A Flat");
  2088. X    case KEY_EFLAT:
  2089. X        return ("E Flat");
  2090. X    case KEY_BFLAT:
  2091. X        return ("B Flat");
  2092. X    case KEY_F:
  2093. X        return ("F");
  2094. X    case KEY_C:
  2095. X        return ("C");
  2096. X    case KEY_G:
  2097. X        return ("G");
  2098. X    case KEY_D:
  2099. X        return ("D");
  2100. X    case KEY_A:
  2101. X        return ("A");
  2102. X    case KEY_E:
  2103. X        return ("E");
  2104. X    case KEY_B:
  2105. X        return ("B");
  2106. X    case KEY_FSHARP:
  2107. X        return ("F Sharp");
  2108. X    case KEY_CSHARP:
  2109. X        return ("C Sharp");
  2110. X    }
  2111. X    return ("");
  2112. X}
  2113. X
  2114. Xconst char *
  2115. XMetaKeyEvent::GetModeStr(void) const
  2116. X{
  2117. X
  2118. X    switch (mode) {
  2119. X    case MODE_MAJOR:
  2120. X        return ("major");
  2121. X    case MODE_MINOR:
  2122. X        return ("minor");
  2123. X    }
  2124. X    return ("");
  2125. X}
  2126. X
  2127. XMetaKeyEvent &
  2128. XMetaKeyEvent::operator=(const MetaKeyEvent &e)
  2129. X{
  2130. X
  2131. X    (MetaEvent)*this = (MetaEvent)e;
  2132. X    key = e.key;
  2133. X    mode = e.mode;
  2134. X    return (*this);
  2135. X}
  2136. X
  2137. Xchar *
  2138. XMetaKeyEvent::GetEventStr(void) const
  2139. X{
  2140. X    ostrstream buf;
  2141. X    char *tbuf;
  2142. X
  2143. X    tbuf = MetaEvent::GetEventStr();
  2144. X    buf << tbuf << " Key: " << GetKeyStr() << " Mode: " << GetModeStr()
  2145. X        << ends;
  2146. X    delete tbuf;
  2147. X    return (buf.str());
  2148. X}
  2149. X
  2150. Xconst char *
  2151. XMetaKeyEvent::SMFRead(SMFTrack &t)
  2152. X{
  2153. X    const unsigned char *ptr;
  2154. X
  2155. X    // get and throw away length
  2156. X    if (t.GetVarValue() != 2)
  2157. X        return ("Incomplete MetaKeyEvent - bad length");
  2158. X
  2159. X    if ((ptr = t.GetByte()) == 0)
  2160. X        return ("Incomplete MetaKeyEvent - missing key");
  2161. X    key = IntToKey((signed char)*ptr);
  2162. X    if ((ptr = t.GetByte()) == 0)
  2163. X        return ("Incomplete MetaKeyEvent - missing mode");
  2164. X    mode = (Mode)*ptr;
  2165. X    return (0);
  2166. X}
  2167. X
  2168. Xconst char *
  2169. XMetaKeyEvent::SMFWrite(SMFTrack &t) const
  2170. X{
  2171. X
  2172. X    if (!t.PutFixValue(2))
  2173. X        return ("Out of memory");
  2174. X    if (!t.PutByte((unsigned char)KeyToInt(key)))
  2175. X        return ("Out of memory");
  2176. X    if (!t.PutByte((unsigned char)mode))
  2177. X        return ("Out of memory");
  2178. X    return (0);
  2179. X}
  2180. X
  2181. Xint
  2182. XMetaKeyEvent::Equal(const Event *e) const
  2183. X{
  2184. X    MetaKeyEvent *eptr = (MetaKeyEvent *)e;
  2185. X
  2186. X    return (MetaEvent::Equal(e) && key == eptr->key && mode == eptr->mode);
  2187. X}
  2188. X
  2189. Xostream &
  2190. Xoperator<<(ostream &os, const MetaKeyEvent &e)
  2191. X{
  2192. X    char *str;
  2193. X
  2194. X    os << (str = e.GetEventStr());
  2195. X    delete str;
  2196. X    return (os);
  2197. X}
  2198. X
  2199. XKey
  2200. XStrToKey(const char *str, int *match)
  2201. X{
  2202. X    Key key;
  2203. X    char *keystr;
  2204. X    int badkey, i, keylen;
  2205. X
  2206. X    keylen = strlen(str);
  2207. X    keystr = new char[keylen + 1];
  2208. X    for (i = 0; i < keylen; i++)
  2209. X        keystr[i] = tolower(str[i]);
  2210. X    keystr[i] = '\0';
  2211. X
  2212. X    /* shut up warning */
  2213. X    key = KEY_C;
  2214. X    badkey = 0;
  2215. X    switch (keystr[0]) {
  2216. X    case 'a':
  2217. X        if (strcmp(keystr, "a") == 0)
  2218. X            key = KEY_A;
  2219. X        else if (strcmp(keystr, "a flat") == 0)
  2220. X            key = KEY_AFLAT;
  2221. X        else if (strcmp(keystr, "a sharp") == 0)
  2222. X            key = KEY_BFLAT;
  2223. X        else
  2224. X            badkey = 1;
  2225. X        break;
  2226. X    case 'b':
  2227. X        if (strcmp(keystr, "b") == 0)
  2228. X            key = KEY_B;
  2229. X        else if (strcmp(keystr, "b flat") == 0)
  2230. X            key = KEY_BFLAT;
  2231. X        else if (strcmp(keystr, "b sharp") == 0)
  2232. X            key = KEY_C;
  2233. X        else
  2234. X            badkey = 1;
  2235. X        break;
  2236. X    case 'c':
  2237. X        if (strcmp(keystr, "c") == 0)
  2238. X            key = KEY_C;
  2239. X        else if (strcmp(keystr, "c flat") == 0)
  2240. X            key = KEY_CFLAT;
  2241. X        else if (strcmp(keystr, "c sharp") == 0)
  2242. X            key = KEY_CSHARP;
  2243. X        else
  2244. X            badkey = 1;
  2245. X        break;
  2246. X    case 'd':
  2247. X        if (strcmp(keystr, "d") == 0)
  2248. X            key = KEY_D;
  2249. X        else if (strcmp(keystr, "d flat") == 0)
  2250. X            key = KEY_DFLAT;
  2251. X        else if (strcmp(keystr, "d sharp") == 0)
  2252. X            key = KEY_EFLAT;
  2253. X        else
  2254. X            badkey = 1;
  2255. X        break;
  2256. X    case 'e':
  2257. X        if (strcmp(keystr, "e") == 0)
  2258. X            key = KEY_E;
  2259. X        else if (strcmp(keystr, "e flat") == 0)
  2260. X            key = KEY_EFLAT;
  2261. X        else if (strcmp(keystr, "e sharp") == 0)
  2262. X            key = KEY_F;
  2263. X        else
  2264. X            badkey = 1;
  2265. X        break;
  2266. X    case 'f':
  2267. X        if (strcmp(keystr, "f") == 0)
  2268. X            key = KEY_F;
  2269. X        else if (strcmp(keystr, "f flat") == 0)
  2270. X            key = KEY_E;
  2271. X        else if (strcmp(keystr, "f sharp") == 0)
  2272. X            key = KEY_FSHARP;
  2273. X        else
  2274. X            badkey = 1;
  2275. X        break;
  2276. X    case 'g':
  2277. X        if (strcmp(keystr, "g") == 0)
  2278. X            key = KEY_G;
  2279. X        else if (strcmp(keystr, "g flat") == 0)
  2280. X            key = KEY_GFLAT;
  2281. X        else if (strcmp(keystr, "g sharp") == 0)
  2282. X            key = KEY_AFLAT;
  2283. X        else
  2284. X            badkey = 1;
  2285. X        break;
  2286. X    default:
  2287. X        badkey = 1;
  2288. X    }
  2289. X
  2290. X    delete keystr;
  2291. X    if (badkey)
  2292. X        *match = 0;
  2293. X    else
  2294. X        *match = 1;
  2295. X
  2296. X    return (key);
  2297. X}
  2298. X
  2299. XMode
  2300. XStrToMode(const char *str, int *match)
  2301. X{
  2302. X    Mode mode;
  2303. X    char *modestr;
  2304. X    int i, modelen;
  2305. X
  2306. X    modelen = strlen(str);
  2307. X    modestr = new char[modelen + 1];
  2308. X    for (i = 0; i < modelen; i++)
  2309. X        modestr[i] = tolower(str[i]);
  2310. X    modestr[i] = '\0';
  2311. X
  2312. X    *match = 1;
  2313. X    if (strcmp(modestr, "minor") == 0)
  2314. X        mode = MODE_MINOR;
  2315. X    else if (strcmp(modestr, "major") == 0)
  2316. X        mode = MODE_MAJOR;
  2317. X    else {
  2318. X        mode = MODE_MAJOR;
  2319. X        *match = 0;
  2320. X    }
  2321. X
  2322. X    return (mode);
  2323. X}
  2324. END_OF_FILE
  2325.   if test 7506 -ne `wc -c <'tclmidi-2.0/events/MetaKey.C'`; then
  2326.     echo shar: \"'tclmidi-2.0/events/MetaKey.C'\" unpacked with wrong size!
  2327.   fi
  2328.   # end of 'tclmidi-2.0/events/MetaKey.C'
  2329. fi
  2330. echo shar: End of archive 6 \(of 14\).
  2331. cp /dev/null ark6isdone
  2332. MISSING=""
  2333. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ; do
  2334.     if test ! -f ark${I}isdone ; then
  2335.     MISSING="${MISSING} ${I}"
  2336.     fi
  2337. done
  2338. if test "${MISSING}" = "" ; then
  2339.     echo You have unpacked all 14 archives.
  2340.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  2341. else
  2342.     echo You still must unpack the following archives:
  2343.     echo "        " ${MISSING}
  2344. fi
  2345. exit 0
  2346. exit 0 # Just in case...
  2347.