1
/*===========================================================================
2
* FileName : module-srfi60.c
3
* About : SRFI-60 Integers as Bits
5
* Copyright (C) 2005-2006 YamaKen
9
* Redistribution and use in source and binary forms, with or without
10
* modification, are permitted provided that the following conditions
13
* 1. Redistributions of source code must retain the above copyright
14
* notice, this list of conditions and the following disclaimer.
15
* 2. Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in the
17
* documentation and/or other materials provided with the distribution.
18
* 3. Neither the name of authors nor the names of its contributors
19
* may be used to endorse or promote products derived from this software
20
* without specific prior written permission.
22
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
23
* IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
26
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
===========================================================================*/
37
/*=======================================
39
=======================================*/
41
/*=======================================
43
=======================================*/
44
#include "sigscheme.h"
45
#include "sigschemeinternal.h"
47
/*=======================================
48
File Local Struct Declarations
49
=======================================*/
51
/*=======================================
52
File Local Macro Declarations
53
=======================================*/
54
#define BITWISE_OPERATION_BODY(op, left, right) \
65
case SCM_REDUCE_PARTWAY: \
66
case SCM_REDUCE_LAST: \
69
result = (SCM_INT_VALUE(left) op SCM_INT_VALUE(right)); \
72
SCM_ASSERT(scm_false); \
74
return MAKE_INT(result); \
75
} while (/* CONSTCOND */ 0)
77
/*=======================================
79
=======================================*/
80
#include "functable-srfi60.c"
82
/*=======================================
83
File Local Function Declarations
84
=======================================*/
86
/*=======================================
87
Function Implementations
88
=======================================*/
90
scm_initialize_srfi60(void)
92
scm_register_funcs(scm_srfi60_func_info_table);
94
scm_define_alias("bitwise-and", "logand");
95
scm_define_alias("bitwise-ior", "logior");
96
scm_define_alias("bitwise-xor", "logxor");
97
scm_define_alias("bitwise-not", "lognot");
98
scm_define_alias("bitwise-merge", "bitwise-if");
99
scm_define_alias("any-bits-set?", "logtest");
102
/* Bitwise Operations */
104
scm_p_srfi60_logand(ScmObj left, ScmObj right, enum ScmReductionState *state)
106
DECLARE_FUNCTION("logand", reduction_operator);
108
BITWISE_OPERATION_BODY(&, left, right);
112
scm_p_srfi60_logior(ScmObj left, ScmObj right, enum ScmReductionState *state)
114
DECLARE_FUNCTION("logior", reduction_operator);
116
BITWISE_OPERATION_BODY(|, left, right);
120
scm_p_srfi60_logxor(ScmObj left, ScmObj right, enum ScmReductionState *state)
122
DECLARE_FUNCTION("logxor", reduction_operator);
124
BITWISE_OPERATION_BODY(^, left, right);
128
scm_p_srfi60_lognot(ScmObj n)
130
DECLARE_FUNCTION("lognot", procedure_fixed_1);
134
return MAKE_INT(~SCM_INT_VALUE(n));
138
scm_p_srfi60_bitwise_if(ScmObj mask, ScmObj n0, ScmObj n1)
140
scm_int_t result, c_mask;
141
DECLARE_FUNCTION("bitwise-if", procedure_fixed_3);
147
c_mask = SCM_INT_VALUE(mask);
148
result = (c_mask & SCM_INT_VALUE(n0)) | (~c_mask & SCM_INT_VALUE(n1));
150
return MAKE_INT(result);
154
scm_p_srfi60_logtest(ScmObj j, ScmObj k)
156
DECLARE_FUNCTION("logtest", procedure_fixed_2);
161
return MAKE_BOOL(SCM_INT_VALUE(j) & SCM_INT_VALUE(k));