All Projects → lemire → Fastrange

lemire / Fastrange

Licence: apache-2.0
A fast alternative to the modulo reduction

Programming Languages

c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to Fastrange

Spans
Spans is a pure Python implementation of PostgreSQL's range types.
Stars: ✭ 106 (-53.91%)
Mutual labels:  interval, range
Monster
The Art of Template MetaProgramming (TMP) in Modern C++♦️
Stars: ✭ 90 (-60.87%)
Mutual labels:  algorithm, range
Period
PHP's time range API
Stars: ✭ 616 (+167.83%)
Mutual labels:  interval, range
Codejam
Set of handy reusable .NET components that can simplify your daily work and save your time when you copy and paste your favorite helper methods and classes from one project to another
Stars: ✭ 217 (-5.65%)
Mutual labels:  algorithm, range
Hackerrank
Solution to HackerRank problems
Stars: ✭ 218 (-5.22%)
Mutual labels:  algorithm
Competitive Programming
My competitive programming guide,reading materials, link to system and design interview preparation and my own coding solutions from Codechef, Leetcode,Geeks for Geeks, HackerRank , spoj, codesignal, codebyte, codeblocks and other online judges
Stars: ✭ 206 (-10.43%)
Mutual labels:  algorithm
Javascript Utilities
一些常用算法的 JavaScript 实现
Stars: ✭ 214 (-6.96%)
Mutual labels:  algorithm
Haste
Haste: a fast, simple, and open RNN library
Stars: ✭ 214 (-6.96%)
Mutual labels:  algorithm
Rangetouch
A super tiny library to make `<input type='range'>` sliders work better on touch devices
Stars: ✭ 224 (-2.61%)
Mutual labels:  range
Ngraph.path
Path finding in a graph
Stars: ✭ 2,545 (+1006.52%)
Mutual labels:  algorithm
Develop Source
Open source for developer.(开发资源整理:Java,Android,算法,iOS,MacOS等等)
Stars: ✭ 219 (-4.78%)
Mutual labels:  algorithm
Play With Data Structures
慕课 liuyubobobo「玩转数据结构」课程的 Go 语言实现版本
Stars: ✭ 217 (-5.65%)
Mutual labels:  algorithm
Panyifei.github.io
请访问 http://panyifei.github.io 一个前端工程狮的打怪日常,欢迎star
Stars: ✭ 220 (-4.35%)
Mutual labels:  algorithm
Gbox
🎨 A multi-platform graphic library
Stars: ✭ 216 (-6.09%)
Mutual labels:  algorithm
C Algorithms
A library of common data structures and algorithms written in C.
Stars: ✭ 2,654 (+1053.91%)
Mutual labels:  algorithm
Hkube
🐟 High Performance Computing over Kubernetes - Core Repo 🎣
Stars: ✭ 214 (-6.96%)
Mutual labels:  algorithm
Ion.rangeslider
jQuery only range slider
Stars: ✭ 2,494 (+984.35%)
Mutual labels:  range
Way To Algorithm
Algorithm Tutorial and Source Code
Stars: ✭ 221 (-3.91%)
Mutual labels:  algorithm
Ekalgorithms
EKAlgorithms contains some well known CS algorithms & data structures.
Stars: ✭ 2,421 (+952.61%)
Mutual labels:  algorithm
React Slider Kit
react-slider-kit is going to be a comprehensive solution to slider feature in react.
Stars: ✭ 219 (-4.78%)
Mutual labels:  range

fastrange: A fast alternative to the modulo reduction

A common operation in software is to take a machine word and map it to an integer value in a range [0,p) as fairly as possible. That is, you want that if all values of the machine word are equally likely then, as much as possible, all integer values in [0,p) are (almost) equally likely. This is common in hashing and probabilistic algorithms.

In computing, the modulo operation finds the remainder after division of one number by another (called the modulus of the operation). Given two positive numbers, a and n, a modulo n (abbreviated as a mod n) is the remainder of the Euclidean division of a by n, where a is the dividend and n is the divisor.

The standard approach to this problem is the modulo reduction (x % p). Though a modulo reduction works fine and has several nice properties, it can be slow even on recent processors because it relies on an integer division.

Thankfully, there is a faster way: we can replace the modulo by a multiplication followed by a shift.

It has accelerated some operations in Google's Tensorflow by 10% to 20%.

Further reading : http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/

See also:

This library provides a single portable header file that you should be able to just drop in your C/C++ projects. The API is simple:

#include "fastrange"

// given a value word, produces an integer in [0,p) without division
uint32_t fastrange32(uint32_t word, uint32_t p);
uint64_t fastrange64(uint64_t word, uint64_t p);
size_t fastrangesize(size_t word, size_t p);
int fastrangeint(int word, int p);

Pre-conditions

For this code to give the desired result, the provided words should span the whole range (e.g., all 32-bit integers). The C rand function does not meet this requirement. If you must use the rand function, wrap it like so:

uint32_t get32rand() {
    return (rand() ^ (rand() << 15) ^ (rand() << 30));
}

uint64_t get64rand() {
    return (((uint64_t)get32rand()) << 32) | get32rand();
}

However, the underlying idea is general: we only require that the word values span an interval of the form [0,1<<L). It suffices then to do ( x * range ) >> L to get a fair map from [0,1<<L) to [0,range). For example, if your word values span the interval [0,65536), then you could simply do ( x * range ) >> 16.

Unbiased range functions

To generate an unbiased random number in a range, you can use an extension of this approach:

uint32_t random_bounded(uint32_t range) {
  uint64_t random32bit =  random32(); //32-bit random number 
  multiresult = random32bit * range;
  leftover = (uint32_t) multiresult;
  if(leftover < range ) {
      threshold = -range % range ;
      while (leftover < threshold) {
            random32bit =  random32();
            multiresult = random32bit * range;
            leftover = (uint32_t) multiresult;
      }
   }
  return multiresult >> 32;
}

See http://lemire.me/blog/2016/06/30/fast-random-shuffling/

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].