2 * libzvbi -- BCD arithmetic for Teletext page numbers
4 * Copyright (C) 2001, 2002 Michael H. Schimek
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA.
22 /* $Id: bcd.h,v 1.19 2008-02-21 07:18:52 mschimek Exp $ */
30 * @addtogroup BCD BCD arithmetic for Teletext page numbers
33 * Teletext page numbers are expressed as packed binary coded decimal
34 * numbers in range 0x100 to 0x8FF. The bcd format encodes one decimal
35 * digit in every hex nibble (four bits) of the number. Page numbers
36 * containing digits 0xA to 0xF are reserved for various system purposes
37 * and not intended for display.
45 * Teletext or Closed Caption page number. For Teletext pages
46 * this is a packed bcd number in range 0x100 ... 0x8FF. Page
47 * numbers containing digits 0xA to 0xF are reserved for various
48 * system purposes, these pages are not intended for display.
50 * Closed Caption page numbers between 1 ... 8 correspond
51 * to the four Caption and Text channels:
53 * <tr><td>1</td><td>Caption 1</td><td>
54 * "Primary synchronous caption service [English]"</td></tr>
55 * <tr><td>2</td><td>Caption 2</td><td>
56 * "Special non-synchronous data that is intended to
57 * augment information carried in the program"</td></tr>
58 * <tr><td>3</td><td>Caption 3</td><td>
59 * "Secondary synchronous caption service, usually
60 * second language [Spanish, French]"</td></tr>
61 * <tr><td>4</td><td>Caption 4</td><td>
62 * "Special non-synchronous data similar to Caption 2"</td></tr>
63 * <tr><td>5</td><td>Text 1</td><td>
64 * "First text service, data usually not program related"</td></tr>
65 * <tr><td>6</td><td>Text 2</td><td>
66 * "Second text service, additional data usually not program related
67 * [ITV data]"</td></tr>
68 * <tr><td>7</td><td>Text 3</td><td>
69 * "Additional text channel"</td></tr>
70 * <tr><td>8</td><td>Text 4</td><td>
71 * "Additional text channel"</td></tr>
80 * This is the subpage number only applicable to Teletext pages,
81 * a packed bcd number in range 0x00 ... 0x99. On special 'clock' pages
82 * (for example listing the current time in different time zones)
83 * it can assume values between 0x0000 ... 0x2359 expressing
84 * local time. These are not actually subpages.
86 typedef int vbi_subno;
91 #define VBI_ANY_SUBNO 0x3F7F
95 #define VBI_NO_SUBNO 0x3F7F
99 * @param dec Decimal number.
101 * Converts a two's complement binary between 0 ... 999 to a
102 * packed bcd number in range 0x000 ... 0x999. Extra digits in
103 * the input will be discarded.
108 _vbi_inline unsigned int
109 vbi_dec2bcd(unsigned int dec)
111 return (dec % 10) + ((dec / 10) % 10) * 16 + ((dec / 100) % 10) * 256;
118 #define vbi_bin2bcd(n) vbi_dec2bcd(n)
122 * @param bcd BCD number.
124 * Converts a packed bcd number between 0x000 ... 0xFFF to a two's
125 * complement binary in range 0 ... 999. Extra digits in the input
129 * Decimal number. The result is undefined when the bcd number contains
130 * hex digits 0xA ... 0xF.
132 _vbi_inline unsigned int
133 vbi_bcd2dec(unsigned int bcd)
135 return (bcd & 15) + ((bcd >> 4) & 15) * 10 + ((bcd >> 8) & 15) * 100;
142 #define vbi_bcd2bin(n) vbi_bcd2dec(n)
146 * @param a BCD number.
147 * @param b BCD number.
149 * Adds two packed bcd numbers, returning a packed bcd sum. Arguments
150 * and result are in range 0xF000 0000 ... 0x0999 9999, that
151 * is -10**7 ... +10**7 - 1 in decimal notation. To subtract you can
152 * add the 10's complement, e. g. -1 = 0xF999 9999.
155 * Packed bcd number. The result is undefined when any of the arguments
156 * contain hex digits 0xA ... 0xF.
158 _vbi_inline unsigned int
159 vbi_add_bcd(unsigned int a, unsigned int b)
166 b = (~b & 0x11111110) >> 3;
174 * @param bcd BCD number.
176 * Tests if @a bcd forms a valid BCD number. The argument must be
177 * in range 0x0000 0000 ... 0x0999 9999.
180 * @c FALSE if @a bcd contains hex digits 0xA ... 0xF.
183 vbi_is_bcd(unsigned int bcd)
185 static const unsigned int x = 0x06666666;
187 return (((bcd + x) ^ (bcd ^ x)) & 0x11111110) == 0;
192 * @param bcd Unsigned BCD number.
193 * @param maximum Unsigned maximum value.
195 * Compares an unsigned packed bcd number digit-wise against a maximum
196 * value, for example 0x295959. @a maximum can contain digits 0x0
200 * @c TRUE if any digit of @a bcd is greater than the
201 * corresponding digit of @a maximum.
206 vbi_bcd_digits_greater (unsigned int bcd,
207 unsigned int maximum)
211 return 0 != (((bcd + maximum) ^ bcd ^ maximum) & 0x11111110);