1 /* srfi-60.c --- Integers as Bits
3 * Copyright (C) 2005, 2006, 2008, 2010, 2014 Free Software Foundation, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 3 of
8 * the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #include "libguile/_scm.h"
28 #include "libguile/eq.h"
30 #include "libguile/validate.h"
31 #include "libguile/numbers.h"
33 #include "libguile/srfi-60.h"
36 SCM_DEFINE (scm_srfi60_log2_binary_factors
, "log2-binary-factors", 1, 0, 0,
38 "Return a count of how many factors of 2 are present in @var{n}.\n"
39 "This is also the bit index of the lowest 1 bit in @var{n}. If\n"
40 "@var{n} is 0, the return is @math{-1}.\n"
43 "(log2-binary-factors 6) @result{} 1\n"
44 "(log2-binary-factors -8) @result{} 3\n"
46 #define FUNC_NAME s_scm_srfi60_log2_binary_factors
52 long nn
= SCM_I_INUM (n
);
54 return SCM_I_MAKINUM (-1);
55 nn
= nn
^ (nn
-1); /* 1 bits for each low 0 and lowest 1 */
56 return scm_logcount (SCM_I_MAKINUM (nn
>> 1));
58 else if (SCM_BIGP (n
))
60 /* no need for scm_remember_upto_here_1 here, mpz_scan1 doesn't do
61 anything that could result in a gc */
62 return SCM_I_MAKINUM (mpz_scan1 (SCM_I_BIG_MPZ (n
), 0L));
65 SCM_WRONG_TYPE_ARG (SCM_ARG1
, n
);
72 SCM_DEFINE (scm_srfi60_copy_bit
, "copy-bit", 3, 0, 0,
73 (SCM index
, SCM n
, SCM newbit
),
74 "Return @var{n} with the bit at @var{index} set according to\n"
75 "@var{newbit}. @var{newbit} should be @code{#t} to set the bit\n"
76 "to 1, or @code{#f} to set it to 0. Bits other than at\n"
77 "@var{index} are unchanged in the return.\n"
80 "(copy-bit 1 #b0101 #t) @result{} 7\n"
82 #define FUNC_NAME s_scm_srfi60_copy_bit
88 ii
= scm_to_ulong (index
);
89 bb
= scm_to_bool (newbit
);
93 long nn
= SCM_I_INUM (n
);
95 /* can't set high bit ii==SCM_LONG_BIT-1, that would change the sign,
96 which is not what's wanted */
97 if (ii
< SCM_LONG_BIT
-1)
99 nn
&= ~(1L << ii
); /* zap bit at index */
100 nn
|= ((long) bb
<< ii
); /* insert desired bit */
101 return scm_from_long (nn
);
105 /* bits at ii==SCM_LONG_BIT-1 and above are all copies of the sign
106 bit, if this is already the desired "bit" value then no need to
107 make a new bignum value */
111 r
= scm_i_long2big (nn
);
115 else if (SCM_BIGP (n
))
117 /* if the bit is already what's wanted then no need to make a new
119 if (bb
== mpz_tstbit (SCM_I_BIG_MPZ (n
), ii
))
122 r
= scm_i_clonebig (n
, 1);
125 mpz_setbit (SCM_I_BIG_MPZ (r
), ii
);
127 mpz_clrbit (SCM_I_BIG_MPZ (r
), ii
);
129 /* changing a high bit might put the result into range of a fixnum */
130 return scm_i_normbig (r
);
133 SCM_WRONG_TYPE_ARG (SCM_ARG1
, n
);
138 SCM_DEFINE (scm_srfi60_rotate_bit_field
, "rotate-bit-field", 4, 0, 0,
139 (SCM n
, SCM count
, SCM start
, SCM end
),
140 "Return @var{n} with the bit field from @var{start} (inclusive)\n"
141 "to @var{end} (exclusive) rotated upwards by @var{count} bits.\n"
143 "@var{count} can be positive or negative, and it can be more\n"
144 "than the field width (it'll be reduced modulo the width).\n"
147 "(rotate-bit-field #b0110 2 1 4) @result{} #b1010\n"
149 #define FUNC_NAME s_scm_srfi60_rotate_bit_field
151 unsigned long ss
= scm_to_ulong (start
);
152 unsigned long ee
= scm_to_ulong (end
);
153 unsigned long ww
, cc
;
155 SCM_ASSERT_RANGE (3, end
, (ee
>= ss
));
158 /* we must avoid division by zero, and a field whose width is 0 or 1
159 will be left unchanged anyway, so in that case we set cc to 0. */
163 cc
= scm_to_ulong (scm_modulo (count
, scm_difference (end
, start
)));
167 long nn
= SCM_I_INUM (n
);
169 if (ee
<= SCM_LONG_BIT
-1)
171 /* Everything fits within a long. To avoid undefined behavior
172 when shifting negative numbers, we do all operations using
173 unsigned values, and then convert to signed at the end. */
174 unsigned long unn
= nn
;
175 unsigned long below
= unn
& ((1UL << ss
) - 1); /* below start */
176 unsigned long above
= unn
& ~((1UL << ee
) - 1); /* above end */
177 unsigned long fmask
= ((1UL << ww
) - 1) << ss
; /* field mask */
178 unsigned long ff
= unn
& fmask
; /* field */
179 unsigned long uresult
= (above
180 | ((ff
<< cc
) & fmask
)
181 | ((ff
>> (ww
-cc
)) & fmask
)
185 if (uresult
> LONG_MAX
)
186 /* The high bit is set in uresult, so the result is
187 negative. We have to handle the conversion to signed
188 integer carefully, to avoid undefined behavior. First we
189 compute ~uresult, equivalent to (ULONG_MAX - uresult),
190 which will be between 0 and LONG_MAX (inclusive): exactly
191 the set of numbers that can be represented as both signed
192 and unsigned longs and thus convertible between them. We
193 cast that difference to a signed long and then substract
195 result
= -1 - (long) ~uresult
;
197 result
= (long) uresult
;
199 return scm_from_long (result
);
203 /* if there's no movement, avoid creating a bignum. */
207 n
= scm_i_long2big (nn
);
211 else if (SCM_BIGP (n
))
216 /* if there's no movement, avoid creating a new bignum. */
221 r
= scm_i_ulong2big (0);
224 /* portion above end */
225 mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (r
), SCM_I_BIG_MPZ (n
), ee
);
226 mpz_mul_2exp (SCM_I_BIG_MPZ (r
), SCM_I_BIG_MPZ (r
), ee
);
228 /* field high part, width-count bits from start go to start+count */
229 mpz_fdiv_q_2exp (tmp
, SCM_I_BIG_MPZ (n
), ss
);
230 mpz_fdiv_r_2exp (tmp
, tmp
, ww
- cc
);
231 mpz_mul_2exp (tmp
, tmp
, ss
+ cc
);
232 mpz_ior (SCM_I_BIG_MPZ (r
), SCM_I_BIG_MPZ (r
), tmp
);
234 /* field low part, count bits from end-count go to start */
235 mpz_fdiv_q_2exp (tmp
, SCM_I_BIG_MPZ (n
), ee
- cc
);
236 mpz_fdiv_r_2exp (tmp
, tmp
, cc
);
237 mpz_mul_2exp (tmp
, tmp
, ss
);
238 mpz_ior (SCM_I_BIG_MPZ (r
), SCM_I_BIG_MPZ (r
), tmp
);
240 /* portion below start */
241 mpz_fdiv_r_2exp (tmp
, SCM_I_BIG_MPZ (n
), ss
);
242 mpz_ior (SCM_I_BIG_MPZ (r
), SCM_I_BIG_MPZ (r
), tmp
);
246 /* bits moved around might leave us in range of an inum */
247 return scm_i_normbig (r
);
250 SCM_WRONG_TYPE_ARG (SCM_ARG1
, n
);
255 SCM_DEFINE (scm_srfi60_reverse_bit_field
, "reverse-bit-field", 3, 0, 0,
256 (SCM n
, SCM start
, SCM end
),
257 "Return @var{n} with the bits between @var{start} (inclusive) to\n"
258 "@var{end} (exclusive) reversed.\n"
261 "(reverse-bit-field #b101001 2 4) @result{} #b100101\n"
263 #define FUNC_NAME s_scm_srfi60_reverse_bit_field
265 long ss
= scm_to_long (start
);
266 long ee
= scm_to_long (end
);
267 long swaps
= (ee
- ss
) / 2; /* number of swaps */
272 long nn
= SCM_I_INUM (n
);
274 if (ee
<= SCM_LONG_BIT
-1)
276 /* all within a long */
277 long smask
= 1L << ss
;
278 long emask
= 1L << (ee
-1);
279 for ( ; swaps
> 0; swaps
--)
281 long sbit
= nn
& smask
;
282 long ebit
= nn
& emask
;
283 nn
^= sbit
^ (ebit
? smask
: 0) /* zap sbit, put ebit value */
284 ^ ebit
^ (sbit
? emask
: 0); /* zap ebit, put sbit value */
289 return scm_from_long (nn
);
293 /* avoid creating a new bignum if reversing only 0 or 1 bits */
297 b
= scm_i_long2big (nn
);
301 else if (SCM_BIGP (n
))
303 /* avoid creating a new bignum if reversing only 0 or 1 bits */
307 b
= scm_i_clonebig (n
, 1);
311 for ( ; swaps
> 0; swaps
--)
313 int sbit
= mpz_tstbit (SCM_I_BIG_MPZ (b
), ss
);
314 int ebit
= mpz_tstbit (SCM_I_BIG_MPZ (b
), ee
);
317 /* the two bits are different, flip them */
320 mpz_clrbit (SCM_I_BIG_MPZ (b
), ss
);
321 mpz_setbit (SCM_I_BIG_MPZ (b
), ee
);
325 mpz_setbit (SCM_I_BIG_MPZ (b
), ss
);
326 mpz_clrbit (SCM_I_BIG_MPZ (b
), ee
);
332 /* swapping zero bits into the high might make us fit a fixnum */
333 return scm_i_normbig (b
);
336 SCM_WRONG_TYPE_ARG (SCM_ARG1
, n
);
341 SCM_DEFINE (scm_srfi60_integer_to_list
, "integer->list", 1, 1, 0,
343 "Return bits from @var{n} in the form of a list of @code{#t} for\n"
344 "1 and @code{#f} for 0. The least significant @var{len} bits\n"
345 "are returned, and the first list element is the most\n"
346 "significant of those bits. If @var{len} is not given, the\n"
347 "default is @code{(integer-length @var{n})} (@pxref{Bitwise\n"
351 "(integer->list 6) @result{} (#t #t #f)\n"
352 "(integer->list 1 4) @result{} (#f #f #f #t)\n"
354 #define FUNC_NAME s_scm_srfi60_integer_to_list
359 if (SCM_UNBNDP (len
))
360 len
= scm_integer_length (n
);
361 ll
= scm_to_ulong (len
);
365 long nn
= SCM_I_INUM (n
);
366 for (i
= 0; i
< ll
; i
++)
368 unsigned long shift
=
369 (i
< ((unsigned long) SCM_LONG_BIT
-1))
370 ? i
: ((unsigned long) SCM_LONG_BIT
-1);
371 int bit
= (nn
>> shift
) & 1;
372 ret
= scm_cons (scm_from_bool (bit
), ret
);
375 else if (SCM_BIGP (n
))
377 for (i
= 0; i
< ll
; i
++)
378 ret
= scm_cons (scm_from_bool (mpz_tstbit (SCM_I_BIG_MPZ (n
), i
)),
380 scm_remember_upto_here_1 (n
);
383 SCM_WRONG_TYPE_ARG (SCM_ARG1
, n
);
390 SCM_DEFINE (scm_srfi60_list_to_integer
, "list->integer", 1, 0, 0,
392 "Return an integer formed bitwise from the given @var{lst} list\n"
393 "of booleans. Each boolean is @code{#t} for a 1 and @code{#f}\n"
394 "for a 0. The first element becomes the most significant bit in\n"
398 "(list->integer '(#t #f #t #f)) @result{} 10\n"
400 #define FUNC_NAME s_scm_srfi60_list_to_integer
404 /* strip high zero bits from lst; after this the length tells us whether
405 an inum or bignum is required */
406 while (scm_is_pair (lst
) && scm_is_false (SCM_CAR (lst
)))
409 SCM_VALIDATE_LIST_COPYLEN (SCM_ARG1
, lst
, len
);
411 if (len
<= SCM_I_FIXNUM_BIT
- 1)
413 /* fits an inum (a positive inum) */
415 while (scm_is_pair (lst
))
418 if (! scm_is_false (SCM_CAR (lst
)))
422 return SCM_I_MAKINUM (n
);
427 SCM n
= scm_i_ulong2big (0);
428 while (scm_is_pair (lst
))
431 if (! scm_is_false (SCM_CAR (lst
)))
432 mpz_setbit (SCM_I_BIG_MPZ (n
), len
);
441 /* note: don't put "scm_srfi60_list_to_integer" arg on its own line, a
442 newline breaks the snarfer */
443 SCM_REGISTER_PROC (s_srfi60_booleans_to_integer
, "booleans->integer", 0, 0, 1, scm_srfi60_list_to_integer
);
447 scm_register_srfi_60 (void)
449 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION
,
451 (scm_t_extension_init_func
)scm_init_srfi_60
, NULL
);
455 scm_init_srfi_60 (void)
457 #ifndef SCM_MAGIC_SNARFER
458 #include "libguile/srfi-60.x"