Skip to content

Added C Implementation of uint32-sqrt function under include/stdlib/math/base/special/fast #2016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# @license Apache-2.0
#
# Copyright (c) 2024 The Stdlib Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# A `.gyp` file for building a Node.js native add-on.
#
# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md
# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md
{
// Define the targets for building the add-on
"targets": [
{
// Name of the target
"target_name": "uint32-sqrt-addon",

// Source files required for building the add-on
"sources": [ "src/main.c", "src/addon.c" ],

// Directory containing header files
"include_dirs": [ "src" ],

// C compiler flags
"cflags_cc": [ "-std=c99" ],

// Exclude exception handling flags
"cflags_cc!": [ "-fno-exceptions" ],

// Conditions based on the host OS
"conditions": [
// Linux-specific compiler flags
["OS=='linux'", {
"cflags": [ "-O3", "-Wall", "-Wextra" ],
"xcode_settings": {
"GCC_PREPROCESSOR_DEFINITIONS": [ "GNUC" ]
}
}],

// macOS-specific compiler flags
["OS=='mac'", {
"cflags": [ "-O3", "-Wall", "-Wextra" ],
"xcode_settings": {
"OTHER_CFLAGS": [ "-fno-objc-arc" ]
}
}],

// Windows-specific compiler flags
["OS=='win'", {
"cflags": [ "/O2", "/W4" ],
"msvs_settings": {
"VCCLCompilerTool": { "ExceptionHandling": 1 }
}
}]
]
}
]
}

]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"variables": {
"src_dir": "./src",
"include_dirs": [
"<!@(node -e \"var arr = require('@stdlib/utils/library-manifest')('./manifest.json',{},{\'basedir\':process.cwd(),\'paths\':\'posix\'}).include; for ( var i = 0; i < arr.length; i++ ) { console.log( arr[ i ] ); }\")"
],
"addon_output_dir": "./src",
"src_files": [
"<(src_dir)/addon.c",
"<!@(node -e \"var arr = require('@stdlib/utils/library-manifest')('./manifest.json',{},{\'basedir\':process.cwd(),\'paths\':\'posix\'}).src; for ( var i = 0; i < arr.length; i++ ) { console.log( arr[ i ] ); }\")"
],
"libraries": [
"<!@(node -e \"var arr = require('@stdlib/utils/library-manifest')('./manifest.json',{},{\'basedir\':process.cwd(),\'paths\':\'posix\'}).libraries; for ( var i = 0; i < arr.length; i++ ) { console.log( arr[ i ] ); }\")"
],
"library_dirs": [
"<!@(node -e \"var arr = require('@stdlib/utils/library-manifest')('./manifest.json',{},{\'basedir\':process.cwd(),\'paths\':\'posix\'}).libpath; for ( var i = 0; i < arr.length; i++ ) { console.log( arr[ i ] ); }\")"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef STDLIB_MATH_BASE_SPECIAL_FAST_UINT32_SQRT_H
#define STDLIB_MATH_BASE_SPECIAL_FAST_UINT32_SQRT_H

/*
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
*/
#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>

/**
* Computes the integer square root of a 32-bit unsigned integer.
*/
uint32_t stdlib_base_fast_uint32_sqrt( uint32_t x );

#ifdef __cplusplus
}
#endif

#endif // !STDLIB_MATH_BASE_SPECIAL_FAST_UINT32_SQRT_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "uint32-sqrt",
"version": "1.0.0",
"description": "A Node.js native add-on for computing the integer square root of a 32-bit unsigned integer.",
"keywords": [
"stdlib",
"math",
"base",
"special",
"fast",
"uint32-sqrt"
],
"repository": {
"type": "git",
"url": "https://github.com/your-username/uint32-sqrt"
},
"author": "Your Name",
"license": "Apache-2.0",
"include": [
"./src"
],
"src": [
"./src/addon.c"
],
"libraries": [],
"libpath": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#/
# @license Apache-2.0
#
# Copyright (c) 2024 The Stdlib Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#/

# VARIABLES #

ifndef VERBOSE
QUIET := @
else
QUIET :=
endif

# RULES #

#/
# Removes generated files for building an add-on.
#
# @example
# make clean-addon
#/
clean-addon:
$(QUIET) -rm -f *.o *.node

.PHONY: clean-addon

#/
# Removes generated files.
#
# @example
# make clean
#/
clean: clean-addon

.PHONY: clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "stdlib/math/base/special/fast/uint32-sqrt.h"
#include "stdlib/napi/export.h"
#include "stdlib/napi/argv_uint32.h"
#include <node_api.h>
#include <assert.h>

static napi_value addon(napi_env env, napi_callback_info info) {
STDLIB_NAPI_ARGV(env, info, argv, argc, 1);
STDLIB_NAPI_ARGV_UINT32(env, value, argv, 0);

napi_value v;
napi_status status = napi_create_uint32(env, stdlib_base_fast_uint32_sqrt(value), &v);
assert(status == napi_ok);

return v;
}

STDLIB_NAPI_MODULE( stdlib_base_fast_uint32_sqrt, addon )
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "stdlib/math/base/special/fast/uint32_sqrt.h"
#include <stdint.h>

uint32_t stdlib_base_fast_uint32_sqrt(uint32_t x) {
uint32_t root = 0;
uint32_t bit = 1 << 30; // Set the second most significant bit

// Adjust the initial bit
while (bit > x) {
bit >>= 2;
}

// Perform a digit-by-digit computation
while (bit != 0) {
uint32_t sum = root + bit;
root >>= 1;
if (x >= sum) {
x -= sum;
root += bit;
}
bit >>= 2;
}

return root;
}