Skip to content

Commit 3738279

Browse files
committed
feat: c files added
1 parent 467a868 commit 3738279

File tree

4 files changed

+238
-0
lines changed

4 files changed

+238
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#ifndef STDLIB_MATH_BASE_ASSERT_IS_ODDF_H
20+
#define STDLIB_MATH_BASE_ASSERT_IS_ODDF_H
21+
22+
#include <stdbool.h>
23+
24+
/*
25+
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
26+
*/
27+
#ifdef __cplusplus
28+
extern "C" {
29+
#endif
30+
31+
/**
32+
* Tests if a finite single-precision floating-point number is an odd number.
33+
*/
34+
bool stdlib_base_is_oddf( const float x );
35+
36+
#ifdef __cplusplus
37+
}
38+
#endif
39+
40+
#endif // !STDLIB_MATH_BASE_ASSERT_IS_ODDF_H
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#/
2+
# @license Apache-2.0
3+
#
4+
# Copyright (c) 2024 The Stdlib Authors.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#/
18+
19+
# VARIABLES #
20+
21+
ifndef VERBOSE
22+
QUIET := @
23+
else
24+
QUIET :=
25+
endif
26+
27+
# Determine the OS ([1][1], [2][2]).
28+
#
29+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
30+
# [2]: http://stackoverflow.com/a/27776822/2225624
31+
OS ?= $(shell uname)
32+
ifneq (, $(findstring MINGW,$(OS)))
33+
OS := WINNT
34+
else
35+
ifneq (, $(findstring MSYS,$(OS)))
36+
OS := WINNT
37+
else
38+
ifneq (, $(findstring CYGWIN,$(OS)))
39+
OS := WINNT
40+
else
41+
ifneq (, $(findstring Windows_NT,$(OS)))
42+
OS := WINNT
43+
endif
44+
endif
45+
endif
46+
endif
47+
48+
49+
# RULES #
50+
51+
#/
52+
# Removes generated files for building an add-on.
53+
#
54+
# @example
55+
# make clean-addon
56+
#/
57+
clean-addon:
58+
$(QUIET) -rm -f *.o *.node
59+
60+
.PHONY: clean-addon
61+
62+
#/
63+
# Removes generated files.
64+
#
65+
# @example
66+
# make clean
67+
#/
68+
clean: clean-addon
69+
70+
.PHONY: clean
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "stdlib/math/base/assert/is_oddf.h"
20+
#include <node_api.h>
21+
#include <stdint.h>
22+
#include <assert.h>
23+
24+
/**
25+
* Receives JavaScript callback invocation data.
26+
*
27+
* @param env environment under which the function is invoked
28+
* @param info callback data
29+
* @return Node-API value
30+
*/
31+
static napi_value addon( napi_env env, napi_callback_info info ) {
32+
napi_status status;
33+
34+
// Get callback arguments:
35+
size_t argc = 1;
36+
napi_value argv[ 1 ];
37+
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
38+
assert( status == napi_ok );
39+
40+
// Check whether we were provided the correct number of arguments:
41+
if ( argc < 1 ) {
42+
status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." );
43+
assert( status == napi_ok );
44+
return NULL;
45+
}
46+
if ( argc > 1 ) {
47+
status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." );
48+
assert( status == napi_ok );
49+
return NULL;
50+
}
51+
52+
napi_valuetype vtype0;
53+
status = napi_typeof( env, argv[ 0 ], &vtype0 );
54+
assert( status == napi_ok );
55+
if ( vtype0 != napi_number ) {
56+
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
57+
assert( status == napi_ok );
58+
return NULL;
59+
}
60+
61+
float x;
62+
status = napi_get_value_float( env, argv[ 0 ], &x );
63+
assert( status == napi_ok );
64+
65+
bool result = stdlib_base_is_oddf( x );
66+
67+
napi_value v;
68+
status = napi_create_int32( env, (int32_t)result, &v );
69+
assert( status == napi_ok );
70+
71+
return v;
72+
}
73+
74+
/**
75+
* Initializes a Node-API module.
76+
*
77+
* @param env environment under which the function is invoked
78+
* @param exports exports object
79+
* @return main export
80+
*/
81+
static napi_value init( napi_env env, napi_value exports ) {
82+
napi_value fcn;
83+
napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn );
84+
assert( status == napi_ok );
85+
return fcn;
86+
}
87+
88+
NAPI_MODULE( NODE_GYP_MODULE_NAME, init )
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "stdlib/math/base/assert/is_oddf.h"
20+
#include "stdlib/math/base/assert/is_evenf.h"
21+
22+
/**
23+
* Tests if a finite single-precision floating-point number is an odd number.
24+
*
25+
* @param x input value
26+
* @return output value
27+
*
28+
* @example
29+
* #include <stdbool.h>
30+
*
31+
* bool out = stdlib_base_is_oddf( 3.0f );
32+
* // returns true
33+
*/
34+
bool stdlib_base_is_oddf( const float x ) {
35+
// Check sign to prevent overflow...
36+
if ( x > 0.0f ) {
37+
return stdlib_base_is_evenf( x - 1.0f );
38+
}
39+
return stdlib_base_is_evenf( x + 1.0f );
40+
}

0 commit comments

Comments
 (0)